将 Coldfusion 加密代码转换为 C#
Converting Coldfusion encryption code to C#
我有一个 Coldfusion 页面,其中包含一段加密变量的代码,如下所示:
<cfset data64 = toBase64(key)>
<cfset encryptedID = encrypt(getUser.ID, data64, "BLOWFISH", "Base64")>
我们正在将站点移动到基于 .NET 的 CMS,我需要将此页面转换为 C#,但我遇到了麻烦。
我已成功将第一行转换为:
byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key);
string keyBase64 = System.Convert.ToBase64String(keyBytes);
我还添加了在 https://defuse.ca/blowfish.htm 找到的 blowfish.cs class,但我不太清楚如何将它与密钥一起使用(以及我是否想使用 ECB、CBC 或 CTR)。我也不确定在 Coldfusion 中使用 base64 编码的模拟是什么......这就是我目前正在尝试的,它不会产生与原始代码相同的结果:
BlowFish b = new BlowFish(keyBase64);
byte[] idBytes = System.Text.Encoding.UTF8.GetBytes(thisUser["ID"].ToString());
byte[] idBytesEncrypted = b.Encrypt_ECB(idBytes);
string idBase64 = System.Convert.ToBase64String(idBytesEncrypted);
我一般没有太多加密经验,Coldfusion 代码是在另一位没有 C# 经验的开发人员的帮助下设置的。任何建议将不胜感激。谢谢!
您可能想试试 BouncyCastle C# API。我 运行 针对 POC 进行了一些测试,它似乎产生了与您的 CF 代码相同的结果。
需要记住的几件事:如果您阅读 Strong Encryption in ColdFusion,它会解释 ColdFusion 默认使用 ECB 模式和 PKCS5Padding。因此,当指定 shorthand Blowfish
时,您实际上是在说使用 Blowfish/ECB/PKCS5Padding
。为了在 C#(或任何语言)中复制加密,您必须使用相同的设置。
C# 端口的文档似乎不多,但据我所知,BlowfishEngine
默认为 ECB 模式。因此,如果您将其包装在 PaddedBufferedBlockCipher
中,结果应该是 PKCS5 填充的。这应该给你与你的 CF 代码相同的结果:
byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(userIDString);
byte[] keyBytes = System.Convert.FromBase64String(keyInBase64);
// initialize for ECB mode and PKCS5/PKCS7 padding
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new BlowfishEngine());
KeyParameter param = new KeyParameter(keyBytes);
cipher.Init(true, param);
// encrypt and encode as base64
byte[] encryptedBytes = cipher.DoFinal(inputBytes);
string idBase64 = System.Convert.ToBase64String(encryptedBytes);
注意: 我不是加密专家,但会说不鼓励使用 "ECB" 模式。参见 wiki for a good illustration of why. So you should seriously consider choosing a different mode。
我有一个 Coldfusion 页面,其中包含一段加密变量的代码,如下所示:
<cfset data64 = toBase64(key)>
<cfset encryptedID = encrypt(getUser.ID, data64, "BLOWFISH", "Base64")>
我们正在将站点移动到基于 .NET 的 CMS,我需要将此页面转换为 C#,但我遇到了麻烦。
我已成功将第一行转换为:
byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key);
string keyBase64 = System.Convert.ToBase64String(keyBytes);
我还添加了在 https://defuse.ca/blowfish.htm 找到的 blowfish.cs class,但我不太清楚如何将它与密钥一起使用(以及我是否想使用 ECB、CBC 或 CTR)。我也不确定在 Coldfusion 中使用 base64 编码的模拟是什么......这就是我目前正在尝试的,它不会产生与原始代码相同的结果:
BlowFish b = new BlowFish(keyBase64);
byte[] idBytes = System.Text.Encoding.UTF8.GetBytes(thisUser["ID"].ToString());
byte[] idBytesEncrypted = b.Encrypt_ECB(idBytes);
string idBase64 = System.Convert.ToBase64String(idBytesEncrypted);
我一般没有太多加密经验,Coldfusion 代码是在另一位没有 C# 经验的开发人员的帮助下设置的。任何建议将不胜感激。谢谢!
您可能想试试 BouncyCastle C# API。我 运行 针对 POC 进行了一些测试,它似乎产生了与您的 CF 代码相同的结果。
需要记住的几件事:如果您阅读 Strong Encryption in ColdFusion,它会解释 ColdFusion 默认使用 ECB 模式和 PKCS5Padding。因此,当指定 shorthand Blowfish
时,您实际上是在说使用 Blowfish/ECB/PKCS5Padding
。为了在 C#(或任何语言)中复制加密,您必须使用相同的设置。
C# 端口的文档似乎不多,但据我所知,BlowfishEngine
默认为 ECB 模式。因此,如果您将其包装在 PaddedBufferedBlockCipher
中,结果应该是 PKCS5 填充的。这应该给你与你的 CF 代码相同的结果:
byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(userIDString);
byte[] keyBytes = System.Convert.FromBase64String(keyInBase64);
// initialize for ECB mode and PKCS5/PKCS7 padding
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new BlowfishEngine());
KeyParameter param = new KeyParameter(keyBytes);
cipher.Init(true, param);
// encrypt and encode as base64
byte[] encryptedBytes = cipher.DoFinal(inputBytes);
string idBase64 = System.Convert.ToBase64String(encryptedBytes);
注意: 我不是加密专家,但会说不鼓励使用 "ECB" 模式。参见 wiki for a good illustration of why. So you should seriously consider choosing a different mode。