问题描述
这里是用于在冷冻中加密的代码
< cfset strBase64Value = encrypt(strValue,24个字符键,AES )/>
正在生成加密值,如 714FEA9A9A2184769CA49D5133F08580
考虑到它只是大写字母和数字,似乎很奇怪。
我应该使用什么C#库来正确解密?
同时查看,似乎默认情况下它使用UUEncode算法进行编码。请问加密器是否使用Base64作为编码参数?
然后他们使用Hex,而不是默认的UUEncode。 hex或base64都可以。只要你们都同意编码,这并不重要。
你可以使用解密字符串。但是,ColdFusion和C#的默认加密设置略有不同。使用加密功能:
- AES是AES / ECB / PKCS5Padding的缩写
- ECB模式不使用IV
- 密钥字符串始终为base64编码
NB:尽管名称不同,对于SUN提供程序, PKCS5Padding
(CF / Java)对应于 PaddingMode.PKCS7
(C#)。 , ...中的SUN提供者表示PKCS#5,其中应使用PKCS#7 - PKCS5Padding应该是PKCS7Padding,这是从只有8个字节的块密码例如(三重)DES对称密码可用。
所以你需要确保你的C#设置被调整为匹配。考虑到这一点,只需解码加密文本和base64的键字符串。使用,只需调整算法设置,以匹配encrypt()函数使用的设置:
使用ColdFusion加密
< cfscript>
plainText =没看到;
// 128位键base64编码
keyInBase64 =Y25Aju8H2P5DR8mY6B0ezg ==;
//AES是AES / ECB / PKCS5Padding的缩写
encryptedText = encrypt(plainText,keyInBase64,AES,hex);
WriteDump(encryptedText);
//结果:8889EDF02F181158AAD902AB86C63951
< / cfscript>
使用C#解密
byte [] bytes = SomeMethodToConvertHexToBytes(encryptedText);
byte [] key = Convert.FromBase64String(keyInBase64);
string decryptptedText = null;
using(RijndaelManaged algorithm = new RijndaelManaged())
{
//初始化设置以匹配CF
中使用的设置algorithm.Mode = CipherMode .ECB;
algorithm.Padding = PaddingMode.PKCS7;
algorithm.BlockSize = 128;
algorithm.KeySize = 128;
algorithm.Key = key;
ICryptoTransform decryptor = algorithm.CreateDecryptor();
using(MemoryStream msDecrypt = new MemoryStream(bytes))
{
using(CryptoStream csDecrypt = new CryptoStream(msDecrypt,decryptor,CryptoStreamMode.Read))
{
using(StreamReader srDecrypt = new StreamReader(csDecrypt))
{
decryptptedText = srDecrypt.ReadToEnd();
}
}
}
}
Console.WriteLine(Encrypted String:{0},encryptedText);
Console.WriteLine(Decrypted String:{0},decrypttextText);记住你可以(可能应该)调整设置,例如使用更安全的 CBC
模式而不是 ECB
。您只需要与CF开发人员协调这些更改。
Here is the code used to encrypt in coldfusion
<cfset strBase64Value = encrypt(strValue,24 character key,AES) />
It is generating encrypted values like 714FEA9A9A2184769CA49D5133F08580
which seems odd to me considering it is only uppercase and numbers.
What C# library should I use to properly decrypt it ?
Also looking at this information, it seems that by default it uses the UUEncode algorithm to encode.
Should I ask the encrypter to use Base64 as encoding parameter ?
解决方案 Then they are using "Hex", not the default "UUEncode". Either "hex" or "base64" is fine. As long as you both agree upon the encoding, it does not really matter.
You can use RijndaelManaged to decrypt the strings. However, the default encryption settings for ColdFusion and C# differ slightly. With the encrypt function:
- "AES" is short for "AES/ECB/PKCS5Padding"
- "ECB" mode does not use an IV
- Key strings are always base64 encoded
NB: Despite the name difference, for the SUN provider, PKCS5Padding
(CF/Java) corresponds to PaddingMode.PKCS7
(C#). As mentioned in this thread, the "... SUN provider in Java indicate[s] PKCS#5 where PKCS#7 should be used - "PKCS5Padding" should have been "PKCS7Padding". This is a legacy from the time that only 8 byte block ciphers such as (triple) DES symmetric cipher were available."
So you need to ensure your C# settings are adjusted to match. With that in mind, just decode the encrypted text from hex and the key string from base64. Using the slightly ugly example in the API, just adjust the algorithm settings to match those used by the encrypt() function:
Encrypt with ColdFusion
<cfscript>
plainText = "Nothing to see";
// 128 bit key base64 encoded
keyInBase64 = "Y25Aju8H2P5DR8mY6B0ezg==";
// "AES" is short for "AES/ECB/PKCS5Padding"
encryptedText = encrypt(plainText, keyInBase64, "AES", "hex");
WriteDump( encryptedText );
// result: 8889EDF02F181158AAD902AB86C63951
</cfscript>
Decrypt with C#
byte[] bytes = SomeMethodToConvertHexToBytes( encryptedText );
byte[] key = Convert.FromBase64String( keyInBase64 );
string decryptedText = null;
using (RijndaelManaged algorithm = new RijndaelManaged())
{
// initialize settings to match those used by CF
algorithm.Mode = CipherMode.ECB;
algorithm.Padding = PaddingMode.PKCS7;
algorithm.BlockSize = 128;
algorithm.KeySize = 128;
algorithm.Key = key;
ICryptoTransform decryptor = algorithm.CreateDecryptor();
using (MemoryStream msDecrypt = new MemoryStream(bytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
decryptedText = srDecrypt.ReadToEnd();
}
}
}
}
Console.WriteLine("Encrypted String: {0}", encryptedText);
Console.WriteLine("Decrypted String: {0}", decryptedText);
Keep in mind you can (and probably should) adjust the settings, such as using the more secure CBC
mode instead of ECB
. You just need to coordinate those changes with the CF developer.
这篇关于在C#中加密ColdFusion和解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!