删除bmp标头进行加密 [英] Remove bmp header to encrypt
问题描述
MyImage = new Bitmap(path);
MemoryStream ms = new MemoryStream();
MyImage.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
byte [] Header = null,picture = null,pictureFull = ms.ToArray(); (int i = 0; i< pictureFull.Length; i ++)
{
if(i <54)
{标题[i] = pictureFull [i]; }
else
{picture [i - 54] = pictureFull [i]; }
}
byte [] demoKeyBytes = new byte [] {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a, 0x0b,0x0c,0x0d,0x0e,0x0f};
PaddingMode padding = PaddingMode.ISO10126;
EncryptedImg = EncryptCall(demoKeyBytes,padding,picture);
问题是,如果我尝试继续加密和解密图片,我得到一个空值。如果我继续使用完整的图像pictureFull进行enc / dec,我也会得到一个空值。我需要完全删除FOR,以使程序能够正常地进行enc / decpictureFull,尽管FOR甚至不修改它。有没有其他方法可以保存byte []数组或出了什么问题?
byte []
是一个对象,所以您不想将该类型的变量初始化为 null
。您可能想要初始化标题
,如下所示:
byte []标题=新字节[54];
和图片
到新字节[]
,其长度至少等于实际图像数据长度,如下所示:
byte [] picture = new byte [pictureFull.Length - 54];
然后你可以复制东西给他们。
I'm pretty new to C# and got handed this AES encryption project on images. There's probably a very simple thing that I'm missing here. I'm trying to remove the header from a bmp and encrypt the rest then add the header back. If I don't remove the header the program works well. To remove, I tried this (bmp header is 54 bytes):
MyImage = new Bitmap("path");
MemoryStream ms = new MemoryStream();
MyImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] Header=null, picture=null, pictureFull = ms.ToArray();
for (int i = 0; i < pictureFull.Length; i++)
{
if (i < 54)
{ Header[i] = pictureFull[i]; }
else
{ picture[i - 54] = pictureFull[i]; }
}
byte[] demoKeyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
PaddingMode padding = PaddingMode.ISO10126;
EncryptedImg = EncryptCall(demoKeyBytes, padding, picture);
The problem is that if i try to go on and encrypt and decrypt "picture" I get a null value. I also get a null value if i go on to enc/dec with the full image "pictureFull". I need to completely remove the FOR in order for the program to enc/dec normally "pictureFull", although that FOR doesn't even modify it. Is there some other way to save byte[] arrays or what's wrong?
byte[]
is an object, so you don't want to initialize a variable of that type to null
. You probably want to initialize Header
like this:
byte[] Header = new byte[54];
and picture
to a new byte[]
with length at least equal to the actual image data length, like this:
byte[] picture = new byte[pictureFull.Length - 54];
Then you can copy things to them.
这篇关于删除bmp标头进行加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!