2011-07-16 3 views
2

BouncyCastelを使用してCfbBlockCipherを作成していますので、ここにコードがあります。ProcessBytesに正確な金額を返すことができますか?

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

そう...

byte[] buf = 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 }; 

私はそれだけで16を返しますsaid ProcessBytes(buf, 0, 17)場合

は、私はまたのdoFinal()を試してみましたが、それは仕事ですやっていません! は、 IBufferedCipherまでです。私は IStreamCipherなどを使用して、dec/encingの正確な金額を取得する必要がありますか?そして、私は CfbBlockCipherが何らかの形で壊れているか、ここで何かworngをやっていると信じています。

答えて

0

私はあなたがここで仕事をしないと思うか分からない。 ProcessBytesを複数回呼び出す必要があり、DoFinal()で終了する必要があります。 ProcessBytes()はブロックサイズのx倍の16バイトしか返さないのが普通です。暗号はあなたがそれにバイトを供給し終えたかどうかを知らないので、あなたがDoFinal()を呼び出すまで別のブロックを計算することはできません。もちろん、最終結果を得るためにProcessBytes()とDoFinal()呼び出しの出力を追加する必要があります。

関連する問題