N
~~~ .NET Ed ~~~
Anybody has any idea why this simple thing is not working? I pass a text
file as input to encrypt it, then pass the encrypted version to the same
function and get some garbled data not at all resembling the input file.
Rijndael rijndaelAlg = Rijndael.Create();
rijndaelAlg.BlockSize = 128; // 128 bits to comply with AES
rijndaelAlg.Padding = PaddingMode.PKCS7;
rijndaelAlg.Mode = CipherMode.CBC;
MD5CryptoServiceProvider m = new MD5CryptoServiceProvider();
PasswordDeriveBytes pdb = new PasswordDeriveBytes(sKey,
m.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(sKey)));
//Set secret key For AES algorithm.
//rijndaelAlg.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
rijndaelAlg.Key = pdb.GetBytes(256/8);
//Set initialization vector.
//rijndaelAlg.IV = ASCIIEncoding.ASCII.GetBytes(sIV);
rijndaelAlg.IV = pdb.GetBytes(16);
FileStream fsIn = new FileStream(sInputFilename, FileMode.Open,
FileAccess.Read);
FileStream fsOut = new FileStream(sOutputFilename, FileMode.Create,
FileAccess.Write);
//Create an AES encryptor from the AES instance.
ICryptoTransform aesencrypt = rijndaelAlg.CreateEncryptor();
//Create crypto stream set to read and do an AES encryption transform on
incoming bytes.
CryptoStream cipherstream = new CryptoStream(fsOut, aesencrypt,
CryptoStreamMode.Write);
int data;
while ((data = fsIn.ReadByte()) != -1)
{
cipherstream.WriteByte((byte) data);
}
byte[] bytearrayinput = new byte[fsIn.Length];
fsIn.Read(bytearrayinput, 0, bytearrayinput.Length);
cipherstream.Write(bytearrayinput, 0, bytearrayinput.Length);
cipherstream.Close();
fsOut.Close();
fsIn.Close();
file as input to encrypt it, then pass the encrypted version to the same
function and get some garbled data not at all resembling the input file.
Rijndael rijndaelAlg = Rijndael.Create();
rijndaelAlg.BlockSize = 128; // 128 bits to comply with AES
rijndaelAlg.Padding = PaddingMode.PKCS7;
rijndaelAlg.Mode = CipherMode.CBC;
MD5CryptoServiceProvider m = new MD5CryptoServiceProvider();
PasswordDeriveBytes pdb = new PasswordDeriveBytes(sKey,
m.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(sKey)));
//Set secret key For AES algorithm.
//rijndaelAlg.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
rijndaelAlg.Key = pdb.GetBytes(256/8);
//Set initialization vector.
//rijndaelAlg.IV = ASCIIEncoding.ASCII.GetBytes(sIV);
rijndaelAlg.IV = pdb.GetBytes(16);
FileStream fsIn = new FileStream(sInputFilename, FileMode.Open,
FileAccess.Read);
FileStream fsOut = new FileStream(sOutputFilename, FileMode.Create,
FileAccess.Write);
//Create an AES encryptor from the AES instance.
ICryptoTransform aesencrypt = rijndaelAlg.CreateEncryptor();
//Create crypto stream set to read and do an AES encryption transform on
incoming bytes.
CryptoStream cipherstream = new CryptoStream(fsOut, aesencrypt,
CryptoStreamMode.Write);
int data;
while ((data = fsIn.ReadByte()) != -1)
{
cipherstream.WriteByte((byte) data);
}
byte[] bytearrayinput = new byte[fsIn.Length];
fsIn.Read(bytearrayinput, 0, bytearrayinput.Length);
cipherstream.Write(bytearrayinput, 0, bytearrayinput.Length);
cipherstream.Close();
fsOut.Close();
fsIn.Close();