本文介绍了c#充气城堡河豚解密-垫块损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to decrypt a blowfish encrypted string with Bouncycastle in C#.

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

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.

我可以使用C#/Bouncycastle使用以下命令重新创建相同的字符串,但是我尚未成功解密它.

I AM able to recreate that same string with C# / Bouncycastle using the following but I have yet to decrypt it successfully.

    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);

Below is what I have for decryption at the moment. The line that fails with error "pad block corrupted" is 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()中可能做错了什么吗?

Any idea what I might be doing wrong in BlowfishDecrypt()?

注意:我从在某处找到的一个Bouncycastle Java示例转换了上述内容(加密和解密).加密有效.我唯一看到的区别是Java示例使用了StringBuffer,而我使用了StringBuilder.

Note:I converted the above (encrypt and decrypt) from a bouncycastle Java example I found somewhere; the encrypt works. The only difference I can see is that the Java example uses a StringBuffer where I use a StringBuilder.

推荐答案

谢谢,Artjom B!

Thank you, Artjom B!

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

应该是

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

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

From there, all I had to do was convert the Hex to a string.

这篇关于c#充气城堡河豚解密-垫块损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 21:27