[英]C# Rijndael IV size doesn't match block size, while it should
我有以下代碼:
private void EncryptFile(string inputFile, string outputFile, string pass)
{
try
{
string password = @pass;
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
byte[] iv = new byte[128];
for(int i =0; i < iv.Length; i++)
{
iv[i] = Convert.ToByte(true);
}
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
MessageBox.Show(RMCrypto.BlockSize + "\n" + iv.Length);
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, iv),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch(Exception ex)
{
//MessageBox.Show("Encryption failed!", "Error");
MessageBox.Show(ex.Message);
}
}
但是IV尺寸有問題。 使用一個簡單的消息框,我發現(可能)塊大小為128.因此,我將IV設置為一個128字節的數組,其中包含“1”值以進行測試。 第一個消息框確認塊大小,IV陣列長度都是128.但是,我得到一個例外,說明Specified initialization vector (IV) does not match the block size for this algorithm.
這是為什么以及如何解決這個問題?
AES 塊大小為128 位 。 不是字節。 位。
AES競賽的獲勝者Rijndael支持128,192和256 位的塊和密鑰大小,但在AES中,塊大小始終為128 位 。 AES標准未采用額外的塊大小
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.