我可以让ProcessBytes返回确切的金额吗?

时间:2011-07-16 04:39:08

标签: c# cryptography bouncycastle blowfish

我正在使用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 };

如果我said ProcessBytes(buf, 0, 17)它只会返回16,我也尝试过DoFinal(),但它没有做到这一点! 我应该使用IBufferedCipher或其他东西来获取dec / encing的确切数量,直到IStreamCipher为止?而且我相信CfbBlockCipher会以某种方式被打破或正在做一些事情。

1 个答案:

答案 0 :(得分:0)

我不知道你认为这里没有做什么。您需要多次调用ProcessBytes并使用DoFinal()完成。 ProcessBytes()只返回16个字节是正常的,因为它是块大小的x倍。密码不知道你是否已经完成了字节的输入,因此在调用DoFinal()之前它无法计算另一个块。当然,您需要附加ProcessBytes()和DoFinal()调用的输出以获得最终结果...