-1

I was able to put together a encryption sample like below but during decryption i get invalid data(Exception). How am i supposed to decrypt

Encryption Method

public static string EncryptWithAes(string plainText, byte[] key, byte[] initiationVector)
        {

            byte[] cryptoBytes = Encoding.UTF8.GetBytes(plainText);

            using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
            {
                aesAlgorithm.Key = key;
                aesAlgorithm.IV = initiationVector;
                aesAlgorithm.Mode = CipherMode.ECB;
                using (ICryptoTransform encryptoTransform = aesAlgorithm.CreateEncryptor(aesAlgorithm.Key, aesAlgorithm.IV))
                {
                    cryptoBytes = encryptoTransform.TransformFinalBlock(cryptoBytes, 0, cryptoBytes.Length);
                }
            }
            return Convert.ToBase64String(cryptoBytes);
        }

Decryption Method

 public static string DecryptAesCryptoString(string cipherText, byte[] key, byte[] initiationVector)
        {
            byte[] decryptedByte;
            using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
            {
                aesAlgorithm.Key = key;
                aesAlgorithm.IV = initiationVector;
                aesAlgorithm.Mode = CipherMode.ECB;

                using (ICryptoTransform decryptoTransform = aesAlgorithm.CreateDecryptor(aesAlgorithm.Key, aesAlgorithm.IV))
                {
                    byte[] cipherBytes = Convert.FromBase64String(cipherText);
                    decryptedByte = decryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);

                }
            }
            return Encoding.UTF8.GetString(decryptedByte);
        }

i think the problem is with all the encoding that are going inside these methods

Sample Data

plainText = stackoverflow

base64encoded Key = B8Y/6doxwqU870C6jzYWhsr3hKSLokAOkkLCDiy+TS4= (should be easy to convert to bytes ain't it)

base64encoded IV = NZIpD60eBmdsOFFhA2bfvw==

encryptedValue = 77+977+977+977+977+9Ce+/ve+/vQ3vv70F77+9UzHvv73vv70=

I provide same encrypted value , IV and Key to decrypt to Stackoverflow

Deeptechtons
  • 10,945
  • 27
  • 96
  • 178

3 Answers3

1

I think your problem is the length of your IV and maybe the key. The IV should be 16 bytes long as i recall, the key has different options, you should look that up.

//TEST:

        RijndaelManaged alg = new RijndaelManaged();

        alg.GenerateKey();
        alg.GenerateIV();

        byte[] key = alg.Key;
        byte[] iv = alg.IV;

        string text = "teststring";

        string encrypted = EncryptWithAes(text, key, iv);

        MessageBox.Show(encrypted);

        String result = DecryptAesCryptoString(encrypted, key, iv);

        MessageBox.Show(result);
hcb
  • 8,147
  • 1
  • 18
  • 17
  • this is how i am generating the Key so the implementation is wrong?`public static byte[] GenerateAesKey() { byte[] key = null; using (RijndaelManaged aesAlgorithm = new RijndaelManaged()) { aesAlgorithm.GenerateKey(); key = aesAlgorithm.Key; } return key; }` – Deeptechtons Feb 09 '12 at 12:02
  • no, that should work. If you generate the IV the same way that should also work – hcb Feb 09 '12 at 12:06
  • and i am doing it same way. I am sure this has to do something with encodings. Could you try taking your time to put a sample quickly – Deeptechtons Feb 09 '12 at 12:07
  • 1
    Have a look to the source code of the RijndaelManaged class, in particular to the Encrypt method. You're basically reproducing some of the steps already present in the class. Try the default implementation (encrypt/decript),see if it works. If yes start making the changes you require. – Giorgio Minardi Feb 09 '12 at 12:22
1

and sadly this was certainly due to the Encoding problem. Now solved it like below

Encryption

public static string EncryptWithAes(string plainText, byte[] key, byte[] initiationVector)
        {
            byte[] cryptoBytes = Convert.FromBase64String(plainText);
            using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
            {
                aesAlgorithm.Key = key;
                aesAlgorithm.IV = initiationVector;
                aesAlgorithm.Mode = CipherMode.ECB;
                using (ICryptoTransform encryptoTransform = aesAlgorithm.CreateEncryptor(aesAlgorithm.Key, aesAlgorithm.IV))
                {
                    cryptoBytes = encryptoTransform.TransformFinalBlock(cryptoBytes, 0, cryptoBytes.Length);
                }
            }
            return Convert.ToBase64String(cryptoBytes);
        }

Decryption

public static string DecryptAesCryptoString(string cipherText, byte[] key, byte[] initiationVector)
{

    byte[] decryptedByte;
    using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
    {
        aesAlgorithm.Key = key;
        aesAlgorithm.IV = initiationVector;
        aesAlgorithm.Mode = CipherMode.ECB;
        using (ICryptoTransform decryptoTransform = aesAlgorithm.CreateDecryptor(aesAlgorithm.Key, aesAlgorithm.IV))
        {
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            decryptedByte = decryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);

        }
    }
    return Convert.ToBase64String(decryptedByte);
}
Deeptechtons
  • 10,945
  • 27
  • 96
  • 178
0

Why you don't give a try by removing the Encoding ? Here's a simple implementation :

public class RijndaelSimpleTest
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        string   plainText          = "Hello, World!";    // original plaintext

        string   passPhrase         = "Pas5pr@se";        // can be any string
        string   saltValue          = "s@1tValue";        // can be any string
        string   hashAlgorithm      = "SHA1";             // can be "MD5"
        int      passwordIterations = 2;                  // can be any number
        string   initVector         = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
        int      keySize            = 256;                // can be 192 or 128

        Console.WriteLine(String.Format("Plaintext : {0}", plainText));

        string  cipherText = RijndaelSimple.Encrypt(plainText,
                                                    passPhrase,
                                                    saltValue,
                                                    hashAlgorithm,
                                                    passwordIterations,
                                                    initVector,
                                                    keySize);

        Console.WriteLine(String.Format("Encrypted : {0}", cipherText));

        plainText          = RijndaelSimple.Decrypt(cipherText,
                                                    passPhrase,
                                                    saltValue,
                                                    hashAlgorithm,
                                                    passwordIterations,
                                                    initVector,
                                                    keySize);

        Console.WriteLine(String.Format("Decrypted : {0}", plainText));
    }
}
Giorgio Minardi
  • 2,765
  • 1
  • 15
  • 11