2

BouncyCastel を使用して CfbBlockCipher を作成しているので、コードは次のとおりです。

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()も試しましたが、うまくいきません!!! IBufferedCipherdec/enc-ing の正確な量を取得するには、それを使用する必要がIStreamCipherありますか? そして、私CfbBlockCipherは何らかの形で壊れているか、ここで何かをしていると信じています。

4

1 に答える 1

0

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

于 2011-07-31T23:35:07.137 に答える