vidyasagar2012
vidyasagar2012

Reputation: 25

Encryption Decryption in windows phone8 c#

I am trying to encrypt my data using the AES algorith.I got the functions from the MSDN site encryption decryption. What i am doing is i'm encrypting the data and storing it as a string using the following method
byte[] encrypted = EncryptStringToBytes_Aes(response, myAes.Key, myAes.IV); string saveresponse = Convert.ToBase64String(encrypted);

and then i save it in IsolatedStorageSettings

settings.Add(merchantId, saveresponse);

But the problem i am facing is when the user comes after sometime and hits my page i check first in the IsolatedStorageSettings object if the data is present i pass that data to decrypt and process further.The step i use to decrypt is as follows

byte[] temp = Convert.FromBase64String(response);


response = DecryptStringFromBytes_Aes(temp, myAes.Key, myAes.IV);

But the above line gives me error "Value can not be null. Parameter name: inputBuffer"

I am unable to find where i am going wrong.Can u guys please let me know what steps should be taken to make it up and running.

Here is the Encryption Code

 static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments. 
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");
        byte[] encrypted;
        // Create an AesManaged object 
        // with the specified key and IV. 
        using (AesManaged aesAlg = new AesManaged())
        {
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption. 
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }


  return encrypted;
}

and here is the decryption code

    static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        try
        {
            // Check arguments. 
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");

            // Declare the string used to hold 
            // the decrypted text. 
            string plaintext = null;

            // Create an AesManaged object 
            // with the specified key and IV. 
            using (AesManaged aesAlg = new AesManaged())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {

                            // Read the decrypted bytes from the decrypting stream 
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }
            return plaintext;

        }
        catch (Exception ex) {
            return "Error";
        }
    }

and on button1 click i call the encryption method

using (AesManaged myAes = new AesManaged())
                                    {

byte[] encrypted = EncryptStringToBytes_Aes(response, myAes.Key, myAes.IV);
string saveresponse = Convert.ToBase64String(encrypted);
}

and on button 2 i call decryption method

using (AesManaged myAes = new AesManaged())
                                        {

    byte[] temp= Convert.FromBase64String(response)
    response = DecryptStringFromBytes_Aes(temp, myAes.Key, myAes.IV);
    }

Upvotes: 0

Views: 169

Answers (1)

vidyasagar2012
vidyasagar2012

Reputation: 25

The problem was the using (AesManaged myAes = new AesManaged()){} block what it does it generates the new key and IV for encryption and decryption.So while decryption the key and IV doesnt match hence the error generates.Just remove the using block and declare the Aesmanaged myAes object at global level and the problem is solved. So the final code would look like

AesManaged myAes = new AesManaged();

On button one click to encrypt;

byte[] encrypted = EncryptStringToBytes_Aes(response, myAes.Key, myAes.IV); string saveresponse = Convert.ToBase64String(encrypted);

and button2 click to decrypt

byte[] temp = Convert.FromBase64String(response);


response = DecryptStringFromBytes_Aes(temp, myAes.Key, myAes.IV);

Thats it, happpy coding.

Upvotes: 1

Related Questions