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

2024-01-31

我目前正在学习 .NET 中的对称密码学。我写了一个demo如下:

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

And

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

To

return Encoding.UTF8.GetString(encrypted);

And

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

我在 CryptoStream 中遇到错误System.NotSupportedException。诊断代码后,我发现Encoding.UTF8.GetBytes(cipherText)字节数多于encrypted

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


UTF-8 http://en.wikipedia.org/wiki/UTF-8是一种字符编码。它将 Unicode 代码点(字符)编码为字节。

Base64 http://en.wikipedia.org/wiki/Base64是二进制到文本的编码。它将字节编码为文本。

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

Encoding.UTF8.GetString decodes一个 UTF-8 编码的字节数组,如果存在无效的字节序列,则该数组是有损的(如果您向其提供像密文这样的随机字节,则很可能出现这种情况)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Convert.ToBase64String/Convert.FromBase64String 和 Encoding.UTF8.GetBytes/Encoding.UTF8.GetString 之间的区别 的相关文章

随机推荐