开发者

How to Decrypt File which was encrypted using RijndaelManaged

I can encrypt image file. but can not decrypt that file.

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

anyone can guess which part is wrong? the code i programmed is the following.

when i read encrypted file, i can not read at all. and the CryptoStream's property called length and position has "NotSupportedException", when i see cryptstream's property using vss.

I waste many hours to solve this problem..... Pls help me.....

Encrypt [Bitmap >> encrypted fie]

Decrypt [encrypted fie >> file]


Encrypt

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

Decrypt

    public static void DecryptFile(
        string sourceFile, string destFile, byte[] key, byte[] iv)
    开发者_StackOverflow社区{

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


Try setting the Mode and Padding members of rijndael.
The default padding mode caused problems when I did a similar implementation.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜