2017-03-11 45 views
0

你好友好OverFlowers: 我在更大的例子的代码行不工作:AESCryptoProvider解密异常

plaintext = srDecrypt.ReadToEnd(); 

它报告一个异常: 输入数据不是一个完整的块。

我有: 1)看看编码 2)验证解密(参数)是正确的。

哦,简单的主要目的是从解密值中取回加密值。明文=行位于解密部分。

using System; 
using System.IO; 
using System.Linq; 
using System.Security.Cryptography; 
using System.Text; 

namespace Encryptor 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      CryptDecrypt cd = new CryptDecrypt(new Guid()); 
      string s = cd.Encrypt("Password"); 
      Console.WriteLine(s); 
      string t = cd.Decrypt(s); 
      Console.WriteLine(t); 
      Console.ReadKey(); 
     } 
    } 
    public class CryptDecrypt 
    { 
     private byte[] Key; 
     private byte[] IV; 
     public CryptDecrypt(Guid keyBase) 
     { 
      string Hash = keyBase.ToString();    
      Key = Encoding.UTF8.GetBytes(Hash.Take(32).ToArray()); 
      IV = Encoding.UTF8.GetBytes(Hash.Reverse().Take(16).ToArray()); 
     } 


     public string Encrypt(string plainText) 
     { 

      byte[] encrypted; 
      // Create an Aes object 
      // with the specified key and IV. 
      using (Aes aesAlg = Aes.Create()) 
      { 
       aesAlg.IV = IV; 
       aesAlg.Key = IV; 
       aesAlg.Padding = PaddingMode.Zeros; 
       // Create a decrytor to perform the stream transform. 
       ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); 

       // Create the streams used for encryption. 
       using (MemoryStream msEncrypt = new MemoryStream()) 
       { 
        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) 
        { 
         using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) 
         { 
          //Write all data to the stream. 
          swEncrypt.Write(plainText); 
         } 
         encrypted = msEncrypt.ToArray(); 
        } 
       } 
      } 

      return Convert.ToBase64String(encrypted); 
     } 

     public string Decrypt(string inputStr) 
     { 
      // Check arguments. 
      if (inputStr == null || inputStr.Length <= 0) 
       throw new ArgumentNullException("cipherText"); 


      byte[] cipherText = Encoding.UTF8.GetBytes(inputStr); 

      // Declare the string used to hold 
      // the decrypted text. 
      string plaintext = null; 

      // Create an Aes object 
      // with the specified key and IV. 
      using (Aes aesAlg = Aes.Create()) 
      { 
       aesAlg.Key = Key; 
       aesAlg.IV = IV; 
       aesAlg.Padding = PaddingMode.Zeros; 
       // Create a decrytor to perform the stream transform. 
       ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); 

       // Create the streams used for decryption. 
       using (MemoryStream msDecrypt = new MemoryStream(cipherText)) 
       { 
        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) 
        { 
         using (StreamReader srDecrypt = new StreamReader(csDecrypt)) 
         { 

          // Read the decrypted bytes from the decrypting stream 
          // and place them in a string. 
          plaintext = srDecrypt.ReadToEnd(); 
         } 
        } 
       } 
      } 
      return plaintext; 

     } 
    } 
} 
+0

您忘记加密值转换从基地64 – Crowcoder

回答

1

您有两个错误。首先,您在Encrypt方法中使用IV作为密钥,其次您在解密之前忘记从Base64转换回来。

见修订,以纠正这些问题的代码。

void Main() 
{ 
    CryptDecrypt cd = new CryptDecrypt(new Guid()); 
    string s = cd.Encrypt("Password"); 
    Console.WriteLine(s); 
    string t = cd.Decrypt(s); 
    Console.WriteLine(t); 
} 

public class CryptDecrypt 
{ 
    private byte[] Key; 
    private byte[] IV; 
    public CryptDecrypt(Guid keyBase) 
    { 
     string Hash = keyBase.ToString(); 
     Key = Encoding.UTF8.GetBytes(Hash.Take(32).ToArray()); 
     IV = Encoding.UTF8.GetBytes(Hash.Reverse().Take(16).ToArray()); 
    } 


    public string Encrypt(string plainText) 
    { 

     byte[] encrypted; 
     // Create an Aes object 
     // with the specified key and IV. 
     using (Aes aesAlg = Aes.Create()) 
     { 
      aesAlg.IV = IV; 
      aesAlg.Key = Key; <- HERE 
      aesAlg.Padding = PaddingMode.Zeros; 
      // Create a decrytor to perform the stream transform. 
      ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); 

      // Create the streams used for encryption. 
      using (MemoryStream msEncrypt = new MemoryStream()) 
      { 
       using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) 
       { 
        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) 
        { 
         //Write all data to the stream. 
         swEncrypt.Write(plainText); 
         swEncrypt.Flush(); 
        } 
        encrypted = msEncrypt.ToArray(); 
       } 
      } 
     } 

     return Convert.ToBase64String(encrypted); 
    } 

    public string Decrypt(string inputStr) 
    { 
     // Check arguments. 
     if (inputStr == null || inputStr.Length <= 0) 
      throw new ArgumentNullException("cipherText"); 

     byte[] cipherText = Convert.FromBase64String(inputStr); <- HERE 

     // Declare the string used to hold 
     // the decrypted text. 
     string plaintext = null; 

     // Create an Aes object 
     // with the specified key and IV. 
     using (Aes aesAlg = Aes.Create()) 
     { 
      aesAlg.Key = Key; 
      aesAlg.IV = IV; 
      aesAlg.Padding = PaddingMode.Zeros; 
      // Create a decrytor to perform the stream transform. 
      ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); 

      // Create the streams used for decryption. 
      using (MemoryStream msDecrypt = new MemoryStream(cipherText)) 
      { 
       using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) 
       { 
        using (StreamReader srDecrypt = new StreamReader(csDecrypt)) 
        { 
         // Read the decrypted bytes from the decrypting stream 
         // and place them in a string. 
         plaintext = srDecrypt.ReadToEnd(); 
        } 
       } 
      } 
     }  
     return plaintext; 
    } 
} 
+0

这绝对作品!万分感谢! –