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