问题 Convert.ToBase64String / Convert.FromBase64String和Encoding.UTF8.GetBytes / Encoding.UTF8.GetString之间的区别


我目前正在学习.NET中的Symmetric Cryptography。我写了一个演示如下:

    private byte[] key = Encoding.ASCII.GetBytes("abcdefgh");
    private byte[] IV = Encoding.ASCII.GetBytes("hgfedcba");
    private byte[] encrypted;

    public Form1()
    {
        InitializeComponent();

    }

    private void btnEncrypt_Click(object sender, EventArgs e)
    {
        this.textBox2.Text = this.Encrypt(this.textBox1.Text);
    }

    private void btnDecrypt_Click(object sender, EventArgs e)
    {
        this.textBox3.Text = this.Decrypt(this.textBox2.Text);
    }

    private string Encrypt(string plainText)
    {
        try
        {
            using (DESCryptoServiceProvider crypto = new DESCryptoServiceProvider())
            {
                crypto.Key = this.key;
                crypto.IV = this.IV;

                ICryptoTransform transform = crypto.CreateEncryptor(crypto.Key, crypto.IV);

                using (MemoryStream stream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Write))
                    {
                        using (StreamWriter writer = new StreamWriter(cryptoStream))
                        {
                            writer.Write(plainText);
                        }

                        encrypted = stream.ToArray();
                    }
                }
            }

            return Convert.ToBase64String(encrypted);
        }
        catch (Exception)
        {

            throw;
        }
    }

    private string Decrypt(string cipherText)
    {
        try
        {
            string plainText = string.Empty;

            using (DESCryptoServiceProvider crypto = new DESCryptoServiceProvider())
            {
                crypto.Key = this.key;
                crypto.IV = this.IV;

                ICryptoTransform transform = crypto.CreateDecryptor(crypto.Key, crypto.IV);

                using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(cipherText)))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Read))
                    {
                        using (StreamReader reader = new StreamReader(cryptoStream))
                        {
                            plainText = reader.ReadToEnd();
                        }
                    }
                }
            }

            return plainText;
        }
        catch (Exception)
        {

            throw;
        }
    }

一切都按预期工作。但如果我更换

return Convert.ToBase64String(encrypted);

using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(cipherText)))

return Encoding.UTF8.GetString(encrypted);

using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(cipherText)))

我在CryptoStream遇到错误 System.NotSupportedException。诊断代码后,我发现了 Encoding.UTF8.GetBytes(cipherText) 有更多的字节比 encrypted

那么使用之间有什么区别 Convert.From/ToBase64String 和 Encoding.UTF8.GetBytes/GetString) ?


2853
2017-11-08 01:14


起源

你的意思是除了显而易见的:它们是两种截然不同的编码机制? - Sam Axe
实际上,我想了解更多关于编码机制的信息,这些编码机制可以解决它们之间的区别,以及为了选择其中一个而需要考虑的因素。 - Doan Cuong
这不是一个可以在这里快速讨论的主题。我建议您先从Google / Bing搜索每个主题,然后在有关实施细节的具体问题时再回来。每种编码都完全不同。 - Sam Axe


答案:


UTF-8 是一种字符编码。它将Unicode代码点(字符)编码为字节。

Base64编码 是二进制到文本编码。它将字节编码为文本。

在这种情况下你需要后者。

Encoding.UTF8.GetString  解码 一个UTF-8编码的字节数组,如果存在无效的字节序列则是有损的(如果你像密文那样提供随机字节,这很有可能)。


15
2017-11-08 13:01



非常感谢,这就是我正在寻找的答案:) - Doan Cuong