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

📄 desengine.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:   DESEngine.java

package jit.crypto.engines;

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

public class DESEngine
    implements BlockCipher
{

    protected static final int BLOCK_SIZE = 8;
    private int workingKey[];
    static short Df_Key[] = {
        1, 35, 69, 103, 137, 171, 205, 239, 254, 220, 
        186, 152, 118, 84, 50, 16, 137, 171, 205, 239, 
        1, 35, 69, 103
    };
    static short bytebit[] = {
        128, 64, 32, 16, 8, 4, 2, 1
    };
    static int bigbyte[] = {
        0x800000, 0x400000, 0x200000, 0x100000, 0x80000, 0x40000, 0x20000, 0x10000, 32768, 16384, 
        8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 
        8, 4, 2, 1
    };
    static byte pc1[] = {
        56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 
        41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 
        26, 18, 10, 2, 59, 51, 43, 35, 62, 54, 
        46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 
        29, 21, 13, 5, 60, 52, 44, 36, 28, 20, 
        12, 4, 27, 19, 11, 3
    };
    static byte totrot[] = {
        1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 
        19, 21, 23, 25, 27, 28
    };
    static byte pc2[] = {
        13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 
        20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 
        26, 19, 12, 1, 40, 51, 30, 36, 46, 54, 
        29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 
        33, 52, 45, 41, 49, 35, 28, 31
    };
    static int SP1[] = {
        0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404, 4, 0x10000, 1024, 0x1010400, 
        0x1010404, 1024, 0x1000404, 0x1010004, 0x1000000, 4, 1028, 0x1000400, 0x1000400, 0x10400, 
        0x10400, 0x1010000, 0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 1028, 
        0x10404, 0x1000000, 0x10000, 0x1010404, 4, 0x1010000, 0x1010400, 0x1000000, 0x1000000, 1024, 
        0x1010004, 0x10000, 0x10400, 0x1000004, 1024, 4, 0x1000404, 0x10404, 0x1010404, 0x10004, 
        0x1010000, 0x1000404, 0x1000004, 1028, 0x10404, 0x1010400, 1028, 0x1000400, 0x1000400, 0, 
        0x10004, 0x10400, 0, 0x1010004
    };
    static int SP2[] = {
        0x80108020, 0x80008000, 32768, 0x108020, 0x100000, 32, 0x80100020, 0x80008020, 0x80000020, 0x80108020, 
        0x80108000, 0x80000000, 0x80008000, 0x100000, 32, 0x80100020, 0x108000, 0x100020, 0x80008020, 0, 
        0x80000000, 32768, 0x108020, 0x80100000, 0x100020, 0x80000020, 0, 0x108000, 32800, 0x80108000, 
        0x80100000, 32800, 0, 0x108020, 0x80100020, 0x100000, 0x80008020, 0x80100000, 0x80108000, 32768, 
        0x80100000, 0x80008000, 32, 0x80108020, 0x108020, 32, 32768, 0x80000000, 32800, 0x80108000, 
        0x100000, 0x80000020, 0x100020, 0x80008020, 0x80000020, 0x100020, 0x108000, 0, 0x80008000, 32800, 
        0x80000000, 0x80100020, 0x80108020, 0x108000
    };
    static int SP3[] = {
        520, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200, 0x20008, 0x8000008, 
        0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 520, 0x8000000, 8, 0x8020200, 512, 
        0x20200, 0x8020000, 0x8020008, 0x20208, 0x8000208, 0x20200, 0x20000, 0x8000208, 8, 0x8020208, 
        512, 0x8000000, 0x8020200, 0x8000000, 0x20008, 520, 0x20000, 0x8020200, 0x8000200, 0, 
        512, 0x20008, 0x8020208, 0x8000200, 0x8000008, 512, 0, 0x8020008, 0x8000208, 0x20000, 
        0x8000000, 0x8020208, 8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208, 520, 0x8020000, 
        0x20208, 8, 0x8020008, 0x20200
    };
    static int SP4[] = {
        0x802001, 8321, 8321, 128, 0x802080, 0x800081, 0x800001, 8193, 0, 0x802000, 
        0x802000, 0x802081, 129, 0, 0x800080, 0x800001, 1, 8192, 0x800000, 0x802001, 
        128, 0x800000, 8193, 8320, 0x800081, 1, 8320, 0x800080, 8192, 0x802080, 
        0x802081, 129, 0x800080, 0x800001, 0x802000, 0x802081, 129, 0, 0, 0x802000, 
        8320, 0x800080, 0x800081, 1, 0x802001, 8321, 8321, 128, 0x802081, 129, 
        1, 8192, 0x800001, 8193, 0x802080, 0x800081, 8193, 8320, 0x800000, 0x802001, 
        128, 0x800000, 8192, 0x802080
    };
    static int SP5[] = {
        256, 0x2080100, 0x2080000, 0x42000100, 0x80000, 256, 0x40000000, 0x2080000, 0x40080100, 0x80000, 
        0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100, 0x40000000, 0x2000000, 0x40080000, 0x40080000, 0, 
        0x40000100, 0x42080100, 0x42080100, 0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100, 0x2000000, 
        0x42000000, 0x80100, 0x80000, 0x42000100, 256, 0x2000000, 0x40000000, 0x2080000, 0x42000100, 0x40080100, 
        0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 256, 0x2000000, 0x42080000, 0x42080100, 0x80100, 
        0x42000000, 0x42080100, 0x2080000, 0, 0x40080000, 0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000, 
        0, 0x40080000, 0x2080100, 0x40000100
    };
    static int SP6[] = {
        0x20000010, 0x20400000, 16384, 0x20404010, 0x20400000, 16, 0x20404010, 0x400000, 0x20004000, 0x404010, 
        0x400000, 0x20000010, 0x400010, 0x20004000, 0x20000000, 16400, 0, 0x400010, 0x20004010, 16384, 
        0x404000, 0x20004010, 16, 0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 16400, 0x404000, 
        0x20404000, 0x20000000, 0x20004000, 16, 0x20400010, 0x404000, 0x20404010, 0x400000, 16400, 0x20000010, 
        0x400000, 0x20004000, 0x20000000, 16400, 0x20000010, 0x20404010, 0x404000, 0x20400000, 0x404010, 0x20404000, 
        0, 0x20400010, 16, 16384, 0x20400000, 0x404010, 16384, 0x400010, 0x20004010, 0, 
        0x20404000, 0x20000000, 0x400010, 0x20004010
    };
    static int SP7[] = {
        0x200000, 0x4200002, 0x4000802, 0, 2048, 0x4000802, 0x200802, 0x4200800, 0x4200802, 0x200000, 
        0, 0x4000002, 2, 0x4000000, 0x4200002, 2050, 0x4000800, 0x200802, 0x200002, 0x4000800, 
        0x4000002, 0x4200000, 0x4200800, 0x200002, 0x4200000, 2048, 2050, 0x4200802, 0x200800, 2, 
        0x4000000, 0x200800, 0x4000000, 0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 2, 
        0x200002, 0x4000000, 0x4000800, 0x200000, 0x4200800, 2050, 0x200802, 0x4200800, 2050, 0x4000002, 
        0x4200802, 0x4200000, 0x200800, 0, 2, 0x4200802, 0, 0x200802, 0x4200000, 2048, 
        0x4000002, 0x4000800, 2048, 0x200002
    };
    static int SP8[] = {
        0x10001040, 4096, 0x40000, 0x10041040, 0x10000000, 0x10001040, 64, 0x10000000, 0x40040, 0x10040000, 
        0x10041040, 0x41000, 0x10041000, 0x41040, 4096, 64, 0x10040000, 0x10000040, 0x10001000, 4160, 
        0x41000, 0x40040, 0x10040040, 0x10041000, 4160, 0, 0, 0x10040040, 0x10000040, 0x10001000, 
        0x41040, 0x40000, 0x41040, 0x40000, 0x10041000, 4096, 64, 0x10040040, 4096, 0x41040, 
        0x10001000, 64, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040, 
        0x40040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0, 0x10041040, 0x41000, 0x41000, 4160, 
        4160, 0x40040, 0x10000000, 0x10041000
    };

    public DESEngine()
    {
        workingKey = null;
    }

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

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

    public int getBlockSize()
    {
        return 8;
    }

    public int processBlock(byte in[], int inOff, byte out[], int outOff)
    {
        if(workingKey == null)
            throw new IllegalStateException("DES 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
        {
            desFunc(workingKey, in, inOff, out, outOff);
            return 8;
        }
    }

    public void reset()
    {
    }

    protected int[] generateWorkingKey(boolean encrypting, byte key[])
    {
        int newKey[] = new int[32];
        boolean pc1m[] = new boolean[56];
        boolean pcr[] = new boolean[56];
        for(int j = 0; j < 56; j++)
        {
            int l = pc1[j];
            pc1m[j] = (key[l >>> 3] & bytebit[l & 0x7]) != 0;
        }

        for(int i = 0; i < 16; i++)
        {
            int m;
            if(encrypting)
                m = i << 1;
            else
                m = 15 - i << 1;
            int n = m + 1;
            newKey[m] = newKey[n] = 0;
            for(int j = 0; j < 28; j++)
            {
                int l = j + totrot[i];
                if(l < 28)
                    pcr[j] = pc1m[l];
                else
                    pcr[j] = pc1m[l - 28];
            }

            for(int j = 28; j < 56; j++)
            {
                int l = j + totrot[i];
                if(l < 56)
                    pcr[j] = pc1m[l];
                else
                    pcr[j] = pc1m[l - 28];
            }

            for(int j = 0; j < 24; j++)
            {
                if(pcr[pc2[j]])
                    newKey[m] |= bigbyte[j];
                if(pcr[pc2[j + 24]])
                    newKey[n] |= bigbyte[j];
            }

        }

        for(int i = 0; i != 32; i += 2)
        {
            int i1 = newKey[i];
            int i2 = newKey[i + 1];
            newKey[i] = (i1 & 0xfc0000) << 6 | (i1 & 0xfc0) << 10 | (i2 & 0xfc0000) >>> 10 | (i2 & 0xfc0) >>> 6;
            newKey[i + 1] = (i1 & 0x3f000) << 12 | (i1 & 0x3f) << 16 | (i2 & 0x3f000) >>> 4 | i2 & 0x3f;
        }

        return newKey;
    }

    protected void desFunc(int wKey[], byte in[], int inOff, byte out[], int outOff)
    {
        int left = (in[inOff + 0] & 0xff) << 24;
        left |= (in[inOff + 1] & 0xff) << 16;
        left |= (in[inOff + 2] & 0xff) << 8;
        left |= in[inOff + 3] & 0xff;
        int right = (in[inOff + 4] & 0xff) << 24;
        right |= (in[inOff + 5] & 0xff) << 16;
        right |= (in[inOff + 6] & 0xff) << 8;
        right |= in[inOff + 7] & 0xff;
        int work = (left >>> 4 ^ right) & 0xf0f0f0f;
        right ^= work;
        left ^= work << 4;
        work = (left >>> 16 ^ right) & 0xffff;
        right ^= work;
        left ^= work << 16;
        work = (right >>> 2 ^ left) & 0x33333333;
        left ^= work;
        right ^= work << 2;
        work = (right >>> 8 ^ left) & 0xff00ff;
        left ^= work;
        right ^= work << 8;
        right = (right << 1 | right >>> 31 & 0x1) & 0xffffffff;
        work = (left ^ right) & 0xaaaaaaaa;
        left ^= work;
        right ^= work;
        left = (left << 1 | left >>> 31 & 0x1) & 0xffffffff;
        for(int round = 0; round < 8; round++)
        {
            work = right << 28 | right >>> 4;
            work ^= wKey[round * 4 + 0];
            int fval = SP7[work & 0x3f];
            fval |= SP5[work >>> 8 & 0x3f];
            fval |= SP3[work >>> 16 & 0x3f];
            fval |= SP1[work >>> 24 & 0x3f];
            work = right ^ wKey[round * 4 + 1];
            fval |= SP8[work & 0x3f];
            fval |= SP6[work >>> 8 & 0x3f];
            fval |= SP4[work >>> 16 & 0x3f];
            fval |= SP2[work >>> 24 & 0x3f];
            left ^= fval;
            work = left << 28 | left >>> 4;
            work ^= wKey[round * 4 + 2];
            fval = SP7[work & 0x3f];
            fval |= SP5[work >>> 8 & 0x3f];
            fval |= SP3[work >>> 16 & 0x3f];
            fval |= SP1[work >>> 24 & 0x3f];
            work = left ^ wKey[round * 4 + 3];
            fval |= SP8[work & 0x3f];
            fval |= SP6[work >>> 8 & 0x3f];
            fval |= SP4[work >>> 16 & 0x3f];
            fval |= SP2[work >>> 24 & 0x3f];
            right ^= fval;
        }

        right = right << 31 | right >>> 1;
        work = (left ^ right) & 0xaaaaaaaa;
        left ^= work;
        right ^= work;
        left = left << 31 | left >>> 1;
        work = (left >>> 8 ^ right) & 0xff00ff;
        right ^= work;
        left ^= work << 8;
        work = (left >>> 2 ^ right) & 0x33333333;
        right ^= work;
        left ^= work << 2;
        work = (right >>> 16 ^ left) & 0xffff;
        left ^= work;
        right ^= work << 16;
        work = (right >>> 4 ^ left) & 0xf0f0f0f;
        left ^= work;
        right ^= work << 4;
        out[outOff + 0] = (byte)(right >>> 24 & 0xff);
        out[outOff + 1] = (byte)(right >>> 16 & 0xff);
        out[outOff + 2] = (byte)(right >>> 8 & 0xff);
        out[outOff + 3] = (byte)(right & 0xff);
        out[outOff + 4] = (byte)(left >>> 24 & 0xff);
        out[outOff + 5] = (byte)(left >>> 16 & 0xff);
        out[outOff + 6] = (byte)(left >>> 8 & 0xff);
        out[outOff + 7] = (byte)(left & 0xff);
    }

    static 
    {
        BLOCK_SIZE = 8;
    }
}

⌨️ 快捷键说明

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