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

📄 iesengine.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:   IESEngine.java

package jit.crypto.engines;

import jit.crypto.*;
import jit.crypto.params.*;
import jit.math.BigInteger;

public class IESEngine
{

    BasicAgreement agree;
    DerivationFunction kdf;
    Mac mac;
    BufferedBlockCipher cipher;
    byte macBuf[];
    boolean forEncryption;
    CipherParameters privParam;
    CipherParameters pubParam;
    IESParameters param;

    public IESEngine(BasicAgreement agree, DerivationFunction kdf, Mac mac)
    {
        this.agree = agree;
        this.kdf = kdf;
        this.mac = mac;
        macBuf = new byte[mac.getMacSize()];
        cipher = null;
    }

    public IESEngine(BasicAgreement agree, DerivationFunction kdf, Mac mac, BufferedBlockCipher cipher)
    {
        this.agree = agree;
        this.kdf = kdf;
        this.mac = mac;
        macBuf = new byte[mac.getMacSize()];
        this.cipher = cipher;
    }

    public void init(boolean forEncryption, CipherParameters privParam, CipherParameters pubParam, CipherParameters param)
    {
        this.forEncryption = forEncryption;
        this.privParam = privParam;
        this.pubParam = pubParam;
        this.param = (IESParameters)param;
    }

    private byte[] decryptBlock(byte in_enc[], int inOff, int inLen, byte z[])
        throws InvalidCipherTextException
    {
        byte M[] = null;
        KeyParameter macKey = null;
        KDFParameters kParam = new KDFParameters(z, param.getDerivationV());
        int macKeySize = param.getMacKeySize();
        kdf.init(kParam);
        inLen -= mac.getMacSize();
        if(cipher == null)
        {
            byte buf[] = new byte[inLen + macKeySize / 8];
            M = new byte[inLen];
            kdf.generateBytes(buf, 0, buf.length);
            for(int i = 0; i != inLen; i++)
                M[i] = (byte)(in_enc[inOff + i] ^ buf[i]);

            macKey = new KeyParameter(buf, inLen, macKeySize / 8);
        } else
        {
            int cipherKeySize = ((IESWithCipherParameters)param).getCipherKeySize();
            byte buf[] = new byte[cipherKeySize / 8 + macKeySize / 8];
            kdf.generateBytes(buf, 0, buf.length);
            cipher.init(false, new KeyParameter(buf, 0, cipherKeySize / 8));
            byte tmp[] = new byte[cipher.getOutputSize(inLen)];
            int off = cipher.processBytes(in_enc, inOff, inLen, tmp, 0);
            off += cipher.doFinal(tmp, off);
            M = new byte[off];
            System.arraycopy(tmp, 0, M, 0, off);
            macKey = new KeyParameter(buf, cipherKeySize / 8, macKeySize / 8);
        }
        byte macIV[] = param.getEncodingV();
        mac.init(macKey);
        mac.update(in_enc, inOff, inLen);
        if(macIV != null)
            mac.update(macIV, 0, macIV.length);
        mac.doFinal(macBuf, 0);
        inOff += inLen;
        for(int t = 0; t < macBuf.length; t++)
            if(macBuf[t] != in_enc[inOff + t])
                throw new InvalidCipherTextException("Mac codes failed to equal.");

        return M;
    }

    private byte[] encryptBlock(byte in[], int inOff, int inLen, byte z[])
        throws InvalidCipherTextException
    {
        byte C[] = null;
        KeyParameter macKey = null;
        KDFParameters kParam = new KDFParameters(z, param.getDerivationV());
        int c_text_length = 0;
        int macKeySize = param.getMacKeySize();
        kdf.init(kParam);
        if(cipher == null)
        {
            byte buf[] = new byte[inLen + macKeySize / 8];
            C = new byte[inLen + mac.getMacSize()];
            c_text_length = inLen;
            kdf.generateBytes(buf, 0, buf.length);
            for(int i = 0; i != inLen; i++)
                C[i] = (byte)(in[inOff + i] ^ buf[i]);

            macKey = new KeyParameter(buf, inLen, macKeySize / 8);
        } else
        {
            int cipherKeySize = ((IESWithCipherParameters)param).getCipherKeySize();
            byte buf[] = new byte[cipherKeySize / 8 + macKeySize / 8];
            kdf.generateBytes(buf, 0, buf.length);
            cipher.init(true, new KeyParameter(buf, 0, cipherKeySize / 8));
            c_text_length = cipher.getOutputSize(inLen);
            C = new byte[c_text_length + mac.getMacSize()];
            int off = cipher.processBytes(in, inOff, inLen, C, 0);
            cipher.doFinal(C, off);
            macKey = new KeyParameter(buf, cipherKeySize / 8, macKeySize / 8);
        }
        byte macIV[] = param.getEncodingV();
        mac.init(macKey);
        mac.update(C, 0, c_text_length);
        if(macIV != null)
            mac.update(macIV, 0, macIV.length);
        mac.doFinal(C, c_text_length);
        return C;
    }

    public byte[] processBlock(byte in[], int inOff, int inLen)
        throws InvalidCipherTextException
    {
        agree.init(privParam);
        BigInteger z = agree.calculateAgreement(pubParam);
        byte tmpz[] = z.toByteArray();
        int bitLen = z.bitLength();
        byte share[] = tmpz;
        if(bitLen % 8 == 0)
        {
            share = new byte[tmpz.length - 1];
            System.arraycopy(tmpz, 1, share, 0, share.length);
        }
        if(forEncryption)
            return encryptBlock(in, inOff, inLen, share);
        else
            return decryptBlock(in, inOff, inLen, share);
    }
}

⌨️ 快捷键说明

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