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

📄 ideaengine.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:   IDEAEngine.java

package jit.crypto.engines;

import jit.crypto.*;
import jit.crypto.params.KeyParameter;

public class IDEAEngine
    implements BlockCipher
{

    protected static final int BLOCK_SIZE = 8;
    private int workingKey[];
    private static final int MASK = 65535;
    private static final int BASE = 0x10001;

    public IDEAEngine()
    {
        workingKey = null;
    }

    public void init(boolean forEncryption, CipherParameters params)
    {
        if(params instanceof KeyParameter)
        {
            workingKey = generateWorkingKey(forEncryption, ((KeyParameter)params).getKey());
            return;
        } else
        {
            throw new IllegalArgumentException("invalid parameter passed to IDEA init - ".concat(String.valueOf(String.valueOf(params.getClass().getName()))));
        }
    }

    public String getAlgorithmName()
    {
        return "IDEA";
    }

    public int getBlockSize()
    {
        return 8;
    }

    public int processBlock(byte in[], int inOff, byte out[], int outOff)
    {
        if(workingKey == null)
            throw new IllegalStateException("IDEA engine not initialised");
        if(inOff + 8 > in.length)
            throw new DataLengthException("input buffer too short");
        if(outOff + 8 > out.length)
        {
            throw new DataLengthException("output buffer too short");
        } else
        {
            ideaFunc(workingKey, in, inOff, out, outOff);
            return 8;
        }
    }

    public void reset()
    {
    }

    private int bytesToWord(byte in[], int inOff)
    {
        return (in[inOff] << 8 & 0xff00) + (in[inOff + 1] & 0xff);
    }

    private void wordToBytes(int word, byte out[], int outOff)
    {
        out[outOff] = (byte)(word >>> 8);
        out[outOff + 1] = (byte)word;
    }

    private int mul(int x, int y)
    {
        if(x == 0)
            x = 0x10001 - y;
        else
        if(y == 0)
        {
            x = 0x10001 - x;
        } else
        {
            int p = x * y;
            y = p & 0xffff;
            x = p >>> 16;
            x = (y - x) + (y >= x ? 0 : 1);
        }
        return x & 0xffff;
    }

    private void ideaFunc(int workingKey[], byte in[], int inOff, byte out[], int outOff)
    {
        int keyOff = 0;
        int x0 = bytesToWord(in, inOff);
        int x1 = bytesToWord(in, inOff + 2);
        int x2 = bytesToWord(in, inOff + 4);
        int x3 = bytesToWord(in, inOff + 6);
        for(int round = 0; round < 8; round++)
        {
            x0 = mul(x0, workingKey[keyOff++]);
            x1 += workingKey[keyOff++];
            x1 &= 0xffff;
            x2 += workingKey[keyOff++];
            x2 &= 0xffff;
            x3 = mul(x3, workingKey[keyOff++]);
            int t0 = x1;
            int t1 = x2;
            x2 ^= x0;
            x1 ^= x3;
            x2 = mul(x2, workingKey[keyOff++]);
            x1 += x2;
            x1 &= 0xffff;
            x1 = mul(x1, workingKey[keyOff++]);
            x2 += x1;
            x2 &= 0xffff;
            x0 ^= x1;
            x3 ^= x2;
            x1 ^= t1;
            x2 ^= t0;
        }

        wordToBytes(mul(x0, workingKey[keyOff++]), out, outOff);
        wordToBytes(x2 + workingKey[keyOff++], out, outOff + 2);
        wordToBytes(x1 + workingKey[keyOff++], out, outOff + 4);
        wordToBytes(mul(x3, workingKey[keyOff]), out, outOff + 6);
    }

    private int[] expandKey(byte uKey[])
    {
        int key[] = new int[52];
        if(uKey.length < 16)
        {
            byte tmp[] = new byte[16];
            System.arraycopy(uKey, 0, tmp, tmp.length - uKey.length, uKey.length);
            uKey = tmp;
        }
        for(int i = 0; i < 8; i++)
            key[i] = bytesToWord(uKey, i * 2);

        for(int i = 8; i < 52; i++)
        {
            if((i & 0x7) < 6)
            {
                key[i] = ((key[i - 7] & 0x7f) << 9 | key[i - 6] >> 7) & 0xffff;
                continue;
            }
            if((i & 0x7) == 6)
                key[i] = ((key[i - 7] & 0x7f) << 9 | key[i - 14] >> 7) & 0xffff;
            else
                key[i] = ((key[i - 15] & 0x7f) << 9 | key[i - 14] >> 7) & 0xffff;
        }

        return key;
    }

    private int mulInv(int x)
    {
        if(x < 2)
            return x;
        int t0 = 1;
        int t1 = 0x10001 / x;
        for(int y = 0x10001 % x; y != 1;)
        {
            int q = x / y;
            x %= y;
            t0 = t0 + t1 * q & 0xffff;
            if(x == 1)
                return t0;
            q = y / x;
            y %= x;
            t1 = t1 + t0 * q & 0xffff;
        }

        return 1 - t1 & 0xffff;
    }

    int addInv(int x)
    {
        return 0 - x & 0xffff;
    }

    private int[] invertKey(int inKey[])
    {
        int p = 52;
        int key[] = new int[52];
        int inOff = 0;
        int t1 = mulInv(inKey[inOff++]);
        int t2 = addInv(inKey[inOff++]);
        int t3 = addInv(inKey[inOff++]);
        int t4 = mulInv(inKey[inOff++]);
        key[--p] = t4;
        key[--p] = t3;
        key[--p] = t2;
        key[--p] = t1;
        for(int round = 1; round < 8; round++)
        {
            t1 = inKey[inOff++];
            t2 = inKey[inOff++];
            key[--p] = t2;
            key[--p] = t1;
            t1 = mulInv(inKey[inOff++]);
            t2 = addInv(inKey[inOff++]);
            t3 = addInv(inKey[inOff++]);
            t4 = mulInv(inKey[inOff++]);
            key[--p] = t4;
            key[--p] = t2;
            key[--p] = t3;
            key[--p] = t1;
        }

        t1 = inKey[inOff++];
        t2 = inKey[inOff++];
        key[--p] = t2;
        key[--p] = t1;
        t1 = mulInv(inKey[inOff++]);
        t2 = addInv(inKey[inOff++]);
        t3 = addInv(inKey[inOff++]);
        t4 = mulInv(inKey[inOff]);
        key[--p] = t4;
        key[--p] = t3;
        key[--p] = t2;
        key[--p] = t1;
        return key;
    }

    private int[] generateWorkingKey(boolean forEncryption, byte userKey[])
    {
        if(forEncryption)
            return expandKey(userKey);
        else
            return invertKey(expandKey(userKey));
    }

    static 
    {
        BLOCK_SIZE = 8;
        MASK = 65535;
        BASE = 0x10001;
    }
}

⌨️ 快捷键说明

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