問題描述
這里是coldfusion中用來加密的代碼
Here is the code used to encrypt in coldfusion
<cfset strBase64Value = encrypt(strValue,24 character key,AES) />
它正在生成像 714FEA9A9A2184769CA49D5133F08580
這樣的加密值,考慮到它只是大寫和數字,這對我來說似乎很奇怪.
It is generating encrypted values like 714FEA9A9A2184769CA49D5133F08580
which seems odd to me considering it is only uppercase and numbers.
我應該使用什么 C# 庫來正確解密它?
What C# library should I use to properly decrypt it ?
同時查看此信息,貌似默認使用UUEncode算法進行編碼.
Also looking at this information, it seems that by default it uses the UUEncode algorithm to encode.
我應該要求加密器使用 Base64 作為編碼參數嗎?
Should I ask the encrypter to use Base64 as encoding parameter ?
推薦答案
它正在生成像
714FEA9A9A2184769CA49D5133F08580
然后他們使用Hex",而不是默認的UUEncode".hex"或base64"都可以.只要你們都同意編碼,這并不重要.
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.
您可以使用 RijndaelManaged 來解密字符串.但是,ColdFusion 和 C# 的默認加密設置略有不同.使用加密功能:
You can use RijndaelManaged to decrypt the strings. However, the default encryption settings for ColdFusion and C# differ slightly. With the encrypt function:
- AES"是AES/ECB/PKCS5Padding"的縮寫
- ECB"模式不使用 IV
- 密鑰字符串始終采用 base64 編碼
注意: 盡管名稱不同,但對于 SUN 提供程序,PKCS5Padding
(CF/Java) 對應于 PaddingMode.PKCS7
(C#).如本帖所述,"... Java 中的 SUN 提供程序指示 [s] PKCS#5 應該在哪里使用 PKCS#7 - "PKCS5Padding" 應該是 "PKCS7Padding".這是從只有 8 字節塊密碼的時候的遺產例如(三重)DES 對稱密碼可用."
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."
因此,您需要確保調整 C# 設置以匹配.考慮到這一點,只需解碼加密文本 來自 hex 和來自 base64 的密鑰字符串.使用 有點難看API 中的示例,只需調整算法設置以匹配 encrypt() 函數使用的設置:
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:
使用 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>
用 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);
請記住,您可以(并且可能應該)調整設置,例如使用更安全的 CBC
模式 而不是 ECB
.您只需要與 CF 開發人員協調這些更改.
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.
這篇關于在 Coldfusion 中加密并在 C# 中解密的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!