【问题标题】:C# getting error on decrypting byte[] from a fileC# 从文件中解密 byte[] 时出错
【发布时间】:2020-06-12 12:57:06
【问题描述】:

我编写了这段代码来加密文本并将加密的文本写入文件,然后从文件中解密。但我得到了这个异常: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;
        }
    }

【问题讨论】:

  • 这样可以解密加密数据吗?
  • 是的。当我使用变量存储加密数据时,我能够成功解密它。但是一旦我将它存储在一个文件中,我就无法解密它。
  • 那你能不能换成默认编码而不是UTF8?
  • 默认编码是什么?
  • Encoding.Default 代替 Encoding.UTF8

标签: c# security encryption io aes


【解决方案1】:

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);

【讨论】:

  • 哪个更好?将加密数据存储为字节数组还是base64string?
  • 定义“更好”。您有什么要求?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-06
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多