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

📄 aeslightengine.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:   AESLightEngine.java

package jit.crypto.engines;

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

public class AESLightEngine
    implements BlockCipher
{

    private static final byte S[] = {
        99, 124, 119, 123, -14, 107, 111, -59, 48, 1, 
        103, 43, -2, -41, -85, 118, -54, -126, -55, 125, 
        -6, 89, 71, -16, -83, -44, -94, -81, -100, -92, 
        114, -64, -73, -3, -109, 38, 54, 63, -9, -52, 
        52, -91, -27, -15, 113, -40, 49, 21, 4, -57, 
        35, -61, 24, -106, 5, -102, 7, 18, -128, -30, 
        -21, 39, -78, 117, 9, -125, 44, 26, 27, 110, 
        90, -96, 82, 59, -42, -77, 41, -29, 47, -124, 
        83, -47, 0, -19, 32, -4, -79, 91, 106, -53, 
        -66, 57, 74, 76, 88, -49, -48, -17, -86, -5, 
        67, 77, 51, -123, 69, -7, 2, 127, 80, 60, 
        -97, -88, 81, -93, 64, -113, -110, -99, 56, -11, 
        -68, -74, -38, 33, 16, -1, -13, -46, -51, 12, 
        19, -20, 95, -105, 68, 23, -60, -89, 126, 61, 
        100, 93, 25, 115, 96, -127, 79, -36, 34, 42, 
        -112, -120, 70, -18, -72, 20, -34, 94, 11, -37, 
        -32, 50, 58, 10, 73, 6, 36, 92, -62, -45, 
        -84, 98, -111, -107, -28, 121, -25, -56, 55, 109, 
        -115, -43, 78, -87, 108, 86, -12, -22, 101, 122, 
        -82, 8, -70, 120, 37, 46, 28, -90, -76, -58, 
        -24, -35, 116, 31, 75, -67, -117, -118, 112, 62, 
        -75, 102, 72, 3, -10, 14, 97, 53, 87, -71, 
        -122, -63, 29, -98, -31, -8, -104, 17, 105, -39, 
        -114, -108, -101, 30, -121, -23, -50, 85, 40, -33, 
        -116, -95, -119, 13, -65, -26, 66, 104, 65, -103, 
        45, 15, -80, 84, -69, 22
    };
    private static final byte Si[] = {
        82, 9, 106, -43, 48, 54, -91, 56, -65, 64, 
        -93, -98, -127, -13, -41, -5, 124, -29, 57, -126, 
        -101, 47, -1, -121, 52, -114, 67, 68, -60, -34, 
        -23, -53, 84, 123, -108, 50, -90, -62, 35, 61, 
        -18, 76, -107, 11, 66, -6, -61, 78, 8, 46, 
        -95, 102, 40, -39, 36, -78, 118, 91, -94, 73, 
        109, -117, -47, 37, 114, -8, -10, 100, -122, 104, 
        -104, 22, -44, -92, 92, -52, 93, 101, -74, -110, 
        108, 112, 72, 80, -3, -19, -71, -38, 94, 21, 
        70, 87, -89, -115, -99, -124, -112, -40, -85, 0, 
        -116, -68, -45, 10, -9, -28, 88, 5, -72, -77, 
        69, 6, -48, 44, 30, -113, -54, 63, 15, 2, 
        -63, -81, -67, 3, 1, 19, -118, 107, 58, -111, 
        17, 65, 79, 103, -36, -22, -105, -14, -49, -50, 
        -16, -76, -26, 115, -106, -84, 116, 34, -25, -83, 
        53, -123, -30, -7, 55, -24, 28, 117, -33, 110, 
        71, -15, 26, 113, 29, 41, -59, -119, 111, -73, 
        98, 14, -86, 24, -66, 27, -4, 86, 62, 75, 
        -58, -46, 121, 32, -102, -37, -64, -2, 120, -51, 
        90, -12, 31, -35, -88, 51, -120, 7, -57, 49, 
        -79, 18, 16, 89, 39, -128, -20, 95, 96, 81, 
        127, -87, 25, -75, 74, 13, 45, -27, 122, -97, 
        -109, -55, -100, -17, -96, -32, 59, 77, -82, 42, 
        -11, -80, -56, -21, -69, 60, -125, 83, -103, 97, 
        23, 43, 4, 126, -70, 119, -42, 38, -31, 105, 
        20, 99, 85, 33, 12, 125
    };
    private static final int rcon[] = {
        1, 2, 4, 8, 16, 32, 64, 128, 27, 54, 
        108, 216, 171, 77, 154, 47, 94, 188, 99, 198, 
        151, 53, 106, 212, 179, 125, 250, 239, 197, 145
    };
    private static final int m1 = 0x80808080;
    private static final int m2 = 0x7f7f7f7f;
    private static final int m3 = 27;
    private int ROUNDS;
    private int WorkingKey[][];
    private int C0;
    private int C1;
    private int C2;
    private int C3;
    private boolean forEncryption;
    private static final int BLOCK_SIZE = 16;

    private int shift(int r, int shift)
    {
        return r >>> shift | r << 32 - shift;
    }

    private int FFmulX(int x)
    {
        return (x & 0x7f7f7f7f) << 1 ^ ((x & 0x80808080) >>> 7) * 27;
    }

    private int mcol(int x)
    {
        int f2 = FFmulX(x);
        return f2 ^ shift(x ^ f2, 8) ^ shift(x, 16) ^ shift(x, 24);
    }

    private int inv_mcol(int x)
    {
        int f2 = FFmulX(x);
        int f4 = FFmulX(f2);
        int f8 = FFmulX(f4);
        int f9 = x ^ f8;
        return f2 ^ f4 ^ f8 ^ shift(f2 ^ f9, 8) ^ shift(f4 ^ f9, 16) ^ shift(f9, 24);
    }

    private int subWord(int x)
    {
        return S[x & 0xff] & 0xff | (S[x >> 8 & 0xff] & 0xff) << 8 | (S[x >> 16 & 0xff] & 0xff) << 16 | S[x >> 24 & 0xff] << 24;
    }

    private int[][] generateWorkingKey(byte key[], boolean forEncryption)
    {
        int KC = key.length / 4;
        if(KC != 4 && KC != 6 && KC != 8)
            throw new IllegalArgumentException("Key length not 128/192/256 bits.");
        ROUNDS = KC + 6;
        int W[][] = new int[ROUNDS + 1][4];
        int t = 0;
        for(int i = 0; i < key.length;)
        {
            W[t >> 2][t & 0x3] = key[i] & 0xff | (key[i + 1] & 0xff) << 8 | (key[i + 2] & 0xff) << 16 | key[i + 3] << 24;
            i += 4;
            t++;
        }

        int k = ROUNDS + 1 << 2;
        for(int i = KC; i < k; i++)
        {
            int temp = W[i - 1 >> 2][i - 1 & 0x3];
            if(i % KC == 0)
                temp = subWord(shift(temp, 8)) ^ rcon[i / KC - 1];
            else
            if(KC > 6 && i % KC == 4)
                temp = subWord(temp);
            W[i >> 2][i & 0x3] = W[i - KC >> 2][i - KC & 0x3] ^ temp;
        }

        if(!forEncryption)
        {
            for(int j = 1; j < ROUNDS; j++)
            {
                for(int i = 0; i < 4; i++)
                    W[j][i] = inv_mcol(W[j][i]);

            }

        }
        return W;
    }

    public AESLightEngine()
    {
        WorkingKey = null;
    }

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

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

    public int getBlockSize()
    {
        return 16;
    }

    public int processBlock(byte in[], int inOff, byte out[], int outOff)
    {
        if(WorkingKey == null)
            throw new IllegalStateException("AES engine not initialised");
        if(inOff + 16 > in.length)
            throw new DataLengthException("input buffer too short");
        if(outOff + 16 > out.length)
            throw new DataLengthException("output buffer too short");
        if(forEncryption)
        {
            unpackBlock(in, inOff);
            encryptBlock(WorkingKey);
            packBlock(out, outOff);
        } else
        {
            unpackBlock(in, inOff);
            decryptBlock(WorkingKey);
            packBlock(out, outOff);
        }
        return 16;
    }

    public void reset()
    {
    }

    private final void unpackBlock(byte bytes[], int off)
    {
        int index = off;
        C0 = bytes[index++] & 0xff;
        C0 |= (bytes[index++] & 0xff) << 8;
        C0 |= (bytes[index++] & 0xff) << 16;
        C0 |= bytes[index++] << 24;
        C1 = bytes[index++] & 0xff;
        C1 |= (bytes[index++] & 0xff) << 8;
        C1 |= (bytes[index++] & 0xff) << 16;
        C1 |= bytes[index++] << 24;
        C2 = bytes[index++] & 0xff;
        C2 |= (bytes[index++] & 0xff) << 8;
        C2 |= (bytes[index++] & 0xff) << 16;
        C2 |= bytes[index++] << 24;
        C3 = bytes[index++] & 0xff;
        C3 |= (bytes[index++] & 0xff) << 8;
        C3 |= (bytes[index++] & 0xff) << 16;
        C3 |= bytes[index++] << 24;
    }

    private final void packBlock(byte bytes[], int off)
    {
        int index = off;
        bytes[index++] = (byte)C0;
        bytes[index++] = (byte)(C0 >> 8);
        bytes[index++] = (byte)(C0 >> 16);
        bytes[index++] = (byte)(C0 >> 24);
        bytes[index++] = (byte)C1;
        bytes[index++] = (byte)(C1 >> 8);
        bytes[index++] = (byte)(C1 >> 16);
        bytes[index++] = (byte)(C1 >> 24);
        bytes[index++] = (byte)C2;
        bytes[index++] = (byte)(C2 >> 8);
        bytes[index++] = (byte)(C2 >> 16);
        bytes[index++] = (byte)(C2 >> 24);
        bytes[index++] = (byte)C3;
        bytes[index++] = (byte)(C3 >> 8);
        bytes[index++] = (byte)(C3 >> 16);
        bytes[index++] = (byte)(C3 >> 24);
    }

    private void encryptBlock(int KW[][])
    {
        C0 ^= KW[0][0];
        C1 ^= KW[0][1];
        C2 ^= KW[0][2];
        C3 ^= KW[0][3];
        int r;
        int r0;
        int r1;
        int r2;
        int r3;
        for(r = 1; r < ROUNDS - 1;)
        {
            r0 = mcol(S[C0 & 0xff] & 0xff ^ (S[C1 >> 8 & 0xff] & 0xff) << 8 ^ (S[C2 >> 16 & 0xff] & 0xff) << 16 ^ S[C3 >> 24 & 0xff] << 24) ^ KW[r][0];
            r1 = mcol(S[C1 & 0xff] & 0xff ^ (S[C2 >> 8 & 0xff] & 0xff) << 8 ^ (S[C3 >> 16 & 0xff] & 0xff) << 16 ^ S[C0 >> 24 & 0xff] << 24) ^ KW[r][1];
            r2 = mcol(S[C2 & 0xff] & 0xff ^ (S[C3 >> 8 & 0xff] & 0xff) << 8 ^ (S[C0 >> 16 & 0xff] & 0xff) << 16 ^ S[C1 >> 24 & 0xff] << 24) ^ KW[r][2];
            r3 = mcol(S[C3 & 0xff] & 0xff ^ (S[C0 >> 8 & 0xff] & 0xff) << 8 ^ (S[C1 >> 16 & 0xff] & 0xff) << 16 ^ S[C2 >> 24 & 0xff] << 24) ^ KW[r++][3];
            C0 = mcol(S[r0 & 0xff] & 0xff ^ (S[r1 >> 8 & 0xff] & 0xff) << 8 ^ (S[r2 >> 16 & 0xff] & 0xff) << 16 ^ S[r3 >> 24 & 0xff] << 24) ^ KW[r][0];
            C1 = mcol(S[r1 & 0xff] & 0xff ^ (S[r2 >> 8 & 0xff] & 0xff) << 8 ^ (S[r3 >> 16 & 0xff] & 0xff) << 16 ^ S[r0 >> 24 & 0xff] << 24) ^ KW[r][1];
            C2 = mcol(S[r2 & 0xff] & 0xff ^ (S[r3 >> 8 & 0xff] & 0xff) << 8 ^ (S[r0 >> 16 & 0xff] & 0xff) << 16 ^ S[r1 >> 24 & 0xff] << 24) ^ KW[r][2];
            C3 = mcol(S[r3 & 0xff] & 0xff ^ (S[r0 >> 8 & 0xff] & 0xff) << 8 ^ (S[r1 >> 16 & 0xff] & 0xff) << 16 ^ S[r2 >> 24 & 0xff] << 24) ^ KW[r++][3];
        }

        r0 = mcol(S[C0 & 0xff] & 0xff ^ (S[C1 >> 8 & 0xff] & 0xff) << 8 ^ (S[C2 >> 16 & 0xff] & 0xff) << 16 ^ S[C3 >> 24 & 0xff] << 24) ^ KW[r][0];
        r1 = mcol(S[C1 & 0xff] & 0xff ^ (S[C2 >> 8 & 0xff] & 0xff) << 8 ^ (S[C3 >> 16 & 0xff] & 0xff) << 16 ^ S[C0 >> 24 & 0xff] << 24) ^ KW[r][1];
        r2 = mcol(S[C2 & 0xff] & 0xff ^ (S[C3 >> 8 & 0xff] & 0xff) << 8 ^ (S[C0 >> 16 & 0xff] & 0xff) << 16 ^ S[C1 >> 24 & 0xff] << 24) ^ KW[r][2];
        r3 = mcol(S[C3 & 0xff] & 0xff ^ (S[C0 >> 8 & 0xff] & 0xff) << 8 ^ (S[C1 >> 16 & 0xff] & 0xff) << 16 ^ S[C2 >> 24 & 0xff] << 24) ^ KW[r++][3];
        C0 = S[r0 & 0xff] & 0xff ^ (S[r1 >> 8 & 0xff] & 0xff) << 8 ^ (S[r2 >> 16 & 0xff] & 0xff) << 16 ^ S[r3 >> 24 & 0xff] << 24 ^ KW[r][0];
        C1 = S[r1 & 0xff] & 0xff ^ (S[r2 >> 8 & 0xff] & 0xff) << 8 ^ (S[r3 >> 16 & 0xff] & 0xff) << 16 ^ S[r0 >> 24 & 0xff] << 24 ^ KW[r][1];
        C2 = S[r2 & 0xff] & 0xff ^ (S[r3 >> 8 & 0xff] & 0xff) << 8 ^ (S[r0 >> 16 & 0xff] & 0xff) << 16 ^ S[r1 >> 24 & 0xff] << 24 ^ KW[r][2];
        C3 = S[r3 & 0xff] & 0xff ^ (S[r0 >> 8 & 0xff] & 0xff) << 8 ^ (S[r1 >> 16 & 0xff] & 0xff) << 16 ^ S[r2 >> 24 & 0xff] << 24 ^ KW[r][3];
    }

    private final void decryptBlock(int KW[][])
    {
        C0 ^= KW[ROUNDS][0];
        C1 ^= KW[ROUNDS][1];
        C2 ^= KW[ROUNDS][2];
        C3 ^= KW[ROUNDS][3];
        int r;
        int r0;
        int r1;
        int r2;
        int r3;
        for(r = ROUNDS - 1; r > 1;)
        {
            r0 = inv_mcol(Si[C0 & 0xff] & 0xff ^ (Si[C3 >> 8 & 0xff] & 0xff) << 8 ^ (Si[C2 >> 16 & 0xff] & 0xff) << 16 ^ Si[C1 >> 24 & 0xff] << 24) ^ KW[r][0];
            r1 = inv_mcol(Si[C1 & 0xff] & 0xff ^ (Si[C0 >> 8 & 0xff] & 0xff) << 8 ^ (Si[C3 >> 16 & 0xff] & 0xff) << 16 ^ Si[C2 >> 24 & 0xff] << 24) ^ KW[r][1];
            r2 = inv_mcol(Si[C2 & 0xff] & 0xff ^ (Si[C1 >> 8 & 0xff] & 0xff) << 8 ^ (Si[C0 >> 16 & 0xff] & 0xff) << 16 ^ Si[C3 >> 24 & 0xff] << 24) ^ KW[r][2];
            r3 = inv_mcol(Si[C3 & 0xff] & 0xff ^ (Si[C2 >> 8 & 0xff] & 0xff) << 8 ^ (Si[C1 >> 16 & 0xff] & 0xff) << 16 ^ Si[C0 >> 24 & 0xff] << 24) ^ KW[r--][3];
            C0 = inv_mcol(Si[r0 & 0xff] & 0xff ^ (Si[r3 >> 8 & 0xff] & 0xff) << 8 ^ (Si[r2 >> 16 & 0xff] & 0xff) << 16 ^ Si[r1 >> 24 & 0xff] << 24) ^ KW[r][0];
            C1 = inv_mcol(Si[r1 & 0xff] & 0xff ^ (Si[r0 >> 8 & 0xff] & 0xff) << 8 ^ (Si[r3 >> 16 & 0xff] & 0xff) << 16 ^ Si[r2 >> 24 & 0xff] << 24) ^ KW[r][1];
            C2 = inv_mcol(Si[r2 & 0xff] & 0xff ^ (Si[r1 >> 8 & 0xff] & 0xff) << 8 ^ (Si[r0 >> 16 & 0xff] & 0xff) << 16 ^ Si[r3 >> 24 & 0xff] << 24) ^ KW[r][2];
            C3 = inv_mcol(Si[r3 & 0xff] & 0xff ^ (Si[r2 >> 8 & 0xff] & 0xff) << 8 ^ (Si[r1 >> 16 & 0xff] & 0xff) << 16 ^ Si[r0 >> 24 & 0xff] << 24) ^ KW[r--][3];
        }

        r0 = inv_mcol(Si[C0 & 0xff] & 0xff ^ (Si[C3 >> 8 & 0xff] & 0xff) << 8 ^ (Si[C2 >> 16 & 0xff] & 0xff) << 16 ^ Si[C1 >> 24 & 0xff] << 24) ^ KW[r][0];
        r1 = inv_mcol(Si[C1 & 0xff] & 0xff ^ (Si[C0 >> 8 & 0xff] & 0xff) << 8 ^ (Si[C3 >> 16 & 0xff] & 0xff) << 16 ^ Si[C2 >> 24 & 0xff] << 24) ^ KW[r][1];
        r2 = inv_mcol(Si[C2 & 0xff] & 0xff ^ (Si[C1 >> 8 & 0xff] & 0xff) << 8 ^ (Si[C0 >> 16 & 0xff] & 0xff) << 16 ^ Si[C3 >> 24 & 0xff] << 24) ^ KW[r][2];
        r3 = inv_mcol(Si[C3 & 0xff] & 0xff ^ (Si[C2 >> 8 & 0xff] & 0xff) << 8 ^ (Si[C1 >> 16 & 0xff] & 0xff) << 16 ^ Si[C0 >> 24 & 0xff] << 24) ^ KW[r--][3];
        C0 = Si[r0 & 0xff] & 0xff ^ (Si[r3 >> 8 & 0xff] & 0xff) << 8 ^ (Si[r2 >> 16 & 0xff] & 0xff) << 16 ^ Si[r1 >> 24 & 0xff] << 24 ^ KW[0][0];
        C1 = Si[r1 & 0xff] & 0xff ^ (Si[r0 >> 8 & 0xff] & 0xff) << 8 ^ (Si[r3 >> 16 & 0xff] & 0xff) << 16 ^ Si[r2 >> 24 & 0xff] << 24 ^ KW[0][1];
        C2 = Si[r2 & 0xff] & 0xff ^ (Si[r1 >> 8 & 0xff] & 0xff) << 8 ^ (Si[r0 >> 16 & 0xff] & 0xff) << 16 ^ Si[r3 >> 24 & 0xff] << 24 ^ KW[0][2];
        C3 = Si[r3 & 0xff] & 0xff ^ (Si[r2 >> 8 & 0xff] & 0xff) << 8 ^ (Si[r1 >> 16 & 0xff] & 0xff) << 16 ^ Si[r0 >> 24 & 0xff] << 24 ^ KW[0][3];
    }

    static 
    {
        m1 = 0x80808080;
        m2 = 0x7f7f7f7f;
        m3 = 27;
        BLOCK_SIZE = 16;
    }
}

⌨️ 快捷键说明

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