2011-06-26 45 views
-1

這裏是我的BlowFishCrypto類BouncyCastle的河豚加密問題

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 

它不工作,但爲什麼?

+0

哪裏是你的'outPutHex.ToHex()'函數? _first_輸出看起來不像測試類中的緩衝區集:0x83 0x00 0xEE 0x03成爲73 00 EC 03。在前四個字節中第一個輸出錯誤50%的情況下,我甚至不會去查看「enc」和「dec」輸出。 (前八個字節是相同的?哎呀。)我建議顯示你的代碼。您可能會驚訝於輸入或輸出例程中的錯誤頻率... – sarnold

+0

@sarnold - 我更新了問題!你可以檢查它 編輯:解密不工作! – Abanoub

+0

它看起來像它應該工作,但事實上輸出中的第一個「緩衝區」與代碼中的「緩衝區」不匹配是一個值得關注的重要原因。嘗試在更大範圍的輸入上測試你的'ToHex()',看看你能否找出它出錯的地方。 – sarnold

回答

1

我想我找到了問題:

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

你創建了兩個密碼上下文incomingCipheroutgoingCipher。你在相同的輸入buffer上運行它們。如果您正在檢查test vectors,這可能很有用,但兩者肯定不會相同。

嘗試:

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

再次更新了這個問題,但我看到了,但是類會如此工作,還是需要一些編輯? – Abanoub

+0

@混合,好吧,這取決於;如果輸出與原來的「緩衝區」和「緩衝區分界線」相同,那麼你的班級可能會正常工作,保持不變。如果輸出是_different_,那麼你需要調查_why_他們是不同的。 – sarnold