解密文件

时间:2017-02-01 10:34:55

标签: c#

    static SymmetricAlgorithm encryption;
    static string password = "SBC";
    static string salt = "ash";
public Decryption()
    {
        encryption = new RijndaelManaged();
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(salt));
        encryption.Key = key.GetBytes(encryption.KeySize / 8);
        encryption.IV = key.GetBytes(encryption.BlockSize / 8);
        encryption.Padding = PaddingMode.PKCS7;
    }
public void Decrypt(Stream inStream, Stream OutStream)
    {
        ICryptoTransform encryptor = encryption.CreateDecryptor();
        inStream.Position = 0;            
        CryptoStream encryptStream1 = new CryptoStream(OutStream, encryptor, CryptoStreamMode.Write);
        CopyTo(inStream, encryptStream1);
        encryptStream1.FlushFinalBlock();
        encryptStream1.Close();
        inStream.Close();
        OutStream.Close();
    }
public void CopyTo(Stream input, Stream output)
    {
        // This method exists only in .NET 4 and higher

        byte[] buffer = new byte[4 * 1024];
        int bytesRead;

        while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
        {
            output.Write(buffer, 0, bytesRead);
        }
    }
  

在我的windows窗体中加载我只是创建一个线程并调用该函数来解密文件,这是线程函数

Thread objthreadhtml = new Thread(new ThreadStart(JsHtmlDecrypt));
            objthreadhtml.IsBackground = true;
            objthreadhtml.Name = "HtmlJsDecrypt";
            objthreadhtml.Priority = ThreadPriority.Highest;
            objthreadhtml.Start();
  

下面的功能是解密功能

 public static void JsHtmlDecrypt()
    {
string startPathForHtml = Application.LocalUserAppDataPath.Replace("\\OfflineApplication\\OfflineApplication\\1.0.0.0", "").ToString() + "\\Apps\\Html\\";
var directoryPathForHtml = new DirectoryInfo(startPathForHtml);
foreach (FileInfo fileForHtml in directoryPathForHtml.GetFiles())
        {
            FileStream inFsForHtml = fileForHtml.OpenRead();
            FileInfo inforFHtml = new FileInfo(fileForHtml.FullName.Replace(fileForHtml.Extension, ".html"));
            FileStream outFsForHtml = inforFHtml.Create();
            UnZipDecryptionEncryption.Decryption m_decryption1 = new Decryption();
            m_decryption1.Decrypt(inFsForHtml, outFsForHtml);
            inFsForHtml.Close();
            outFsForHtml.Close();
            UnZipDecryptionEncryption.DeleteZipandFiles m_delete1 = new DeleteZipandFiles();
            m_delete1.DeleteFiles(fileForHtml.FullName);          
        }
}
  

这里我的错误填充在行

中无效
encryptStream1.FlushFinalBlock();
  

请帮我一个人如何解决这个问题我被困在其中。

1 个答案:

答案 0 :(得分:0)

您的“解密”功能正在尝试与您想要的相反:加密数据:

CryptoStream encryptStream1 = new CryptoStream(OutStream, encryptor, CryptoStreamMode.Write);

我想,你想要的是解密它(我的代码使用字节数组作为输入/输出,所以你可能想要修改它):

ICryptoTransform decryptor =  encryption.CreateDecryptor();
// byte[] (cipherText) <-- encryted text
MemoryStream memoryStream = new MemoryStream(cipherText); 
//  here is the most important part: CryptoStreamMode.Read
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);

byte[] plainTextBytes = new byte[cipherText.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);

memoryStream.Close();
cryptoStream.Close();

// my text uses UTF8 encoding, so to get the plain text as string:
string result = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);