⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bufferedblockcipher.java

📁 进行与数字证书相关开发必须的java源码
💻 JAVA
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   BufferedBlockCipher.java

package jit.crypto;


// Referenced classes of package jit.crypto:
//            DataLengthException, InvalidCipherTextException, BlockCipher, CipherParameters

public class BufferedBlockCipher
{

    protected byte buf[];
    protected int bufOff;
    protected boolean forEncryption;
    protected BlockCipher cipher;
    protected boolean partialBlockOkay;
    protected boolean pgpCFB;

    protected BufferedBlockCipher()
    {
    }

    public BufferedBlockCipher(BlockCipher cipher)
    {
        this.cipher = cipher;
        buf = new byte[cipher.getBlockSize()];
        bufOff = 0;
        String name = cipher.getAlgorithmName();
        int idx = name.indexOf(47) + 1;
        pgpCFB = idx > 0 && name.startsWith("PGP", idx);
        if(pgpCFB)
            partialBlockOkay = true;
        else
            partialBlockOkay = idx > 0 && (name.startsWith("CFB", idx) || name.startsWith("OFB", idx));
    }

    public BlockCipher getUnderlyingCipher()
    {
        return cipher;
    }

    public void init(boolean forEncryption, CipherParameters params)
        throws IllegalArgumentException
    {
        this.forEncryption = forEncryption;
        reset();
        cipher.init(forEncryption, params);
    }

    public int getBlockSize()
    {
        return cipher.getBlockSize();
    }

    public int getUpdateOutputSize(int len)
    {
        int total = len + bufOff;
        int leftOver;
        if(pgpCFB)
            leftOver = total % buf.length - (cipher.getBlockSize() + 2);
        else
            leftOver = total % buf.length;
        return total - leftOver;
    }

    public int getOutputSize(int len)
    {
        int total = len + bufOff;
        int leftOver;
        if(pgpCFB)
        {
            leftOver = total % buf.length - (cipher.getBlockSize() + 2);
        } else
        {
            leftOver = total % buf.length;
            if(leftOver == 0)
                return total;
        }
        return (total - leftOver) + buf.length;
    }

    public int processByte(byte in, byte out[], int outOff)
        throws IllegalStateException, DataLengthException
    {
        int resultLen = 0;
        buf[bufOff++] = in;
        if(bufOff == buf.length)
        {
            resultLen = cipher.processBlock(buf, 0, out, outOff);
            bufOff = 0;
        }
        return resultLen;
    }

    public int processBytes(byte in[], int inOff, int len, byte out[], int outOff)
        throws IllegalStateException, DataLengthException
    {
        if(len < 0)
            throw new IllegalArgumentException("Can't have a negative input length!");
        int blockSize = getBlockSize();
        int length = getUpdateOutputSize(len);
        if(length > 0 && outOff + length > out.length)
            throw new DataLengthException("output buffer too short");
        int resultLen = 0;
        int gapLen = buf.length - bufOff;
        if(len > gapLen)
        {
            System.arraycopy(in, inOff, buf, bufOff, gapLen);
            resultLen += cipher.processBlock(buf, 0, out, outOff);
            bufOff = 0;
            len -= gapLen;
            for(inOff += gapLen; len > buf.length; inOff += blockSize)
            {
                resultLen += cipher.processBlock(in, inOff, out, outOff + resultLen);
                len -= blockSize;
            }

        }
        System.arraycopy(in, inOff, buf, bufOff, len);
        bufOff += len;
        if(bufOff == buf.length)
        {
            resultLen += cipher.processBlock(buf, 0, out, outOff + resultLen);
            bufOff = 0;
        }
        return resultLen;
    }

    public int doFinal(byte out[], int outOff)
        throws InvalidCipherTextException, IllegalStateException, DataLengthException
    {
        int resultLen = 0;
        if(outOff + bufOff > out.length)
            throw new DataLengthException("output buffer too short for doFinal()");
        if(bufOff != 0 && partialBlockOkay)
        {
            cipher.processBlock(buf, 0, buf, 0);
            resultLen = bufOff;
            bufOff = 0;
            System.arraycopy(buf, 0, out, outOff, resultLen);
        } else
        if(bufOff != 0)
            throw new DataLengthException("data not block size aligned");
        reset();
        return resultLen;
    }

    public void reset()
    {
        for(int i = 0; i < buf.length; i++)
            buf[i] = 0;

        bufOff = 0;
        cipher.reset();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -