none
解密的时候出现 “填充无效,无法被移除。” 异常 RRS feed

  • 常规讨论

  • 我根据MSDN上面的提供的示例程序做了一个简单的加密解密程序,发现,有的时候,就是随机会出现经过加密的数据不能够解密,抛出   填充无效,无法被移除。   异常 

    代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;
    
    namespace FailureAnalysis
    {
      public class EncryptHlper
      {
    
        public static void EncryptTextToFile(String Data, String FileName)
        {
          byte[] Key;
          byte[] IV;
          GetKeyAndIV(out Key, out IV);
          try
          {
            // Create or open the specified file.
    
            FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);
    
            // Create a new Rijndael object.
    
            Rijndael RijndaelAlg = Rijndael.Create();
    
            // Create a CryptoStream using the FileStream 
    
            // and the passed key and initialization vector (IV).
    
            CryptoStream cStream = new CryptoStream(fStream,
              RijndaelAlg.CreateEncryptor(Key, IV),
              CryptoStreamMode.Write);
    
            // Create a StreamWriter using the CryptoStream.
    
            StreamWriter sWriter = new StreamWriter(cStream);
    
            try
            {
              // Write the data to the stream 
    
              // to encrypt it.
    
              sWriter.Write(Data);
            }
            catch (Exception e)
            {
              Console.WriteLine("An error occurred: {0}", e.Message);
            }
            finally
            {
              // Close the streams and
    
              // close the file.
    
              sWriter.Close();
              cStream.Close();
              fStream.Close();
            }
          }
          catch (CryptographicException e)
          {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
          }
          catch (UnauthorizedAccessException e)
          {
            Console.WriteLine("A file error occurred: {0}", e.Message);
          }
    
        }
    
        public static string DecryptTextFromFile(String FileName)
        {
          byte[] Key;
          byte[] IV;
          GetKeyAndIV(out Key, out IV);
          try
          {
            // Create or open the specified file. 
    
            FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);
    
            // Create a new Rijndael object.
    
            Rijndael RijndaelAlg = Rijndael.Create();
    
            // Create a CryptoStream using the FileStream 
    
            // and the passed key and initialization vector (IV).
    
            CryptoStream cStream = new CryptoStream(fStream,
              RijndaelAlg.CreateDecryptor(Key, IV),
              CryptoStreamMode.Read);
    
            // Create a StreamReader using the CryptoStream.
    
            StreamReader sReader = new StreamReader(cStream);
    
            string val = null;
    
            try
            {
              // Read the data from the stream 
    
              // to decrypt it.
    
              val = sReader.ReadToEnd();
    
    
            }
            catch (Exception e)
            {
              Console.WriteLine("An error occurred: {0}", e.Message);
            }
            finally
            {
    
              // Close the streams and
    
              // close the file.
    
              sReader.Close();
              cStream.Close();
              fStream.Close();
            }
    
            // Return the string. 
    
            return val;
          }
          catch (CryptographicException e)
          {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
            return null;
          }
          catch (UnauthorizedAccessException e)
          {
            Console.WriteLine("A file error occurred: {0}", e.Message);
            return null;
          }
        }
    
        private static void GetKeyAndIV(out byte[] Key, out byte[] IV)
        {
          Key = new byte[32];
          int i = 0;
          for (i = 0; i < 32; i++)
          {
            Key[i] = (byte)i;
          }
          IV = new byte[16];
          for (i = 0; i < 16; i++)
          {
            IV[i] = (byte)i;
          }
        }
      }
    }
    
    


    • 已更改类型 qianyang 2011年8月29日 1:07
    2011年8月27日 1:49

全部回复