HappyCoding
HappyCoding

Reputation: 661

Encrypted String Isn't Returned Decrypted

Steps to reproduce the unexpected result:

  1. Using http://aesencryption.net/ I encrypt the text HappyCoding with yecpPqAJ+PnBMtggWVz42WME3TjhG313OhvBuUJOFtc= as the key and 256-Bit option chosen in the drop-down. I receive Lox/sfjNyXOzP9ZE8Fjj9REcuB+iJ1EXXuNjf2du29c= as a result.
  2. I then run it through the Decrypt function in my code:

    var testAesString = "Lox/sfjNyXOzP9ZE8Fjj9REcuB+iJ1EXXuNjf2du29c=";
    var decryptedString = Decrypt(testAesString, key);
    

    and receive "�ГYC���{R\u0017V��@\u0013�NH�$�|�\u001a)˪n�mp" instead of "HappyCoding"

The code for the Decrypt function is below:

private static string Decrypt(string stringCypher_Text, string stringKey)
        {
            Byte[] Key = Convert.FromBase64String(stringKey);

            Byte[] Cypher_Text = Convert.FromBase64String(stringCypher_Text);

            RijndaelManaged Crypto = null;
            MemoryStream MemStream = null;
            ICryptoTransform Decryptor = null;
            CryptoStream Crypto_Stream = null;
            StreamReader Stream_Read = null;
            string Plain_Text;

            try
            {
                Crypto = new RijndaelManaged();
                Crypto.Padding = PaddingMode.Zeros;
                Crypto.Key = Key;
                Crypto.BlockSize = 256;
                Crypto.Mode = CipherMode.ECB;
                Crypto.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

                Decryptor = Crypto.CreateDecryptor(Crypto.Key, Crypto.IV);
                MemStream = new MemoryStream(Cypher_Text);
                Crypto_Stream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);
                Stream_Read = new StreamReader(Crypto_Stream);
                Plain_Text = Stream_Read.ReadToEnd();
            }
            finally
            {
                if (Crypto != null)
                    Crypto.Clear();

                MemStream.Flush();
                MemStream.Close();
            }
            return Plain_Text;
        }

I am not receiving any errors. I am receiving an unexpected result. I don't know how to approach this in regards to testing it further. My thought is maybe the website I am using to receive the encrypted values in the first place is using different settings etc.

Any direction on how to test and/or resolve is appreciated.

Upvotes: 0

Views: 344

Answers (2)

Mark
Mark

Reputation: 1371

Played around with it a bit more and got the following to work. The site appears to just grab the first 32 bytes of whatever string you use as key.

    public static string DecryptLikeSite(string base64EncodedCipherText, string key)
    {
        using (var alg = new RijndaelManaged())
        {
            alg.BlockSize = 256;
            alg.Key = System.Text.Encoding.ASCII.GetBytes(key).Take(32).ToArray();
            alg.Mode = CipherMode.ECB;
            alg.Padding = PaddingMode.Zeros;
            alg.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            var cipherText = Convert.FromBase64String(base64EncodedCipherText);

            using (ICryptoTransform decryptor = alg.CreateDecryptor())
            {
                using (var ms = new MemoryStream(cipherText))
                {
                    using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        using (var sr = new StreamReader(cs))
                        {
                            return sr.ReadToEnd().Replace("\0", string.Empty);
                        }
                    }
                }
            }
        }
    }

    public static void Test()
    {
        string key = "yecpPqAJ+PnBMtggWVz42WME3TjhG313OhvBuUJOFtc=";
        string expectedPlainText = "HappyCoding";

        string base64EncodedSiteCipherText = "Lox/sfjNyXOzP9ZE8Fjj9REcuB+iJ1EXXuNjf2du29c=";
        string plainText = DecryptLikeSite(base64EncodedSiteCipherText, key);

        bool success = expectedPlainText == plainText;
    }

Upvotes: 2

erickson
erickson

Reputation: 269667

Because the cipher text is two blocks, but the plain text is less than one block, I guess that the first block is an IV.

Try using the first 16 bytes of the cipher text as an IV in CBC mode, and decrypt the next 16 bytes.

Upvotes: 0

Related Questions