0

I am trying to use AesCryptoServiceProvider to achieve the same encryption mechanism as Aes. Here is my AesCryptoServiceProvider version of it:

        public string version1(string plainText, string encryptionKey, string initializationVector)
        {
            AesCryptoServiceProvider provider = new AesCryptoServiceProvider
            {
                BlockSize = 128,
                Padding = PaddingMode.PKCS7,
                Key = Convert.FromBase64String(encryptionKey),
                IV = Encoding.UTF8.GetBytes(initializationVector)
            };

            byte[] buffer = Encoding.ASCII.GetBytes(plainText);
            byte[] encrypted = provider.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length);
            return Convert.ToBase64String(encrypted);
        }

And here is the Aes version of it:

        public string version2(string plainText, string encryptionKey, string initializationVector)
        {
            byte[] clearBytes = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptedBytes;
            byte[] iv = Encoding.UTF8.GetBytes(initializationVector);

            using (Aes aes = Aes.Create())
            {
                aes.BlockSize = 128;
                aes.Padding = PaddingMode.PKCS7;
                aes.Key = Convert.FromBase64String(encryptionKey);
                aes.IV = iv;

                using (MemoryStream ms = new MemoryStream())
                using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                    encryptedBytes = ms.ToArray();
                }
            }

            byte[] ivEncryptedBytes = new byte[iv.Length + encryptedBytes.Length];
            Buffer.BlockCopy(iv, 0, ivEncryptedBytes, 0, iv.Length);
            Buffer.BlockCopy(encryptedBytes, 0, ivEncryptedBytes, iv.Length, encryptedBytes.Length);

            return Convert.ToBase64String(ivEncryptedBytes);
        }

When I encrypt the same string using version1 and version2 they came out to be different. Any idea on how these two methods are different and how I can make version1 produces the same encrypted string as version2? (p.s. I am rather new to encryption so sorry if the answer is obvious) Thanks!

cxc
  • 201
  • 2
  • 10
  • 1
    It could be helpful for us to see a sample output from version1 and version2. As far as I can see the output of version2 is "initializationVector" | "encryptedBytes", means that both are concatenated. This version is mostly done when sending the IV together with the ciphertext to the recipient. Try to decode both results to a hexstring and we will see the difference (you can use https://base64.guru/converter/decode/hex/ as online decoder). – Michael Fehr Jun 09 '20 at 23:16
  • @MichaelFehr That was it! Can't believe I missed that haha now it feels like a stupid question but thanks a lot for the help! – cxc Jun 09 '20 at 23:48
  • My pleasure to help you :-) It would be good for the community if you write an answer with your self-found solution to help others with the same problem. – Michael Fehr Jun 09 '20 at 23:54
  • @MichaelFehr Thanks and will do! One more question - is this way of encryption (concatenating initializationVector | encryptedBytes) common in the encryption world? – cxc Jun 10 '20 at 14:23
  • 1
    For almost all AES encryption algorithms (except ECB mode) you need an initialization vector on encryption side. That IV needs to get communicated to the recipient (decryption side) aside with the encrypted-/cipher-text. In most communication protocols I have seen the IV is concatenated with the ciphertext and then Base64-encoded, so - yes - it's common in the encryption world. – Michael Fehr Jun 10 '20 at 15:27

1 Answers1

0

As @MichaelFehr pointed out, version2 only has the initialization vector and the encrypted bytes concatenated together before converting the bytes back to string. I have tested that if I concatenate the string the same way as version2 in version1, the result string will become the same.

cxc
  • 201
  • 2
  • 10