[英]c# Bouncy Castle Blowfish Decryption - Pad block corrupted
我正在嘗試使用 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 "";
}
}
以下是我目前用於解密的內容。 因錯誤“填充塊損壞”而失敗的行是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);
從那里,我所要做的就是將十六進制轉換為字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.