Hi i have a problem with Rijndael encryption (I've uploaded the source code). Basically i'm trying to convert an image into byte[] data, run Rijndael encryption on it based on a given password. Then i need to be able to convert that Byte[] data back into a bmp. This is where the problem seems to occur. The line:
tempImage = Image.FromStream(ms, true);
always throws the exception "invalid paramater" meaning that it can't read the byte[] data and convert it into a bmp image. If i take out the Rijndael encryption, there is no problem but for some reason, when i do use it, it seems to give me back an invalid byte[] representation for a bmp?! I've been trying to work out what's going on here for a while and it's getting really frustrating. I'd appreciate any help that can be given.
Also, you may notice that i've used a message box to show me information about the byte data before and after encryption. I did notice that the byte[] data afterwards was 10 larger than before encryption. I don't know why or what this means but it seems to occur first at the line:
byte[] encryptedData = ms2.ToArray();
Thanks in advance :)
Loading
Sam HobbsPosted Apr 9, 2011, 4:00 PM
Samuel Amantea CollinsPosted Apr 9, 2011, 1:33 PM
Samuel Amantea CollinsPosted Apr 2, 2011, 7:18 PM
private void SubmitButton_Click(object sender, EventArgs e)
{
Bitmap b = (Bitmap)Picture;
byte[] Bytes = GetBytes(b);
byte[] EncBytes = Encrypt(Bytes, "sawerfgyujnml");
/*
MessageBox.Show("Byte's length = " + Bytes.Length + '\n'
+"EncByte's length = " + EncBytes.Length + '\n'
+"Byte's rank = " + Bytes.Rank + '\n'
+"Enc Byte's rank = " + EncBytes.Rank + '\n'
+"Byte's Long Lenght = " + Bytes.LongLength + '\n'
+"EncByte's Long Lenght = " + EncBytes.LongLength + '\n'
+"Byte's Is fixed size? " + Bytes.IsFixedSize + '\n'
+"EncByte's Is fixed size? " + EncBytes.IsFixedSize + '\n');
*/
Image EncPicture = GetImage(EncBytes);
pictureBox.Image = EncPicture.GetThumbnailImage(pictureBox.Width, pictureBox.Height,
ThumbnailCallback, System.IntPtr.Zero);
}
private byte[] GetBytes(Bitmap b)
{
using (MemoryStream ms = new MemoryStream())
{
b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] ret = ms.ToArray();
return ret;
}
}
private Image GetImage(byte[] PictureBytes)
{
Image tempImage;
using (MemoryStream ms = new MemoryStream(PictureBytes, 0, PictureBytes.Length))
{
ms.Write(PictureBytes, 0, PictureBytes.Length);
tempImage = Image.FromStream(ms, true);
return tempImage;
}
}
public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
{
MemoryStream ms2 = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms2,
alg.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(clearData, 0, clearData.Length);
cs.Close();
byte[] encryptedData = ms2.ToArray();
return encryptedData;
}
public static byte[] Encrypt(byte[] clearData, string Password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
return Encrypt(clearData, pdb.GetBytes(32), pdb.GetBytes(16));
}
public bool ThumbnailCallback()
{
return false;
}