0

I have test input string (key=value&key=value...) and key (like this D2335A9CA4924D9F914D2B47B450D436) I need to Encrypt my string using AES encryption but my result is not equal to provider's result. Result example : AD0C66FB3C1204A8B0AC68AA9B9E3029C86DFF5872753F2F8D7B68EA667D8616215C20F831ABD5A4D56F286E471651AE5C15BCEB2F368200B4D9F3F6D2F0791E8F45D45FD .................... What I'm doing wrong? (googlized code)

public static string Encrypt(string toEncrypt)
        {
            byte[] keyArray = ASCIIEncoding.ASCII.GetBytes("D2335A9CA4924D9F914D2B47B450D436");
            byte[] toEncryptArray = ASCIIEncoding.ASCII.GetBytes(toEncrypt);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.KeySize = 128;
            rDel.BlockSize = 256;
            rDel.IV = keyArray;
            rDel.Mode = CipherMode.CFB; 
            rDel.Padding = PaddingMode.PKCS7; 
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return FormatByteArray(resultArray);
        }

        internal static string FormatByteArray(byte[] b)
        {
            System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
            int i = 0;
            for (i = 0; i < b.Length; i++)
            {
                if (i != 0 && i % 16 == 0)
                    sb1.Append("\n");
                sb1.Append(System.String.Format("{0:X2} ", b[i]));
            }
            return sb1.ToString();
        }
Ars
  • 193
  • 1
  • 4
  • 15

1 Answers1

3

What I'm doing wrong?

byte[] keyArray = ASCIIEncoding.ASCII.GetBytes("D2335A9CA4924D9F914D2B47B450D436");

This can't be right. It'll give you 64 bytes, half of them 0x00.

It looks like it's Hex encoded, I think you'll need a little loop to decode it.
That would give you 16 bytes (128 bits).


Old answer:

You need to decode this string, it probably is Base64 encoded. Try:

byte[] keyArray = System.Convert.FromBase64String("D2335A9CA4924D9F914D2B47B450D436")

And check if you have a valid (length) key.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    Maybe hex, not base64? If so, see http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa-in-c – Jeff Ogata Oct 13 '11 at 17:41
  • 1
    @adrift, Yes, I was already editing. And that link is helpful too. – H H Oct 13 '11 at 17:44
  • thanks guys. I've done this but the result anyway is not correct. And any building this code brings new result. I guess some parameters are dynamic. – Ars Oct 13 '11 at 18:09
  • 1
    @ars - nothing should be dynamic, you just have to work accurately, and you need to _know for sure_ what the other side uses: ASCII, 128, 256, IV == key, CFB, PKCS7. Get 1 wrong and you have a different result. – H H Oct 13 '11 at 18:20
  • @Henk - I've did what you said. End the result is become better. But anyway my result string shorter. It's strange for me because winmerge shows that my result is sub string of correct result. I'm using the same key and input string with provider. – Ars Oct 13 '11 at 20:49
  • 1
    The result should be a multiple of BlockSize. Could be that the other string contains other/more padding. The real test would be to write the Decrypt() mirror function and see if you get your original string back. – H H Oct 13 '11 at 21:03
  • Thanks Hank Your advices was helpful. – Ars Oct 14 '11 at 05:01