在 Coldfusion 中加密并在 C# 中解密 [英] Encrypt in Coldfusion and decrypt in C#

查看:16
本文介绍了在 Coldfusion 中加密并在 C# 中解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是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# 中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆