Tôi đang làm việc trên phương pháp mã hóa cơ sở. Tôi đang sử dụng RijndaelManaged. Tôi đã nhận được mã này từ một nơi nào đó từ lâu rồi, nhưng không thể nhớ được ở đâu.Vectơ khởi tạo được chỉ định (IV) không khớp với kích thước khối cho thuật toán này
Tôi đã làm việc với mã trước đây, nhưng đã thay đổi và tôi không thể hiểu được.
Khi tôi chạy mã của mình, tôi nhận được lỗi sau;
Vectơ khởi tạo được chỉ định (IV) không khớp với kích thước khối cho thuật toán này.
Đây là mã của tôi:
string textToEncrypt = "TEST STRING";
int keySize = 256;
string hashAlgorithm = "SHA1";
string passPhrase = "AH!PSB0%FGHR$";
string saltValue = "LRT%YUR#[email protected]";
string initVector = "HR$2pIjHR$2pIj";
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(textToEncrypt);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, 2);
byte[] keyBytes = password.GetBytes(keySize/8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes,initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,encryptor,CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherTextBytes);
Bất kỳ trợ giúp sẽ được đánh giá cao.
Oh my, đây là nó! Cảm ơn bạn rất nhiều vì sự giúp đỡ. –