【发布时间】:2019-06-18 13:26:01
【问题描述】:
我正在尝试使用 BouncyCastle 为 ASP.NET MVC 模拟冷融合 Blowfish/Hex 加密。我能够使用此链接中列出的步骤成功解密在 CF 中加密的字符串:Encrypt and Decrypt data using Blowfish/CBC/PKCS5Padding
除了在 7 个字符的末尾添加 \0 之外,它的效果很好。
我不想使用 Blowfish/CBC/PKCS5Padding,只是使用 Blowfish/Hex。我无法使反向(加密)工作。以下是我到目前为止的代码
我尝试使用手动填充 = 以及忽略。但是,即使使用附加字符,它仍然不同步。
解密(确实有效):
string keyString = "TEST";
BlowfishEngine engine = new BlowfishEngine();
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine);
cipher.Init(false, new KeyParameter(Convert.FromBase64String(keyString)));
byte[] out1 = Hex.Decode(hexstring);
byte[] out2 = new byte[cipher.GetOutputSize(out1.Length)];
int len2 = cipher.ProcessBytes(out1, 0, out1.Length, out2, 0);
cipher.DoFinal(out2, len2);
string myval = Encoding.UTF8.GetString(out2);
加密(不起作用):
string keyString = "TEST";
string stringToEncrypt = "0123456";
stringToEncrypt = stringToEncrypt + "=";
BlowfishEngine engine2 = new BlowfishEngine();
PaddedBufferedBlockCipher cipher2 = new PaddedBufferedBlockCipher(engine2);
cipher2.Init(true, new KeyParameter(Convert.FromBase64String(keyString)));
byte[] inB = Hex.Encode(Convert.FromBase64String(stringToEncrypt));
byte[] outB = new byte[cipher2.GetOutputSize(inB.Length)];
int len1 = cipher2.ProcessBytes(inB, 0, inB.Length, outB, 0);
cipher2.DoFinal(outB, len1);
var myval2 = BitConverter.ToString(outB).Replace("-", "");
“不起作用”的意思 - 它返回一个加密的字符串,它绝不会反映由 CF 加密的字符串。最重要的是,使用上述方法解密时返回的字符串与最初输入的字符串不匹配(使用 .NET/BouncyCastle 加密)
【问题讨论】:
-
您能否详细说明“不起作用”?
-
当然。它返回一个加密的字符串,它绝不会镜像由 CF 加密的字符串。最重要的是,使用上述方法解密时,返回的字符串与最初输入的字符串不匹配(使用 .NET/BouncyCastle 加密)。
标签: c# encryption coldfusion bouncycastle blowfish