0

I found this article online and implemented a modified version of it.

public static byte[] Encrypt(byte[] input, byte[] iv) { var aes = new AesManaged(); aes.Key = StringToByteArray("abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"); aes.IV = StringToByteArray("00010001000000000000000000000000"); aes.KeySize = 128; var encryptor = aes.CreateEncryptor(); using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { cs.Write(input, 0, input.Length); cs.Close(); } return ms.ToArray(); } } public static byte[] StringToByteArray(string hex) { var NumberChars = hex.Length; var bytes = new byte[NumberChars / 2]; for (var i = 0; i < NumberChars; i += 2) bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); return bytes; } 

Now the question I have is, here I am providing same IV and Key (obviously just for testing, in production I'm changing the IV each time I encrypt), but it's returning different encrypted bytes each time I encrypt the same input.

I looked up some posts and they said the output is supposed to be same for specific key/iv combination. Am I missing something here?


EDIT:

[TestMethod] public void Encryption_returns_same_value_for_same_key_and_iv() { const string input = "my input"; var bytes = Encoding.UTF32.GetBytes(input); var result = EncryptionManager.Encrypt(bytes, bytes); var result2 = EncryptionManager.Encrypt(bytes, bytes); Assert.AreEqual(result, result2); } 

This is how i'm calling the encrypt method

13
  • Can you print aes.Mode and aes.Padding to the console to see what kind of mode of operation the AesManaged() class uses by default? Randomization / Padding might have happened. Commented Jan 27, 2016 at 20:14
  • 2
    Cannot reproduce: ideone.com/teoqkr Commented Jan 27, 2016 at 20:16
  • @ArtjomB. love that ideone thing. i am even more confused now Commented Jan 27, 2016 at 20:21
  • @MaximilianGerhardt its CBC and PKCS7 Commented Jan 27, 2016 at 20:21
  • 1
    If cipher block chaining mode is used with a constant key and IV, and PKCS#7 is used (which uses deterministic padding for messages of fixed size in this case), then the encryption of the same bytes must result in the same ciphertext. No randomization happening here. Show us how you call your Encrypt() function. Commented Jan 27, 2016 at 20:23

1 Answer 1

2

So after some discussion, the problem was actually part of the code which was not shown here. Indeed, the original code above always gave the same results and the Unit test should have passed (with additionally using SequenceEqual on the assertion). However, the aes.KeySize was changed in the code (by some colleagues) after setting the key, like this:

aes.Key = StringToByteArray("abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"); aes.IV = StringToByteArray("00010001000000000000000000000000"); aes.KeySize = 128; 

However, as we found out by outputting the used key after setting the KeySize property using

Console.WriteLine("Used Key for Encryption: " + BitConverter.ToString(aes.Key)); 

the key changes to a random key after you modify the KeySize. That's why we kept getting different results. Sample outputs for calling the function with the same input vector:

Used Key for Encryption: C7-35-58-42-3A-2A-79-DE-0D-09-78-20-34-90-1F-EC
Ciphertext: E4-AA-A3-3B-01-CF-F0-C1-07-9A-0B-73-3E-70-C9-8A

Used Key for Encryption: 8A-95-E7-26-60-F9-CE-66-BA-A4-DE-D2-FA-70-AC-DE
Ciphertext: C5-E7-D3-32-38-21-54-25-86-61-70-CB-94-46-A6-37

Used Key for Encryption: A4-D7-01-8F-35-2B-7F-2D-E6-0A-A9-7F-95-42-71-D6
Ciphertext: F1-B2-75-64-D1-90-75-32-0D-CB-D9-AE-11-AE-DB-DD

The problem is solved by first setting the KeySize and then setting the Key property itself.

Sign up to request clarification or add additional context in comments.

1 Comment

haha just realized i didn't add that keysize=256 part in the original post. thanks again for noticing it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.