|
Creato da Arudy il 15/09/2006
Note, appunti, di tutto un pò riguardo alla mia esperienza di programmatore
|
Tag
Cerca in questo Blog
Menu
I miei link preferiti
Area personale
« Modificare lo stato di u... | Icone dei componenti cre... » |
String Encryption
Ecco come criptare e decriptare una semplice stringa di testo:
private static byte[] arrDESKey = new byte[] {12, 16, 63, 156, 58, 4, 218, 32};
private static byte[] arrDESIV = new byte[] {55, 103, 243, 79, 36, 99, 17, 3};
public static string Encrypt(string str)
{
DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
MemoryStream objMemoryStream = new MemoryStream();
CryptoStream objCryptoStream = new CryptoStream(objMemoryStream,objDES.CreateEncryptor(arrDESKey,arrDESIV),CryptoStreamMode.Write);
StreamWriter objStreamWriter = new StreamWriter(objCryptoStream);
objStreamWriter.Write(str);
objStreamWriter.Flush();
objCryptoStream.FlushFinalBlock();
objMemoryStream.Flush();
return Convert.ToBase64String(objMemoryStream.GetBuffer(), 0, (int)objMemoryStream.Length);
}
public static string Decrypt(string str)
{
DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
byte[] arrInput = Convert.FromBase64String(str);
MemoryStream objMemoryStream = new MemoryStream(arrInput);
CryptoStream objCryptoStream = new CryptoStream(objMemoryStream,objDES.CreateDecryptor(arrDESKey,arrDESIV),CryptoStreamMode.Read);
StreamReader objStreamReader = new StreamReader(objCryptoStream);
return objStreamReader.ReadToEnd();
}
![]() |