如何解密使用 RijndaelManaged 加密的文件

发布于 2024-11-04 00:52:13 字数 2774 浏览 0 评论 0原文

我可以加密图像文件。但无法解密该文件。

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

谁能猜出哪一部分是错误的? 我编程的代码如下。

当我读取加密文件时,我根本无法读取。 并且 CryptoStream 的称为长度和位置的属性具有“NotSupportedException”, 当我使用 vss 看到 cryptstream 的属性时。

我浪费了很多时间来解决这个问题...... 请帮助我......

加密 [位图>>加密的fie]

解密 [加密fie>>文件]


加密

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上芳菲 2024-11-11 00:52:13

尝试设置 rijndael 的 Mode 和 Padding 成员。
当我进行类似的实现时,默认的填充模式引起了问题。
<代码>

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

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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文