AESCryptoProvider解密异常

时间:2017-03-11 19:27:49

标签: c# encoding aes

Hello Friendly OverFlowers: 我在更大的例子中有一行代码不起作用:

plaintext = srDecrypt.ReadToEnd();

报告例外情况: 输入数据不是一个完整的块。

我有: 1)看着编码 2)已验证的Decrypt(args)是正确的。

哦,简单主要的意图是从解密的值中取回加密值。明文=行在解密部分。

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;

        }
    }
}

1 个答案:

答案 0 :(得分: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;
    }
}