简体   繁体   English

c# Bouncy Castle Blowfish Decryption - Pad 块损坏

[英]c# Bouncy Castle Blowfish Decryption - Pad block corrupted

I am trying to decrypt a blowfish encrypted string with Bouncycastle in C#.我正在尝试使用 C# 中的 Bouncycastle 解密河豚加密字符串。

I am able to easily encrypt and decrypt my own string but, unfortunately, I have to decrypt a string that is generated by another system.我能够轻松地加密和解密我自己的字符串,但不幸的是,我必须解密由另一个系统生成的字符串。

I AM able to recreate that same string with C# / Bouncycastle using the following but I have yet to decrypt it successfully.我能够使用以下内容使用 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 "";
        }
    }

Below is what I have for decryption at the moment.以下是我目前用于解密的内容。 The line that fails with error "pad block corrupted" is cipher.DoFinal(out2, len2);因错误“填充块损坏”而失败的行是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();

    }

Any idea what I might be doing wrong in BlowfishDecrypt()?知道我在 BlowfishDecrypt() 中可能做错了什么吗?

Note: I converted the above (encrypt and decrypt) from a bouncycastle Java example I found somewhere;注意:我从我在某处找到的 bouncycastle Java 示例转换了上述(加密和解密); the encrypt works.加密有效。 The only difference I can see is that the Java example uses a StringBuffer where I use a StringBuilder.我能看到的唯一区别是 Java 示例使用 StringBuffer,而我使用的是 StringBuilder。

Thank you, Artjom B!谢谢你,Artjom B!

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

Should have been应该是

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

From there, all I had to do was convert the Hex to a string.从那里,我所要做的就是将十六进制转换为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM