I’m using C# .Net 4.0. I generate the key and IV(initialization vector) using the rijindael class. I then write both of them to a file. The IV is always correct when I read the file, but the last byte of the key is always zero. I look at the key before writing to the file and it is fine, reading it back in the last byte is always zero.
I have tried setting the padding mode to the various choices and they don’t make a difference.
using (Rijndael myRijndael = Rijndael.Create())
{
//Create keys
try
{
byte[] key;
byte[] iv;
key = new byte[32];
iv = new byte[16];
theKeys.Key = myRijndael.Key;
theKeys.IV = myRijndael.IV;
FileStream fs = File.Create("yyy.txt");
fs.Write(theKeys.Key, 0, theKeys.Key.Length);
fs.Flush();
fs.Close();
FileStream ts = File.Open("yyy.txt", FileMode.Append);
ts.Write(theKeys.IV, 0, theKeys.IV.Length);
ts.Flush();
ts.Close();
FileStream ms = File.Open("yyy.txt", FileMode.Open);
ms.Read(key, 0, 31);
ms.Seek(32, 0);
ms.Read(iv, 0, 16);
ms.Flush();
ms.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
1 Answer