C# AESScryptoProvider解密异常

C# AESScryptoProvider解密异常,c#,encoding,aes,C#,Encoding,Aes,你好,友好的飞越者: 我在更大的示例中有一行代码不起作用: plaintext = srDecrypt.ReadToEnd(); 它报告了一个异常: 输入数据不是一个完整的块 我有: 我看了一下编码 2个已验证的解密参数是正确的 哦,简单main的意图是从解密值中获取加密值。明文=行位于解密部分 using System; using System.IO; using System.Linq; using System.Security.Cryptography; using System.T

你好,友好的飞越者: 我在更大的示例中有一行代码不起作用:

plaintext = srDecrypt.ReadToEnd();
它报告了一个异常: 输入数据不是一个完整的块

我有: 我看了一下编码 2个已验证的解密参数是正确的

哦,简单main的意图是从解密值中获取加密值。明文=行位于解密部分

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;

        }
    }
}

你有两个错误。第一个是在加密方法中使用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;
    }
}

您忘记了转换来自基64的加密值。这绝对有效!非常感谢!