Desencriptar cadena de texto con C#

Podemos desencriptar cualquier cadena de texto con C# en base a una key de encriptación. El siguiente código espera un parámetro texto del que queremos desencriptar, la key secreta de encriptación, y el CyphMode).

Podemos desencriptar cualquier cadena de texto con C# en base a una key de encriptación. El siguiente código espera un parámetro texto del que queremos desencriptar, la key secreta de encriptación, y el CyphMode).

La función devolverá el valor desencriptado. Nos puede venir bien si queremos desencriptar id's de valores, por ejemplo...


public static string Decrypt(string p_InputString, string p_SecretKey, CipherMode p_CyphMode)

{

 if (String.IsNullOrEmpty(p_InputString)) {

  return String.Empty;

 } else {

  StringBuilder ret = new StringBuilder();

  byte[] InputbyteArray = new byte[Convert.ToInt32(p_InputString.Length) / 2];

  TripleDESCryptoServiceProvider Des = new TripleDESCryptoServiceProvider();

  MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();


  try {

   Des.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(p_SecretKey));

   Des.Mode = p_CyphMode;

   for (int X = 0; X <;= InputbyteArray.Length - 1; X++) {

    Int32 IJ = (Convert.ToInt32(p_InputString.Substring(X * 2, 2), 16));

    ByteConverter BT = new ByteConverter();

    InputbyteArray[X] = new byte();

    InputbyteArray[X] = Convert.ToByte(BT.ConvertTo(IJ, typeof(byte)));

   }


   MemoryStream ms = new MemoryStream();

   CryptoStream cs = new CryptoStream(ms, Des.CreateDecryptor(), CryptoStreamMode.Write);


   //Flush the data through the crypto stream into the memory stream

   cs.Write(InputbyteArray, 0, InputbyteArray.Length);

   cs.FlushFinalBlock();


   //Get the decrypted data back from the memory stream

   byte[] B = ms.ToArray();

   ms.Close();

   for (int I = 0; I <;= B.GetUpperBound(0); I++) {

    ret.Append(Convert.ToChar(B[I]));

   }

  } catch (Exception ex) {

   //   ME.Publish("SF.Utils.Utils", "DecryptString", ex, ManageException_Enumerators.ErrorLevel.FatalError);

   return String.Empty;

  }


  return ret.ToString();

 }

}


Espero que lo disfruten, compartan y comenten. ;)

"Si se puede imaginar... se puede programar."

No hay comentarios:

Publicar un comentario

Google