Как расшифровать файл, который был зашифрован с помощью RijndaelManaged - PullRequest
3 голосов
/ 29 апреля 2011

Я могу зашифровать файл изображения. но не могу расшифровать этот файл.

while ((readLen = cryptStrm.Read (bs, 0, bs.Length))> 0)

Кто-нибудь может угадать, какая часть не так? код, который я запрограммировал, следующий:

когда я читаю зашифрованный файл, я вообще не могу читать. и свойство CryptoStream, называемое length и position, имеет "NotSupportedException", когда я вижу свойство cryptstream, используя vss.

Я трачу много часов, чтобы решить эту проблему ..... Пожалуйста, помогите мне .....

Шифрование [Растровое изображение >> зашифрованный тьфу]

Расшифровать [зашифрованный файл >> файл]


Шифрование

    public static void EncryptFile(
        Bitmap bmp, string destFile, byte[] key, byte[] iv)
    {

        System.Security.Cryptography.RijndaelManaged rijndael =
            new System.Security.Cryptography.RijndaelManaged();

        rijndael.Key = key;
        rijndael.IV = iv;

        System.IO.FileStream outFs = new System.IO.FileStream(
            destFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);

        System.Security.Cryptography.ICryptoTransform encryptor =
            rijndael.CreateEncryptor();

        System.Security.Cryptography.CryptoStream cryptStrm =
            new System.Security.Cryptography.CryptoStream(
                outFs, encryptor,
                System.Security.Cryptography.CryptoStreamMode.Write);

        MemoryStream ms = new MemoryStream();
        bmp.Save(ms, ImageFormat.Jpeg);


        byte[] bs = new byte[1024];
        int readLen;
        while ((readLen = ms.Read(bs, 0, bs.Length)) > 0)
        {
            cryptStrm.Write(bs, 0, readLen);
        }

        ms.Close();
        cryptStrm.Close();
        encryptor.Dispose();
        outFs.Close();
    }

Расшифровать

    public static void DecryptFile(
        string sourceFile, string destFile, byte[] key, byte[] iv)
    {

        System.Security.Cryptography.RijndaelManaged rijndael =
            new System.Security.Cryptography.RijndaelManaged();

        rijndael.Key = key;
        rijndael.IV = iv;

        System.IO.FileStream inFs = new System.IO.FileStream(
            sourceFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);

        System.Security.Cryptography.ICryptoTransform decryptor =
            rijndael.CreateDecryptor();

        System.Security.Cryptography.CryptoStream cryptStrm =
            new System.Security.Cryptography.CryptoStream(
                inFs, decryptor,
                System.Security.Cryptography.CryptoStreamMode.Read);

        System.IO.FileStream outFs = new System.IO.FileStream(
            destFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
        byte[] bs = new byte[1024];
        int readLen;

        while ((readLen = cryptStrm.Read(bs, 0, bs.Length)) > 0)
        {
            outFs.Write(bs, 0, readLen);
        }

        outFs.Close();
        cryptStrm.Close();
        decryptor.Dispose();
        inFs.Close();
    }

1 Ответ

0 голосов
/ 30 апреля 2011

Попробуйте установить члены Mode и Padding rijndael.
Режим заполнения по умолчанию вызывал проблемы, когда я делал аналогичную реализацию. // It is reasonable to set encryption mode to Cipher Block Chaining // (CBC). Use default options for other symmetric key parameters. rijndael.Mode = CipherMode.CBC; rijndael.Padding = PaddingMode.None;

...