C# 以位图C的形式打开加密图像#
我需要在加密图像中执行数据隐藏。要执行数据隐藏,我需要位图图像。但我不知道如何将图像保存为位图 下面是我的加密代码C# 以位图C的形式打开加密图像#,c#,encryption,bitmap,data-hiding,C#,Encryption,Bitmap,Data Hiding,我需要在加密图像中执行数据隐藏。要执行数据隐藏,我需要位图图像。但我不知道如何将图像保存为位图 下面是我的加密代码 public void EncryptFile(string source, string destination) { string sKey = "super545"; FileStream fsInput = new FileStream(source, FileMode.Open, FileAccess.Read); FileStream fsEn
public void EncryptFile(string source, string destination)
{
string sKey = "super545";
FileStream fsInput = new FileStream(source, FileMode.Open, FileAccess.Read);
FileStream fsEncrypted = new FileStream(destination, FileMode.Create, FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
byte[] bytearrayinput = new byte[fsInput.Length - 1];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
}
这被称为:
EncryptFile(originalimage, output);
output
是一个字符串变量,具有存储加密图像的路径
如何调用函数来运行加密
我收到错误,当我点击此行时,参数无效:
Bitmap bitmap3 = new Bitmap(output);
我猜你想做的是非常接近这个:
public void EncryptFile(string source, string destination)
{
string sKey = "super545";
FileStream fsInput = new FileStream(source, FileMode.Open, FileAccess.Read);
FileStream fsEncrypted = new FileStream(destination, FileMode.Create, FileAccess.Write);
//Consider to use something else, DES is dead
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//use some key derivation function like pbkdf2 instead
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//should be random, may be fixed ONLY for testing purposes
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
//byte[] bytearrayinput = new byte[fsInput.Length - 1]; // what do you need that big buffer for anyways?
//fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
//cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
byte[] headerBuffer = new byte[54]; // buffer for our bmp header ... without any color tables or masks
//No need for lots of checks in a proof of concept
fsInput.Read(headerBuffer, 0, headerBuffer.Length);
var biCompression = BitConverter.ToInt32(headerBuffer, 30); //get biComp from header
if (biCompression != 0 && biCompression != 3)
{
throw new Exception("Compression is not in the correct format");
}
//The buffer is copied without any encryption
fsEncrypted.Write(headerBuffer, 0, headerBuffer.Length);
//copy the rest and encrypt it ... don't care about color tables and masks for now
//and let's just hope plaintext and ciphertext have the right size
fsInput.CopyTo(cryptostream);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
}
代码似乎正在加密整个位图文件,而不是其中包含的数据。这就是它失败的原因,您已加密了文件格式,但它不再理解它是什么。我已尝试排除标题:-byte[]bytearrayinput=new byte[fsInput.Length-54];仍然无法打开图像。这里有几件事我想指出。。。排除标题意味着:将它们复制到输出文件而不加密它们。。。不只是从输入文件中减去54字节…54字节可能是标题大小,但可能您还希望保留颜色表/颜色掩码不变…当然,整个过程只适用于未压缩的位图,因此您可能需要测试标题字段的双压缩是0还是3。。。如果不是,首先拒绝图像或解压缩它。考虑使用< <代码>使用< <代码>语句>实现<代码> IDISPOSTABLE < /代码>的对象,即<代码>流<代码> s,以确保它们被关闭并正确处理。这也是最好的做法,很好的推广。嘿,非常感谢。它解决了我的问题。代码正在运行:)我将分析您的代码并从中学习。@TheLethalCoder-这是一个PoC,意味着尽可能接近Abby的代码。。。如果我们开始在这里应用编码准则,那么80%的变化将与Abby的问题无关。。。意思是你删掉了有趣的东西-(@DarkSquirrel42有趣的东西(尽管我很喜欢)是StackOverflow上的噪音。我确实知道,但这是一些人代码中的一个主要问题…即内存泄漏。如果你想保持这种状态,我通常会添加一个“要改进”部分或类似内容,以便OP可以看到他们如何改进代码,这只是一个想法,但确实有帮助。此外,我对你的答案投了赞成票:)@DarkSquirrel42有关如何做到这一点的示例,请参见我的答案: