Encryption and Decryption Unicode Characters

Recently I had faced one major issue while encryption and decryption  XML  contains UNICODE characters

Example:-

Ä,ß,Ö, etc… are displayed as ? character after applying encryption/decryption

Refer unicode characters at wiki http://en.wikipedia.org/wiki/List_of_Unicode_characters

Problem:-

GetBytes  member function of  Encoding.ASCII was used while UNICODE occupy 16 Bytes memory.

Solution:-

Instead use GetBytes member function of UTF8Encoding  instance to encryption / decryption pain text.

Code:-

public class Encryption
    {
        //Security key for Encryption
        private const string Security_Key = "86MLSF45GE8S6J20AK45Q9KF5O6DDAQ1";
        private const string vec = "F5502320F8429037";

        public string PerformEncryption(string input)
        {
            string output = "";
            Aes aes = new AesCryptoServiceProvider();
            aes.Mode = CipherMode.CBC;
            try
            {
                UTF8Encoding utf8 = new UTF8Encoding();
                byte[] key = Encoding.ASCII.GetBytes(Security_Key);
                //byte[] plaintext = Encoding.ASCII.GetBytes(input);
                byte[] plaintext = utf8.GetBytes(input);
                byte[] IV = Encoding.ASCII.GetBytes(vec);
                aes.Key = key;
                aes.IV = IV;
                ICryptoTransform transform = aes.CreateEncryptor(aes.Key, aes.IV);
                MemoryStream memStreamEncryptedData = new MemoryStream();
                memStreamEncryptedData.Write(plaintext, 0, plaintext.Length);
                CryptoStream encStream = new CryptoStream(memStreamEncryptedData, transform, CryptoStreamMode.Write);
                byte[] ciphertext = memStreamEncryptedData.ToArray();
                output = Convert.ToBase64String(ciphertext);
                encStream.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
            return output;
        }

        public string PerformDecryption(string input)
        {
            string output = "";
            Aes aes = new AesCryptoServiceProvider();
            aes.Mode = CipherMode.CBC;
            try
            {
                UTF8Encoding utf8 = new UTF8Encoding();
                byte[] ciphertext = Convert.FromBase64String(input);
                byte[] key = Encoding.ASCII.GetBytes(Security_Key);
                byte[] IV = Encoding.ASCII.GetBytes(vec);
                aes.Key = key;
                aes.IV = IV;
                ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);
                MemoryStream memDecryptStream = new MemoryStream();
                memDecryptStream.Write(ciphertext, 0, ciphertext.Length);
                CryptoStream cs_decrypt = new CryptoStream(memDecryptStream, transform, CryptoStreamMode.Write);

                byte[] plaintext = memDecryptStream.ToArray();
                //output = Encoding.ASCII.GetString(plaintext);
                output = utf8.GetString(plaintext);

            }
            catch (Exception e)
            {
                throw e;
            }
            return output;
        }

    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s