c# – 使用Rijndael加密/解密文件

前端之家收集整理的这篇文章主要介绍了c# – 使用Rijndael加密/解密文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要传输xml文件,并且需要加密它们.我发现一些例子认为我很接近,但是当我解密文件时,我最终会遇到尾随垃圾字符.有一些关于这个的帖子,但我没有看到任何将完全有帮助.这是加密和解密代码.
private void EncryptFile(string inputFile,string outputFile,string key) {
    try {
        byte[] keyBytes;
        keyBytes = Encoding.Unicode.GetBytes(key);

        Rfc2898DeriveBytes derivedKey = new Rfc2898DeriveBytes(key,keyBytes);

        RijndaelManaged rijndaelCSP = new RijndaelManaged();
        rijndaelCSP.Key = derivedKey.GetBytes(rijndaelCSP.KeySize / 8);
        rijndaelCSP.IV = derivedKey.GetBytes(rijndaelCSP.BlockSize / 8);

        ICryptoTransform encryptor = rijndaelCSP.CreateEncryptor();

        FileStream inputFileStream = new FileStream(inputFile,FileMode.Open,FileAccess.Read);

        byte[] inputFileData = new byte[(int)inputFileStream.Length];
        inputFileStream.Read(inputFileData,(int)inputFileStream.Length);

        FileStream outputFileStream = new FileStream(outputFile,FileMode.Create,FileAccess.Write);

        CryptoStream encryptStream = new CryptoStream(outputFileStream,encryptor,CryptoStreamMode.Write);
        encryptStream.Write(inputFileData,(int)inputFileStream.Length);
        encryptStream.FlushFinalBlock();

        rijndaelCSP.Clear();
        encryptStream.Close();
        inputFileStream.Close();
        outputFileStream.Close();
    }
    catch (Exception ex) {
        MessageBox.Show(ex.Message,"Encryption Failed!",MessageBoxButtons.OK,MessageBoxIcon.Error);
        return;
    }

    MessageBox.Show("File Encryption Complete!");

}

private void DecryptFile(string inputFile,string key) {
    try {
        byte[] keyBytes = Encoding.Unicode.GetBytes(key);

        Rfc2898DeriveBytes derivedKey = new Rfc2898DeriveBytes(key,keyBytes);

        RijndaelManaged rijndaelCSP = new RijndaelManaged();
        rijndaelCSP.Key = derivedKey.GetBytes(rijndaelCSP.KeySize / 8);
        rijndaelCSP.IV = derivedKey.GetBytes(rijndaelCSP.BlockSize / 8);
        ICryptoTransform decryptor = rijndaelCSP.CreateDecryptor();

        FileStream inputFileStream = new FileStream(inputFile,FileAccess.Read);

        CryptoStream decryptStream = new CryptoStream(inputFileStream,decryptor,CryptoStreamMode.Read);

        byte[] inputFileData = new byte[(int)inputFileStream.Length];
        decryptStream.Read(inputFileData,FileAccess.Write);
        outputFileStream.Write(inputFileData,inputFileData.Length);
        outputFileStream.Flush();

        rijndaelCSP.Clear();

        decryptStream.Close();
        inputFileStream.Close();
        outputFileStream.Close();
    }
    catch (Exception ex) {
        MessageBox.Show(ex.Message,"Decryption Failed!",MessageBoxIcon.Error);
        return;
    }

    MessageBox.Show("File Decryption Complete!");
}

我结束了

<?xml version="1.0" encoding="UTF-8"?>
<transaction>
  <header>
    <qOrderNumber></qOrderNumber>
    <qRequestDate></qRequestDate>
    <testOrder></testOrder>
    <qCustomerNumber></qCustomerNumber>
    <transactionStatus></transactionStatus>
  </header>
  <lines>
    <line>
      <productID></productID>
      <serialNumber></serialNumber>
    </line>
    <line> 
      <productID></productID>
      <serialNumber></serialNumber>
    </line>
  </lines>
</transaction>NULNULNULNULNULNUL

解决方法

解密时,请注意CryptoStream.Read调用的返回值.它告诉您字节数组中解密数据的长度(通常与填充引起的加密数据长度不匹配).尝试在解密函数中使用以下内容
int decrypt_length = decryptStream.Read(inputFileData,(int)inputFileStream.Length);
FileStream outputFileStream = new FileStream(outputFile,FileAccess.Write);
outputFileStream.Write(inputFileData,decrypt_length);
原文链接:https://www.f2er.com/csharp/93514.html

猜你在找的C#相关文章