[C#从文件解密byte []时出错

问题描述 投票:0回答:1

我编写了这段代码来加密文本,然后将加密的文本写入文件,然后从文件中解密。但是我遇到了一个异常:System.Security.Cryptography.CryptographicException:'输入数据不是完整的块。当我使用byte []存储数据时,它可以完美工作,但似乎无法正确将文件转换为byte []。我也尝试了File.ReadAllBytes,但是遇到了同样的错误。请帮助我。

    class Program
    {
        static void Main(string[] args)
        {
            string decrypted;
            byte[] encrypted;
            Console.Write("Enter a text to encrypt : ");
            string plaintext = Console.ReadLine();
            using (Aes aes = Aes.Create())
            {
                encrypted = AesEncryption.Encrypt(plaintext, aes);
                File.WriteAllText(@"C:\Users\sepita\Desktop\My.txt", Encoding.UTF8.GetString(encrypted), Encoding.UTF8);
                decrypted = AesEncryption.Decrypt(Encoding.UTF8.GetBytes(File.ReadAllText(@"C:\Users\sepita\Desktop\My.txt")), aes);
            }
            Console.WriteLine($"Encrypted : {Encoding.UTF8.GetString(encrypted)}");
            Console.WriteLine($"Decrypted : {decrypted}");
        }
    }
    static class AesEncryption
    {
        public static byte[] Encrypt(string plaintext, Aes aes)
        {
            byte[] encrypted;
            ICryptoTransform encryptor = aes.CreateEncryptor();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream stream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        writer.Write(plaintext);
                    }
                    encrypted = memoryStream.ToArray();
                }
            }
            return encrypted;
        }
        public static string Decrypt(byte[] encrypted, Aes aes)
        {
            string decrypted = null;
            ICryptoTransform decryptor = aes.CreateDecryptor();
            using (MemoryStream memoryStream = new MemoryStream(encrypted))
            {
                using (CryptoStream stream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        decrypted = reader.ReadToEnd();
                    }
                }
            }
            return decrypted;
        }
    }
c# security encryption io aes
1个回答
0
投票

Encrypt函数的结果是二进制数据。如果这是有效的UTF8字符串,那就太幸运了,因此Encoding.UTF8.GetString(encrypted)通常不会起作用。

替换为

            File.WriteAllBytes(@"C:\Users\sepita\Desktop\My.bin", encrypted);
            decrypted = AesEncryption.Decrypt(File.ReadAllBytes(@"C:\Users\sepita\Desktop\My.bin"), aes);

将起作用。如果要文本文件,请对二进制数据使用BASE64转换:

            File.WriteAllText(@"C:\Users\sepita\Desktop\My.txt", Convert.ToBase64String(encrypted));
            decrypted = AesEncryption.Decrypt(Convert.FromBase64String(File.ReadAllText(@"C:\Users\sepita\Desktop\My.txt")), aes);
© www.soinside.com 2019 - 2024. All rights reserved.