public byte[] Encrypt(byte[] dataToEncrypt, byte[] key, byte[] iv) { using (var des = new DESCryptoServiceProvider()) { des.Mode = CipherMode.CBC; des.Padding = PaddingMode.PKCS7; des.Key = key; des.IV = iv; using (var memoryStream = new MemoryStream()) { var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(), CryptoStreamMode.Write); cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length); cryptoStream.FlushFinalBlock(); return memoryStream.ToArray(); } } } public byte[] Decrypt(byte[] dataToDecrypt, byte[] key, byte[] iv) { using (var des = new DESCryptoServiceProvider()) { des.Mode = CipherMode.CBC; des.Padding = PaddingMode.PKCS7; des.Key = key; des.IV = iv; using (var memoryStream = new MemoryStream()) { var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(), CryptoStreamMode.Write); cryptoStream.Write(dataToDecrypt, 0, dataToDecrypt.Length); cryptoStream.FlushFinalBlock(); return memoryStream.ToArray(); } } }
Triple DES Encryption:
public byte[] Encrypt(byte[] dataToEncrypt, byte[] key, byte[] iv) { using (var des = new TripleDESCryptoServiceProvider()) { des.Mode = CipherMode.CBC; des.Padding = PaddingMode.PKCS7; des.Key = key; des.IV = iv; using (var memoryStream = new MemoryStream()) { var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(), CryptoStreamMode.Write); cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length); cryptoStream.FlushFinalBlock(); return memoryStream.ToArray(); } } } public byte[] Decrypt(byte[] dataToDecrypt, byte[] key, byte[] iv) { using (var des = new TripleDESCryptoServiceProvider()) { des.Mode = CipherMode.CBC; des.Padding = PaddingMode.PKCS7; des.Key = key; des.IV = iv; using (var memoryStream = new MemoryStream()) { var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(), CryptoStreamMode.Write); cryptoStream.Write(dataToDecrypt, 0, dataToDecrypt.Length); cryptoStream.FlushFinalBlock(); var decryptBytes = memoryStream.ToArray(); return decryptBytes; } } }
AES Encryption:
public byte[] Encrypt(byte[] dataToEncrypt, byte[] key, byte[] iv) { using (var aes = new AesCryptoServiceProvider()) { aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; aes.Key = key; aes.IV = iv; using (var memoryStream = new MemoryStream()) { var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write); cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length); cryptoStream.FlushFinalBlock(); return memoryStream.ToArray(); } } } public byte[] Decrypt(byte[] dataToDecrypt, byte[] key, byte[] iv) { using (var aes = new AesCryptoServiceProvider()) { aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; aes.Key = key; aes.IV = iv; using (var memoryStream = new MemoryStream()) { var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write); cryptoStream.Write(dataToDecrypt, 0, dataToDecrypt.Length); cryptoStream.FlushFinalBlock(); var decryptBytes = memoryStream.ToArray(); return decryptBytes; } } }
Không có nhận xét nào :
Đăng nhận xét