Tôi hiện đang làm việc về triển khai AES trong C#. Phương thức mã hóa có hai tham số: một chuỗi và một mật khẩu. Tôi đang lấy chuỗi cung cấp và chuyển đổi nó thành một mảng byte, vì vậy tôi có thể sử dụng nó sau này để ghi dữ liệu vào một luồng với BinaryWriter
.Làm thế nào để nhận được byte chính xác và nhất quán từ một chuỗi mã hóa AES?
Vấn đề là khi tôi sử dụng Convert.FromBase64String(string)
tôi nhận được FormatException: Invalid length.
và khi tôi sử dụng Encoding.UTF8.GetBytes(string)
phương thức giải mã của tôi sẽ ném và ngoại lệ PKCS7.Padding không hợp lệ.
Tôi đã cố gắng giải quyết vấn đề này trong vài ngày qua. Tôi đã đọc gần các câu hỏi vô hạn trong stackoverflow.com và các trang web khác, nhưng tôi vẫn không biết cách đáng tin cậy nhất để giải quyết vấn đề này là gì.
Các chuỗi sẽ được sử dụng trong chương trình này được giới hạn trong các câu (ví dụ: "Cái gì đó để mã hóa") và các số (ví dụ: "12345").
Cảm ơn bạn trước, đây là đoạn code tôi có vào thời điểm này trong thời gian:
public class AESProvider {
public byte[] EncryptStringToBytes_Aes(string plainText, string Key)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
byte[] plainTextInBytes = Convert.FromBase64String(plainText);
byte[] encrypted;
//Create an Aes object
//with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.GenerateIV();
byte[] IV = aesAlg.IV;
//The Salt will be the first 8 bytes of the IV.
byte[] theSalt = new byte[8];
Array.Copy(IV,theSalt,8);
//A key for AES is generated by expanding the password using the following method.
Rfc2898DeriveBytes keyGen = new Rfc2898DeriveBytes(Key,theSalt);
byte[] aesKey = keyGen.GetBytes(16);
aesAlg.Key = aesKey;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (BinaryWriter swEncrypt = new BinaryWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainTextInBytes);
}
encrypted = msEncrypt.ToArray();
}
}
// Prepend the IV to the ciphertext so it can be used in the decryption process.
using (MemoryStream ivPlusCipher = new MemoryStream())
{
using (BinaryWriter tBinaryWriter = new BinaryWriter(ivPlusCipher))
{
tBinaryWriter.Write(IV);
tBinaryWriter.Write(encrypted);
tBinaryWriter.Flush();
}
return ivPlusCipher.ToArray();
}
}
}
public byte[] DecryptStringFromBytes_Aes(byte[] cipherText, string Key)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the string used to hold
// the decrypted text.
byte[] decrypted;
// Create an Aes object
// with the specified key and IV.
// Create the streams used for decryption.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.PKCS7;
//Grab IV from ciphertext
byte[] IV = new byte[16];
Array.Copy(cipherText,0,IV,0,16);
//Use the IV for the Salt
byte[] theSalt = new byte[8];
Array.Copy(IV,theSalt,8);
Rfc2898DeriveBytes keyGen = new Rfc2898DeriveBytes(Key,theSalt);
byte[] aesKey = keyGen.GetBytes(16);
aesAlg.Key = aesKey;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, IV);
using (MemoryStream msDecrypt = new MemoryStream())
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
{
using (BinaryWriter srDecrypt = new BinaryWriter(csDecrypt))
{
//Decrypt the ciphertext
srDecrypt.Write(cipherText, IV.Length, (cipherText.Length - IV.Length));
}
decrypted = msDecrypt.ToArray();
return decrypted;
}
}
}
}
}
'DecryptStringFromBytes' của bạn không trả lại chuỗi, tại sao? –
Câu hỏi quan trọng nhất là và sẽ vẫn là: những gì được đặt trong chuỗi đầu vào mà bạn có nghĩa vụ phải giải mã. Ý tôi là, nếu nó là hệ thập lục phân thì không có câu trả lời nào đúng. Vui lòng cung cấp một ví dụ! –
Cảm ơn bạn đã nhập. Ví dụ về các chuỗi đã được thêm vào câu hỏi! – Mandos