将加密图像打开为Bitmap C#

时间:2015-12-14 06:31:03

标签: c# encryption bitmap data-hiding

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

以下是我的加密代码。

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

1 个答案:

答案 0 :(得分:2)

我想你要做的就是非常接近这个:

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