16

I currently am learning Symmetric Cryptography in .NET. I wrote a demo as following:

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

Everything work as expected. But if I replace

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

I got an error at CryptoStream System.NotSupportedException. After diagnostic the code, I found that Encoding.UTF8.GetBytes(cipherText) have more bytes than encrypted

So what is the difference between using Convert.From/ToBase64String and Encoding.UTF8.GetBytes/GetString) ?

3
  • You mean besides the obvious: They are two very different encoding mechanisms?
    – Sam Axe
    Commented Nov 8, 2013 at 1:36
  • 1
    Actually, I want to know more about the encoding mechanism that make the difference between them and what should I have to consider in order to choose one over the other.
    – Doan Cuong
    Commented Nov 8, 2013 at 1:57
  • That's not really a topic that can be covered quickly here. I would suggest you start by searching Google/Bing for each topic and then come back when you have specific questions regarding the implementation details. Each encoding is so radically different.
    – Sam Axe
    Commented Nov 8, 2013 at 2:40

1 Answer 1

27

UTF-8 is a character encoding. It encodes Unicode codepoints (characters) into bytes.

Base64 is a binary-to-text encoding. It encodes bytes to text.

You need the latter in this case.

Encoding.UTF8.GetString decodes a UTF-8-encoded byte array, which is lossy if there are invalid byte sequences (which is highly likely if you feed it random bytes like a ciphertext).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.