Perfecto he realizado los cambios segun me habeis dicho y efectivamente todo funciona y me ha quedado asi:
class Program
{
static string Cifrar(string texto)
{
byte[] dato;
byte[] dato_cifrado;
RSACryptoServiceProvider sec
= new RSACryptoServiceProvider
(); dato=UTF8Encoding.UTF8.GetBytes(texto);
dato_cifrado=sec.Encrypt(dato,false);
Guardarllave(sec.ToXmlString(true));
return Convert.ToBase64String(dato_cifrado, 0, dato_cifrado.Length);
}
static string DesCifrar(string textocifrado)
{
RSACryptoServiceProvider sec
= new RSACryptoServiceProvider
(); byte[] data, datades;
data = Convert.FromBase64String(textocifrado);
sec.FromXmlString(Leerllave());
datades=sec.Decrypt(data, false);
return UTF8Encoding.UTF8.GetString(datades);
}
static void Guardarllave(string llave)
{
XmlWriter w = XmlWriter.Create("llave.xml");
w.WriteComment("Fichero que guarda la llave de encriptacion, by JunXCosio");
w.WriteStartElement("Llave");
w.WriteElementString("key1",llave);
w.WriteEndElement();
w.Close();
}
static string Leerllave()
{
XmlReader r = XmlReader.Create("llave.xml");
r.ReadStartElement("Llave");
string KEY = r.ReadElementContentAsString();
r.Close();
return KEY;
}
static void Main(string[] args)
{
string texto = "Texto que quiero cifrar";
string Texto_Codificado;
Texto_Codificado = Cifrar(texto);
Console.WriteLine("Texto tal cual: {0}", texto);
Console.WriteLine("Texto cifrado: {0}",Texto_Codificado);
Console.WriteLine("Texto descifrado: {0}", DesCifrar(Texto_Codificado));
Console.ReadKey();
}
}
Pero entonces tengo una duda, para poder descifrar siempre hay que tener el fichero xml??? por lo tanto siempre que se teng ese fichero se podra leer el texto cifrado...y si alguien obtiene dicho ficheroy tiene unos minimos conocimientos de .Net lo podra leer...curioso...
Voy a mirar otra cosa ya que esto no me vale...
Muchisimas gracias...