打开加密图像作为位图 C#

Open encrypted image as Bitmap C#

我需要在加密图像中执行数据隐藏。要执行数据隐藏,我需要有位图图像。但是我不知道如何将图像保存为位图。

下面是我的加密代码。

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();
}