I copied and used the C# code in the sample from this page, C# equivalent to this ColdFusion Decrypt function but it is not working for me. I'm not sure if the coldfusion decrypt sample on that page is the same as what I have. Anyway, my coldfusion code goes like this:
to encrypt:
<cfset strBase64Value = ToBase64(encrypt(strValue,"mykey")) />
to decrypt:
<cfset strDecrypted = decrypt(ToString(toBinary(strBase64Value)),"mykey") />
Where strValue is the string to begin with, strDecrypted is the returned decrypted string, strBase64Value is the encrypted string and "mykey" is the key password used to encrypt/decrypt strings.
My C# code follows:
private string ConvertString(string string1, string string2)
{
byte[] key = ASCIIEncoding.ASCII.GetBytes(string1);
byte[] encryptedData = Convert.FromBase64String(string2);
Aes aes = Aes.Create();
aes.Mode = CipherMode.ECB;
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, aes.CreateDecryptor(key, null), CryptoStreamMode.Write))
{
cs.Write(encryptedData, 0, encryptedData.Length);
}
byte[] decryptedData = ms.ToArray();
string clearText = Encoding.ASCII.GetString(decryptedData);
return clearText;
}
}
I get an error that says "The specified key is not a valid size for this algorithm. Parameter name: key"