Tema destacado: Recuerda que debes registrarte en el foro para poder participar (preguntar y responder)
Autor
|
Tema: Cifrando y Descifrando C# (Leído 3,731 veces)
|
junxcosio
Desconectado
Mensajes: 9
|
Pues eso estoy realizando un par de funciones en C# y al cifrar va todo de cine, el problema es que al descifrar me da un error que dice "Datos incorrectos". En esta linea= datades=sec.Decrypt(data, false); el codigo del programa es: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; namespace encriptarstring { 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); 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); datades=sec.Decrypt(data, false); //<------AQUI DA EL ERROR return UTF8Encoding.UTF8.GetString(datades); } 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(); } } } A ver si hay suerte y alguno sabeis que es lo hago mal... Un saludo.
|
|
|
|
« Última modificación: 14 Enero 2010, 15:31 por junxcosio »
|
En línea
|
"Si se puede imaginar, se puede programar..."
|
|
|
|
|
b10s_0v3rr1d3
Desconectado
Mensajes: 185
si puede hacerse debe hacerse
|
[se adelantaron xD] con la exception que tira el error, en el msdn de win$ te lleva aqui: http://support.microsoft.com/kb/842791/escomenta que tienes que usar el mismo vector de inicializacion que usaste a la hora de cifrar los datos [en este caso seria 'sec', con una variable global ya funciona bien:] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; namespace encriptarstring { class Program { public static RSACryptoServiceProvider sec = new RSACryptoServiceProvider(); 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); 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); datades = sec.Decrypt(data, false); //<------AQUI DA EL ERROR return UTF8Encoding.UTF8.GetString(datades); } 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(); } } }
|
|
|
|
|
En línea
|
|
|
|
|
|
junxcosio
Desconectado
Mensajes: 9
|
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...
|
|
|
|
« Última modificación: 14 Enero 2010, 15:37 por junxcosio »
|
En línea
|
"Si se puede imaginar, se puede programar..."
|
|
|
raul338
Conectado
Mensajes: 2.373
La sonrisa es la mejor forma de afrontar las cosas
|
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...
Bueno si, pero no creas que el .net te tiene que dar todo, tu mismo puedes re-cifrar ese XML, juntar varias capas de encriptacion, para asi tener algo sumamente cifrado. Puedes crear algun algoritmo que mezcle lo cifrado con el XML y punto. Usa tu imaginacion 
|
|
|
|
|
En línea
|
|
|
|
junxcosio
Desconectado
Mensajes: 9
|
ok, estoy ahora haciendo unas pruebas con el cifrado 3DES... cuando lo termine lo pongo por si le vale a alguien...porque lo que estoy haciendo es que la llave la cifro con el 3DES...
un saludo.
|
|
|
|
|
En línea
|
"Si se puede imaginar, se puede programar..."
|
|
|
|
|