2013-10-31 36 views
0

嗨,大家好,请帮我出这一点,我不断收到此错误:长度数据的解密是无效的

Length of the data to decrypt is invalid.

我在做什么错?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Security.Cryptography; 
using System.IO; 

namespace inChargeAES 
{ 
    public class inChargeCrypto : IinChargeAES 
    { 
     public inChargeCrypto() {} 

     public String inChargeEncrypt(String plaintext, byte[] encryptionKey, byte[] initializationVector) 
     { 
      if (plaintext == null || plaintext.Length <= 0) 
      { 
       throw new ArgumentNullException("plaintext"); 
      } 
      if(encryptionKey == null || encryptionKey.Length <= 0){ 
       throw new ArgumentNullException("encryptionKey"); 
      } 
      if(initializationVector == null || initializationVector.Length <= 0){ 
       throw new ArgumentNullException("initializationVector"); 
      } 
      byte[] encryptedText; 
      using(RijndaelManaged rjManage = new RijndaelManaged()) 
      { 
       rjManage.Key = encryptionKey; 
       rjManage.IV = initializationVector; 
       rjManage.Mode = CipherMode.CBC; 
       //rjManage.Padding = PaddingMode.None; 

       ICryptoTransform iTransformer = rjManage.CreateEncryptor(rjManage.Key, rjManage.IV); 
       using(MemoryStream memStream = new MemoryStream()) 
       { 
        using(CryptoStream cEncryptStream = new CryptoStream(memStream, iTransformer, CryptoStreamMode.Write)) 
        { 
         using(StreamWriter encryptStreamWriter = new StreamWriter(cEncryptStream)) 
         { 
          encryptStreamWriter.Write(plaintext); 
         } 
         encryptedText = memStream.ToArray(); 
        } 
       } 
      } 
      return Convert.ToBase64String(encryptedText); 
     } 

     public String inChargeDecrypt(byte[] cipher, byte[] encryptionKey, byte[] initializationVector) 
     { 
      if (cipher == null || cipher.Length <= 0){ 
       throw new ArgumentNullException("cipher"); 
      } 
      if (encryptionKey == null || encryptionKey.Length <= 0){ 
       throw new ArgumentNullException("encryptionKey"); 
      } 
      if (initializationVector == null || initializationVector.Length <= 0){ 
       throw new ArgumentNullException("initializationVector"); 
      } 

      String decryptedText = null; 
      using (RijndaelManaged rijManage = new RijndaelManaged()) 
      { 
       rijManage.Key = encryptionKey; 
       rijManage.IV = initializationVector; 
       rijManage.Mode = CipherMode.CBC; 
       rijManage.Padding = PaddingMode.None; 

       ICryptoTransform iTranformation = rijManage.CreateDecryptor(rijManage.Key, rijManage.IV); 
       using(MemoryStream memStream = new MemoryStream(cipher)) 
       { 
        using(CryptoStream cDecryptorStream = new CryptoStream(memStream, iTranformation, CryptoStreamMode.Read)) 
        { 
         using (StreamReader decryptReader = new StreamReader(cDecryptorStream)) 
         { 
          decryptedText = decryptReader.ReadToEnd(); //Exception Is Thrown 
         } 
         //memStream.Read(cipher, 0, cipher.Length); 
        } 
       } 
      } 
      return decryptedText; 
     } 
    } 
} 
+0

当你加密和解密时,填充需要是相同的值。 –

+0

你的方法适用于我没有填充加密或解密 – Kevin

回答

0

您已经注释过加密函数的填充模式。

当我在解密函数注释它一切按预期工作。

假设你没有犯过错,将base64字符串转换回byte []

+0

我不明白你的意思是“假设你没有犯过一个错误转换你的base64字符串回到字节[]” –

+0

你的加密方法返回一个字符串使用'return Convert.ToBase64String(encryptedText);' 但是你的解密方法需要一个'byte []'数组作为输入。你如何将加密的字符串转换回'byte []' –

相关问题