Cifrando y Descifrando C#

(1/2) > >>

junxcosio:
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:
Código
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.

raul338:
Código
void Main()
{
           string xD = ""; // Aca iria la llave
 
           Console.Write("Escriba texto a cifrar por RSA: ");
           string s = Console.ReadLine();
           byte[] matrix = Encoding.ASCII.GetBytes(s);
 
           Console.Write("Bytes de lo escrito: ");
           foreach (byte i in matrix)
               Console.Write(i.ToString() + "-");
 
           Console.WriteLine();
           Console.WriteLine();
 
           RSACryptoServiceProvider rcsp = new RSACryptoServiceProvider();
 
           byte[] result = rcsp.Encrypt(matrix, true);
           xD = rcsp.ToXmlString(true); // Guardamos las llave
           Console.WriteLine(xD);
 
           Console.WriteLine("Texto cifrado: ");
           Console.WriteLine(Encoding.ASCII.GetString(result));
           Console.WriteLine();
 
           // Mostramos los bytes cifrados
           foreach (byte i in result)
               Console.Write(i.ToString() + "-");
 
           Console.WriteLine();
           Console.WriteLine();
 
           RSACryptoServiceProvider rcsp2 = new RSACryptoServiceProvider();
           //rcsp2.CspKeyContainerInfo = rcsp.CspKeyContainerInfo;
           rcsp2.FromXmlString(xD); // Aca retomamos la llave
           matrix = rcsp2.Decrypt(result, true);
           Console.WriteLine("Texto descifrado: " + Encoding.ASCII.GetString(matrix));
 
 
           Console.ReadLine();
}
 

Ese es mi codigo de prueba que hice hace unos dias, el RSA es asimetrico y genera una llave cuando encriptas, que la tienes que guardar y pasarsela cuando desencriptas, sino es imposible :P

shug0:

[se adelantaron xD]
con la exception que tira el error, en el msdn de win$ te lleva aqui:

http://support.microsoft.com/kb/842791/es

comenta 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:]

Código
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();
 
 
       }
   }
}

raul338:
Claro, asi funciona, pero ahora con eso tienes un problema, intenta guardar el texto cifrado, cierra el programa, abrelo e intenta desifrarlo, como veras (o supondras) tira error, tienes que guardar el texto cifrado, y el XML de los parametros que te genera para desifrarlo. Suerte ;)

junxcosio:
Perfecto he realizado los cambios segun me habeis dicho y efectivamente todo funciona y me ha quedado asi:

Código
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...

Navegación

[0] Índice de Mensajes

[#] Página Siguiente