0

I have implemented AES Algorithm using this website. https://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged(v=vs.110).aspx

After Encrypting the value is converted in to string and send to Database. which was successful. When i was retrieving the value Encrypted value, that was successful. But when I start decryption, no error were found as well as no output was shown(Getting output using the Message box). what should i do?

byte[] cipherbytes = System.Text.ASCIIEncoding.Default.GetBytes(encypted);
       //AES Decryption start
        try
        {
            using (AesManaged myAes = new AesManaged())
            {
                // Decrypt the bytes to a string.
                string roundtrip = DecryptStringFromBytes_Aes(cipherbytes, myAes.Key, myAes.IV);
                //Console.WriteLine("Round Trip: {0}", roundtrip);
                MessageBox.Show(roundtrip, "Decrypted text"); //this meessage box is not showing
            }

        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e.Message);
            //MessageBox.Show("Inside is working");
        }

//Here is Decryption Algorithm as well

static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        // 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("IV");

        // 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();
                    }
                }
            }

        }
Cleptus
  • 3,446
  • 4
  • 28
  • 34

1 Answers1

0

Your DecryptStringFromBytes_Aes does not return a value, you should add return plaintext; at the end. Does it compile?

The MessageBox is not shown because... It does not get to that line, you are having and exception earlier in the code (in the DecryptStringFromBytes_Aes function). Put a breakpoint in this line Console.WriteLine("Error: {0}", e.Message); and check the error. You could also checl the console for the error written.

Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • return plaintext; was missing unfortunately. I try to find out the issues using above method. I put the break-point as per your suggestion. But nothing happened. when i enable the Messagbox in catch (Exception e), message box pop-up. which shows that exception is occurring. but still unable to find the issue using console error. – Muhammad Faseeh May 03 '17 at 02:51