Mình đang làm phần mềm ứng dụng chat có sử dụng phương pháp mã hóa AES thì gặp lỗi như hình trên. Có ai đã từng gặp lỗi thì chia sẻ kinh nghiệm cho mình với.
Đây là code của 2 hàm mã hóa và giải mã:
/// <summary>
/// Hàm này dùng để mã hóa dữ liệu theo giải thuật AES.
/// </summary>
/// <param name="plaintext">
/// Đây là chuỗi cần mã hóa thuộc kiểu string.
/// </param>
/// <param name="Key">
/// Khóa được dùng để mã hóa thuộc kiểu dữ liệu byte[].
/// </param>
/// <param name="IV">
/// Vector khởi tạo để dùng được mã hóa thuộc kiểu dữ liệu byte[].
/// </param>
/// <param name="mode">
/// Chế độ mã hóa:
/// 1. ECB
/// 2. CBC
/// 3. CFB
/// </param>
/// <returns>
/// Giá trị trả về là chuỗi đã được mã hóa thuộc kiểu dữ liệu byte[].
/// </returns>
public static byte[] EncryptStringToBytes_Aes(string plaintext, string key, string mode)
{
// Check arguments.
if (plaintext == null || plaintext.Length <= 0)
throw new ArgumentNullException("plainText");
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
if (mode == "ECB") aesAlg.Mode = CipherMode.ECB;
else if (mode == "CBC") aesAlg.Mode = CipherMode.CBC;
else if (mode == "CFB") aesAlg.Mode = CipherMode.CFB;
aesAlg.Key = Convert.FromBase64String(key); ;
aesAlg.IV = ASCIIEncoding.ASCII.GetBytes(key.Substring(0, 16));
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plaintext);
}
encrypted = msEncrypt.ToArray();
}
}
return encrypted;
}
}
/// <summary>
/// Hàm này được dùng để giải mã dữ liệu theo giải thuật AES.
/// </summary>
/// <param name="cipherText">
/// Đây là chuỗi đã được mã hóa.
/// </param>
/// <param name="Key">
/// Key dùng để giải mã thuộc kiểu dữ liệu byte[].
/// </param>
/// <param name="IV">
/// Vector khởi tạo dùng để giải mã thuộc kiểu dữ liệu byte[].
/// </param>
/// <param name="mode">
/// Chế độ giải mã:
/// 1. ECB
/// 2. CBC
/// 3. CFB
/// </param>
/// <returns>
/// Giá trị trả về là chuỗi đã được giải mã thuộc kiểu string.
/// </returns>
public static string DecryptStringFromBytes_Aes(byte[] cipherText, string key, string mode)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
if (mode == "ECB") aesAlg.Mode = CipherMode.ECB;
else if (mode == "CBC") aesAlg.Mode = CipherMode.CBC;
else if (mode == "CFB") aesAlg.Mode = CipherMode.CFB;
aesAlg.Key = Convert.FromBase64String(key);
aesAlg.IV = ASCIIEncoding.ASCII.GetBytes(key.Substring(0, 16));
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}