开发者

can I get ProcessBytes to return an exact amount?

am using BouncyCastel to make a CfbBlockCipher so here is the codes.

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[] Proce开发者_Python百科ssBytes(byte[] buffer, int startIndex, int len)
        {
            return cipher.ProcessBytes(buffer, startIndex, len);
        }
        public void   Reset()
        {
            cipher.Reset();
        }
    }
}

so...

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

if i said ProcessBytes(buf, 0, 17) it will only return 16, i also tried DoFinal() but it's not doing it's job!!! is that up to IBufferedCipher should i use IStreamCipher or something else to get the exact amount of what am dec/enc-ing? And i believe CfbBlockCipher is broken somehow or am doing something worng here.


I don't know what you consider not doing the job here. You need to call ProcessBytes multiple times and finish with DoFinal(). It's normal that ProcessBytes() only returns 16 bytes because that is x times the block size. The cipher does not know if you've finished feeding it bytes, so it cannot calculate another block until you call DoFinal(). Of course, you need to append the output of the ProcessBytes() and DoFinal() calls to get the end result...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜