AES解密文件数据并写入磁盘

时间:2014-02-04 19:42:46

标签: c# encryption openssl aes bouncycastle

我正在尝试解密文件数据并写入磁盘。它正在创建文件,但它已损坏。如果有任何建议,请告诉我。我使用的是AES 256算法。

  column_data =  string_data //base64 encoded string
  byte[] data_bytes = Convert.FromBase64String(column_data);
  string decodedString = Encoding.UTF8.GetString(data_bytes);

  bytes[] decrypted = DecryptAESText(decodedString, secret, iv);

  File.WriteAllBytes(@".\file.gif", decrypted );


    // DecryptAESText returns the decrypted text, receives text in Base64
    public static byte[] DecryptAESText(string cipheredText, byte[] Key, byte[] IV)
    {
        IBufferedCipher cipher = InitAESCipher(false, Key, IV);
        string plainText = string.Empty;
        byte[] plainBytes = null;
        try
        {
            byte[] cipheredBytes = Convert.FromBase64String(cipheredText);
            plainBytes = new byte[cipher.GetOutputSize(cipheredBytes.Length)];
            plainBytes = cipher.DoFinal(cipheredBytes);
        }
        catch (Exception e)
        {
          Console.WriteLine(e.ToString() + "Issue in DecryptAESText column Name:");
        }
        return plainBytes;
    }

0 个答案:

没有答案