C# BouncyCastle河豚加密问题

C# BouncyCastle河豚加密问题,c#,cryptography,bouncycastle,blowfish,C#,Cryptography,Bouncycastle,Blowfish,这是我的BlowFishCrypto课程 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Modes; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parame

这是我的BlowFishCrypto课程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
namespace Common.Encryption
{
    public class BlowfishCryptographer
    {
        private bool forEncryption;
        private IBufferedCipher cipher;

        public BlowfishCryptographer(bool forEncryption)
        {
            this.forEncryption = forEncryption;
            cipher = new BufferedBlockCipher(new CfbBlockCipher(new BlowfishEngine(), 64));
            cipher.Init(forEncryption, new ParametersWithIV(new KeyParameter(Encoding.ASCII.GetBytes("DR654dt34trg4UI6")), new byte[8]));
        }
        public void ReInit(byte[] IV,BigInteger pubkey)
        {
            cipher.Init(forEncryption, new ParametersWithIV(new KeyParameter(pubkey.ToByteArrayUnsigned()),IV));
        }
        public byte[] DoFinal()
        {
            return cipher.DoFinal();
        }
        public byte[] DoFinal(byte[] buffer)
        {
           return cipher.DoFinal(buffer);
        }
        public byte[] DoFinal(byte[] buffer, int startIndex, int len)
        {
            return cipher.DoFinal(buffer, startIndex, len);
        }
        public byte[] ProcessBytes(byte[] buffer)
        {
            return cipher.ProcessBytes(buffer);
        }
        public byte[] ProcessBytes(byte[] buffer, int startIndex, int len)
        {
            return cipher.ProcessBytes(buffer, startIndex, len);
        }
        public void   Reset()
        {
            cipher.Reset();
        }
    }
}
在我的另一节课上,我试着测试它,所以我就这样做了

 BlowfishCryptographer incomingCipher=new BlowfishCryptographer(true);
 BlowfishCryptographer outgoingCipher=new BlowfishCryptographer(false);

byte[] buffer = new byte[] { 0x83, 0x00, 0xEE, 0x03, 0x26, 0x6D, 0x14, 0x00, 0xF1, 0x65, 0x27, 0x00, 0x19, 0x02, 0xD8, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xDB, 0xD7, 0x0F, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2B, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x41, 0x00, 0x64, 0x00, 0xE4, 0x00, 0x00, 0x00, 0xDD, 0x0A, 0x18, 0x19, 0x00, 0x00, 0x79, 0x91, 0x87, 0x00, 0x00, 0x01, 0x00, 0xA8, 0x02, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x34, 0x00, 0x6A, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0A, 0x7E, 0x42, 0x6C, 0x75, 0x65, 0x57, 0x61, 0x76, 0x65, 0x7E, 0x00, 0x09, 0x42, 0x6C, 0x61, 0x63, 0x6B, 0x44, 0x75, 0x73, 0x74 };

Console.WriteLine("\n\nBuffer\n" + outPutHex.ToHex(buffer));
            Console.WriteLine("\n\nBuffer enc\n" + outPutHex.ToHex(outgoingCipher.DoFinal(buffer)));
            Console.WriteLine("\n\nBuffer dec\n" + outPutHex.ToHex(incomingCipher.DoFinal(buffer)));
outputhex func将输出十六进制结果

public static String ToHex(byte[] buf)
        {
            var builder = new StringBuilder();
            foreach (var b in buf) builder.Append(b.ToString("X2")+ " ");
            return builder.ToString();
        } 
因此,结果是:

Buffer
83 00 EE 03 26 6D 14 00 F1 65 27 00 19 02 D8 0F 00 00 00 00 00 00 DB D7 0F 08 00
 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 2B 04 00 00 00 00 00 00 07 00 41 0
0 64 00 E4 00 00 00 DD 0A 18 19 00 00 79 91 87 00 00 01 00 A8 02 00 00 64 00 00
00 34 00 6A 18 00 00 00 00 00 00 C2 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 00 03 0A 7E 42 6C 75 65 57 61 76 65 7E 00 09 42 6C 61 63 6B 44 75 73 74


Buffer enc
EB 28 65 06 EF B5 B9 3E 01 2F D0 B4 C2 25 4C 9C E2 05 D8 B5 93 AC F9 0F 92 87 8B
 5D 1E 45 F6 59 F8 FE 57 A8 0D CF 6C 6B E8 8D F9 88 A6 1D 6D 05 CC B8 6A 9F B0 8
D 13 70 AB F6 3F F8 DD EA ED 16 C3 DB A6 77 B2 46 29 0B DA F4 E2 FF A4 BA 6F C0
06 28 71 57 08 C8 EC 0F 65 54 13 46 C1 23 08 A5 28 C9 9F 9F 1D AD F9 66 09 A7 3B
 E3 22 64 A3 A0 8C 90 BC 1A 99 F1 4F F6 73 49 32 10 78 7D CF FF 68 01 75


Buffer dec
EB 28 65 06 EF B5 B9 3E B7 BE A2 2A 3D 92 5F D5 CF E3 D5 09 C0 5B 9D AD 01 D6 E4
 6D 73 3A 66 59 A9 83 10 11 80 FE 31 48 68 28 A0 01 C9 D8 AD 3E 38 B7 42 4B E5 E
5 56 44 99 91 E8 72 F0 C9 2B AF 83 8C 35 33 6E 08 CA 1E F0 3F 59 E8 64 8D A6 1C
CE 6E FF DC D6 3A FC D0 80 5B 36 81 06 FA 4E 0F 0B FA 54 CA C0 AD 32 52 68 28 8B
 05 CA D2 D3 7C 90 48 93 71 99 CE 28 0B 38 F2 8E 93 74 2F B1 67 9E 68 3F

它不工作,但为什么?

我想我发现了问题:

Console.WriteLine("\n\nBuffer\n" + outPutHex.ToHex(buffer));
Console.WriteLine("\n\nBuffer enc\n" +
    outPutHex.ToHex(outgoingCipher.DoFinal(buffer)));
Console.WriteLine("\n\nBuffer dec\n" +
    outPutHex.ToHex(incomingCipher.DoFinal(buffer)));
您正在创建两个密码上下文
incomingCipher
outgoingCipher
。您可以在同一个输入
缓冲区
上运行它们。如果您要对照检查,这可能很有用,但两者肯定不一样

尝试:


您的
outPutHex.ToHex()函数在哪里?第一个输出看起来与测试类中设置的缓冲区完全不同:
0x83 0x00 0xEE 0x03
变为
73 00 EC 03
。由于第一个输出在前四个字节中有50%的错误,我甚至不想查看
enc
dec
输出。(前八个字节中哪些是相同的?哎呀。)我建议显示所有代码。您可能会惊讶于输入或输出例程中的bug如此频繁…@sarnold-我更新了这个问题!你可以检查它编辑:解密根本不工作!!看起来它应该可以工作,但是输出中的第一个
缓冲区与代码中的不匹配这一事实是一个很大的问题。试着在更多的输入上测试你的
ToHex()
,看看你是否能找出哪里出了问题。@sarnold oh dman soz我复制了一个错误的输出,我用正确的缓冲区再次更新了这个问题。我看到了,但是这个类是这样工作的还是需要一些编辑?@Mixed,嗯,这取决于;如果输出与原始的
Buffer
Buffer dec
行相同,那么您的类可能会正常工作,不会发生变化。如果输出不同,那么您需要调查它们不同的原因。
byte[] buffer = new byte[] { 0x83, 0x00, /* etc */ }

byte[] enc = incomingCipher.DoFinal(buffer);
byte[] dec = outgoingCipher.DoFinal(enc);

Console.WriteLine("\n\nBuffer\n" + outPutHex.ToHex(buffer));
Console.WriteLine("\n\nBuffer enc\n" + outPutHex.ToHex(enc));
Console.WriteLine("\n\nBuffer dec\n" + outPutHex.ToHex(dec));