0

我在让 asp.net C# 文件加密/解密过程正常工作时遇到了一些麻烦。我可以上传和加密文件,但无法进行解密。

我得到错误:Exception Details: System.Security.Cryptography.CryptographicException: Bad Data.在解密线上:

byte[] KeyDecrypted = rsa.Decrypt(KeyEncrypted, false);

这是我的加密功能:

   private void EncryptFile(string inFile)
    {
        RijndaelManaged rjndl = new RijndaelManaged();
        rjndl.KeySize = 256;
        rjndl.BlockSize = 256;
        rjndl.Mode = CipherMode.CBC;
        ICryptoTransform transform = rjndl.CreateEncryptor();

        byte[] keyEncrypted = rsa.Encrypt(rjndl.Key, false);

        byte[] LenK = new byte[4];
        byte[] LenIV = new byte[4];

        int lKey = keyEncrypted.Length;
        LenK = BitConverter.GetBytes(lKey);
        int lIV = rjndl.IV.Length;
        LenIV = BitConverter.GetBytes(lIV);

        int startFileName = inFile.LastIndexOf("\\") + 1;
        // Change the file's extension to ".enc"
        string outFile = EncrFolder + inFile.Substring(startFileName, inFile.LastIndexOf(".") - startFileName) + ".enc";

        lblDecryptFileName.Text = outFile;

        using (FileStream outFs = new FileStream(outFile, FileMode.Create))
        {
            outFs.Write(LenK, 0, 4);
            outFs.Write(LenIV, 0, 4);
            outFs.Write(keyEncrypted, 0, lKey);
            outFs.Write(rjndl.IV, 0, lIV);

            using (CryptoStream outStreamEncrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write))
            {
                int count = 0;
                int offset = 0;
                int blockSizeBytes = rjndl.BlockSize / 8;
                byte[] data = new byte[blockSizeBytes];
                int bytesRead = 0;
                using (FileStream inFs = new FileStream(inFile, FileMode.Open))
                {
                    do
                    {
                        count = inFs.Read(data, 0, blockSizeBytes);
                        offset += count;
                        outStreamEncrypted.Write(data, 0, count);
                        bytesRead += blockSizeBytes;
                    }
                    while (count > 0);
                    inFs.Close();
                }
                outStreamEncrypted.FlushFinalBlock();
                outStreamEncrypted.Close();
            }
            outFs.Close();
        }

    }

这是发生错误的解密函数。

   private void DecryptFile(string inFile)
    {

        // Create instance of Rijndael for
        // symetric decryption of the data.
        RijndaelManaged rjndl = new RijndaelManaged();
        rjndl.KeySize = 256;
        rjndl.BlockSize = 256;
        rjndl.Mode = CipherMode.CBC;
        byte[] LenK = new byte[4];
        byte[] LenIV = new byte[4];
        string outFile = DecrFolder + inFile.Substring(0, inFile.LastIndexOf(".")) + ".txt";

        using (FileStream inFs = new FileStream(EncrFolder + inFile, FileMode.Open))
        {

            inFs.Seek(0, SeekOrigin.Begin);
            inFs.Seek(0, SeekOrigin.Begin);
            inFs.Read(LenK, 0, 3);
            inFs.Seek(4, SeekOrigin.Begin);
            inFs.Read(LenIV, 0, 3);

            int lenK = BitConverter.ToInt32(LenK, 0);
            int lenIV = BitConverter.ToInt32(LenIV, 0);
            int startC = lenK + lenIV + 8;
            int lenC = (int)inFs.Length - startC;

            // Create the byte arrays for
            // the encrypted Rijndael key,
            // the IV, and the cipher text.
            byte[] KeyEncrypted = new byte[lenK];
            byte[] IV = new byte[lenIV];

            // Extract the key and IV
            // starting from index 8
            // after the length values.
            inFs.Seek(8, SeekOrigin.Begin);
            inFs.Read(KeyEncrypted, 0, lenK);
            inFs.Seek(8 + lenK, SeekOrigin.Begin);
            inFs.Read(IV, 0, lenIV);
            Directory.CreateDirectory(DecrFolder);

            byte[] KeyDecrypted = rsa.Decrypt(KeyEncrypted, false);

            ICryptoTransform transform = rjndl.CreateDecryptor(KeyDecrypted, IV);

            using (FileStream outFs = new FileStream(outFile, FileMode.Create))
            {

                int count = 0;
                int offset = 0;

                int blockSizeBytes = rjndl.BlockSize / 8;
                byte[] data = new byte[blockSizeBytes];

                inFs.Seek(startC, SeekOrigin.Begin);
                using (CryptoStream outStreamDecrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write))
                {
                    do
                    {
                        count = inFs.Read(data, 0, blockSizeBytes);
                        offset += count;
                        outStreamDecrypted.Write(data, 0, count);

                    }
                    while (count > 0);

                    outStreamDecrypted.FlushFinalBlock();
                    outStreamDecrypted.Close();
                }
                outFs.Close();
            }
            inFs.Close();
        }

    }

对此的任何帮助都会很棒!我不是 RSA 加密专家,并且已经阅读了很多帖子,但仍然无法提出解决方案。

4

1 回答 1

1

我终于想通了。当我在那里尝试时,该代码在桌面应用程序中运行良好。它只是在我试图编写的 asp.net 4 Web 应用程序中不起作用。问题是 RSA 对象没有在会话中持续存在。因此,RSA 对象创建得很好。文件被加密好了。但是当我去解密文件时,RSA 对象并不存在。的错误消息具有System.Security.Cryptography.CryptographicException: Bad Data误导性,因为这不是真正的问题,数据很好。

因此,在创建密钥和 RSA 对象时,我使用了以下内容:

rsa = new RSACryptoServiceProvider(cspp);
Session["rsa"] = rsa;

接下来,当调用解密函数时,我添加了:

if (rsa == null)
     rsa = (RSACryptoServiceProvider)Session["rsa"];

当然,如果没有 RSA 会话的密钥,也有更多的代码,但这是我遇到的问题的高级解决方案。

如果有人在寻找这个,请告诉我,我可以分享更多代码。

于 2012-10-25T12:35:42.410 回答