gpt4 book ai didi

c# Bouncy CaSTLe Blowfish 解密 - 垫 block 已损坏

转载 作者:行者123 更新时间:2023-12-03 19:45:16 33 4
gpt4 key购买 nike

我正在尝试使用 C# 中的 BouncycaSTLe 解密河豚加密字符串。

我能够轻松地加密和解密我自己的字符串,但不幸的是,我必须解密由另一个系统生成的字符串。

我能够使用以下命令使用 C#/BouncycaSTLe 重新创建相同的字符串,但我尚未成功解密它。

    using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;

...

    static readonly Encoding Encoding = Encoding.UTF8;

public string BlowfishEncrypt(string strValue, string key)
{
try
{
BlowfishEngine engine = new BlowfishEngine();

PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine);

KeyParameter keyBytes = new KeyParameter(Encoding.GetBytes(key));

cipher.Init(true, keyBytes);

byte[] inB = Encoding.GetBytes(strValue);

byte[] outB = new byte[cipher.GetOutputSize(inB.Length)];

int len1 = cipher.ProcessBytes(inB, 0, inB.Length, outB, 0);

cipher.DoFinal(outB, len1);

return BitConverter.ToString(outB).Replace("-", "");
}
catch (Exception)
{
return "";
}
}

下面是我目前要解密的内容。因错误“pad block corrupted”而失败的行是 cipher.DoFinal(out2, len2);

    public string BlowfishDecrypt(string name, string keyString)
{


BlowfishEngine engine = new BlowfishEngine();
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine);

StringBuilder result = new StringBuilder();

cipher.Init(false, new KeyParameter(Encoding.GetBytes(keyString)));

byte[] out1 = Convert.FromBase64String(name);
byte[] out2 = new byte[cipher.GetOutputSize(out1.Length)];

int len2 = cipher.ProcessBytes(out1, 0, out1.Length, out2, 0);

cipher.DoFinal(out2, len2); //Pad block corrupted error happens here

String s2 = BitConverter.ToString(out2);

for (int i = 0; i < s2.Length; i++) {
char c = s2[i];
if (c != 0) {
result.Append(c.ToString());
}
}

return result.ToString();

}

知道我在 BlowfishDecrypt() 中可能做错了什么吗?

注意:我从某处找到的 bouncycaSTLe Java 示例转换了上面的内容(加密和解密);加密工作。我能看到的唯一区别是 Java 示例使用 StringBuffer,而我使用 StringBuilder。

最佳答案

谢谢你,Artjom B!

byte[] out1 = Convert.FromBase64String(name);

应该是

byte[] out1 = Hex.Decode(name);

从那里开始,我所要做的就是将十六进制转换为字符串。

关于c# Bouncy CaSTLe Blowfish 解密 - 垫 block 已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096313/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com