DannyDSB Official
DannyDSB Official

Reputation: 17

Encrypting And Decrypting a string using Aes Encryption - C#

I wanna store an encrypted string inside a SQL Database as a byte array and I can't figure out what I'm doing wrong. The code is this:

    private void loginBtn_Click(object sender, EventArgs e)
    {
        try
        {
            string password = passwordBox.Text.ToString();

            using (Aes algorithm = Aes.Create())
            {
                byte[] encryptedPassword = EncryptString(password, algorithm.Key, algorithm.IV);

                string roundTrip = DecryptString(encryptedPassword, algorithm.Key, algorithm.IV);

                MessageBox.Show("Encrypted Password: " + encryptedPassword.ToString() + '\n' + "Round Trip: " + roundTrip.ToString());
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

And the code used for the 'EncryptString' and 'DecryptString' is the one from Microsoft's Aes Class Reference (the Example situated at the end of the page).

I executed my code and all it gives me in a Message Box is this:

Encrypted Password: System.Byte[]

Round Trip: (empty space)

    static byte[] EncryptString(string str, byte[] key, byte[] IV)
    {
        if (str == null || str.Length <= 0)
            throw new ArgumentNullException("string");
        if (key == null || key.Length <= 0)
            throw new ArgumentNullException("key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");

        byte[] encrypted;

        using (Aes algorithm = Aes.Create())
        {
            algorithm.Key = key;
            algorithm.IV = IV;

            ICryptoTransform encryptor = algorithm.CreateEncryptor(algorithm.Key, algorithm.IV);

            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(str);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }

        return encrypted;
    }

    static string DecryptString(byte[] cipher, byte[] key, byte[] IV)
    {
        if (cipher == null || cipher.Length <= 0)
            throw new ArgumentNullException("cipher");
        if (key == null || key.Length <= 0)
            throw new ArgumentNullException("key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");

        string decrypted;

        using (Aes algorithm = Aes.Create())
        {
            algorithm.Key = key;
            algorithm.IV = IV;

            ICryptoTransform decryptor = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV);

            using (MemoryStream msDecrypt = new MemoryStream())
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        decrypted = srDecrypt.ReadToEnd();
                    }
                }
            }
        }

        return decrypted;
    }

Can someone help me fix it, please?

P.S. The Text Box has the Password Char setted to *

Upvotes: 0

Views: 1212

Answers (1)

Ňuf
Ňuf

Reputation: 6217

In DecryptString method, you forgot to pass cipher parameter to constructor of msDecrypt memory stream as an input, thus method actually deciphers empty input stream, so result is empty too.

Line

using (MemoryStream msDecrypt = new MemoryStream())

should actually be:

using (MemoryStream msDecrypt = new MemoryStream(cipher))

and then everything works fine.

Upvotes: 1

Related Questions