简体   繁体   中英

Decrypting AES string not working as expected

I am trying to decrypt a simple AES string BqvGk+lyQ+pyhSqwV3MfRg== (which translates as Hello World) upon user input with a hardcoded key but I get an error. Could the be an issue when it's trying to read the base64 string from terminal? Not sure how to fix that.

   at EncryptionDecryptionUsingSymmetricKey.AesOperation.DecryptString(String key) in \\visualstudio\AES_Decryptor\AES_Decryptor\Program.cs:line 32
   at EncryptionDecryptionUsingSymmetricKey.AesOperation.Main(String[] args) in \\visualstudio\AES_Decryptor\AES_Decryptor\Program.cs:line 21
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace EncryptionDecryptionUsingSymmetricKey
{
    public class AesOperation
    {
        static void Main(string[] args)
        {
            var key = "b14ca5898a4e4133bbce2ea2315a1916";

            Console.WriteLine("[+] Decrypt: ");
            var str = Console.ReadLine();
           
            var decryptedString = AesOperation.DecryptString(key);
            Console.WriteLine($"[+] Original payload: {decryptedString}");

        }

        private static object DecryptString(string key)
        {
            throw new NotImplementedException();
        }

        public static string DecryptString(string key, string cipherText)
        {
            byte[] iv = new byte[16];
            byte[] buffer = Convert.FromBase64String(cipherText);

            using (Aes aes = Aes.Create())
            {
                aes.Key = Encoding.UTF8.GetBytes(key);
                aes.IV = iv;
                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                using (MemoryStream memoryStream = new MemoryStream(buffer))
                {
                    using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader streamReader = new StreamReader((Stream)cryptoStream))
                        {
                            return streamReader.ReadToEnd();
                        }
                    }
                }
            }
        }
    }
}

This method is working. When I run:

    void Main()
    {
        var key = "b14ca5898a4e4133bbce2ea2315a1916";

        var decryptedString = DecryptString(key, "BqvGk+lyQ+pyhSqwV3MfRg==");
        Console.WriteLine($"[+] Original payload: {decryptedString}");
    }

The result is:

[+] Original payload: Hello World!

I am using your own method as well:

public static string DecryptString(string key, string cipherText)
{
    byte[] iv = new byte[16];
    byte[] buffer = Convert.FromBase64String(cipherText);

    using (Aes aes = Aes.Create())
    {
        aes.Key = Encoding.UTF8.GetBytes(key);
        aes.IV = iv;
        ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

        using (MemoryStream memoryStream = new MemoryStream(buffer))
        {
            using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader streamReader = new StreamReader((Stream)cryptoStream))
                {
                    return streamReader.ReadToEnd();
                }
            }
        }
    }
}

SO I think you need to either provide more of the error message if there is any, or we need more of the code. Cause your key decrypts the AES string just fine with your own provided method.

If I had to guess the issue is 1 of 2 things.

var str = Console.ReadLine();

I don't see you reference str anywhere. also it is going to read the entire console line, which might give you more than the aes string. Also you might just be calling your throw exception method. I am not sure why that is there. Perhaps try getting rid of that and running code. I am referring to this method.

private static object DecryptString(string key)
    {
        throw new NotImplementedException();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM