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();
}
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.
精彩评论