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

📄 des.java

📁 说明: 1、里面有什么: 1.1、org.bouncycastle.*下的所有软件是bouncycastle组织开发的软件包 1.2、org.infosecurity.*下的软件包括
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.infosecurity.cryptography;
/**
 * <p>Title: DES算法的软件实现 </p>
 * <p>Description: DES算法的软件实现,功能包括:加密解密</p>
 * <p>Copyright: Copyright (c) 2003</p>
 */
public class DES   {


    private int[] encryptKeys = new int[32];
    private int[] decryptKeys = new int[32];

    private int[] tempInts = new int[2];


    public DES( ) {

    }

    // 步1.构造对象
    public DES( byte[] key )    {
        if( key.length == 7 ) {
            byte[] key8 = new byte[8];
            makeSMBKey( key, key8 );
            setKey( key8 );
        } else {
            setKey( key );
        }
    }
    /**
     * 步2. 加密一个数组(长度必须是8的倍数)
     */
    public byte[] encrypt(byte[] clearText) {

        int length = clearText.length;

        if (length % 8 != 0) {
            System.out.println("长度必须是8的倍数");
            return null;
        }

        byte[] cipherText = new byte[length];
        int count = length / 8;

        for (int i=0; i<count; i++)
            encrypt(clearText, i*8, cipherText, i*8);

        return cipherText;
    }

    /**
     * 步3.解密一个数组(长度必须是8的倍数)
     */
    public byte[] decrypt(byte[] cipherText) {

        int length = cipherText.length;

        if (length % 8 != 0) {
            System.out.println("长度必须是8的倍数");
            return null;
        }

        byte[] clearText = new byte[length];
        int count = length / 8;

        for (int i=0; i<count; i++)
            decrypt(cipherText, i*8, clearText, i*8);

        return clearText;
    }

    /*=====================以下为内部使用方法===================*/
    public static void makeSMBKey(byte[] key7, byte[] key8) {

        int i;

        key8[0] = (byte) ( ( key7[0] >> 1) & 0xff);
        key8[1] = (byte)(( ((key7[0] & 0x01) << 6) | (((key7[1] & 0xff)>>2) & 0xff)) & 0xff );
        key8[2] = (byte)(( ((key7[1] & 0x03) << 5) | (((key7[2] & 0xff)>>3) & 0xff)) & 0xff );
        key8[3] = (byte)(( ((key7[2] & 0x07) << 4) | (((key7[3] & 0xff)>>4) & 0xff)) & 0xff );
        key8[4] = (byte)(( ((key7[3] & 0x0F) << 3) | (((key7[4] & 0xff)>>5) & 0xff)) & 0xff );
        key8[5] = (byte)(( ((key7[4] & 0x1F) << 2) | (((key7[5] & 0xff)>>6) & 0xff)) & 0xff );
        key8[6] = (byte)(( ((key7[5] & 0x3F) << 1) | (((key7[6] & 0xff)>>7) & 0xff)) & 0xff );
        key8[7] = (byte)(key7[6] & 0x7F);
        for (i=0;i<8;i++) {
            key8[i] = (byte)( key8[i] << 1);
        }
    }

    public void setKey( byte[] key ) {
        deskey( key, true, encryptKeys );
        deskey( key, false, decryptKeys );
    }
    private void deskey( byte[] keyBlock, boolean encrypting, int[] KnL ) {

        int i, j, l, m, n;
        int[] pc1m = new int[56];
        int[] pcr  = new int[56];
        int[] kn   = new int[32];

        for ( j = 0; j < 56; ++j )  {
            l       = pc1[j];
            m       = l & 07;
            pc1m[j] = ( (keyBlock[l >>> 3] & bytebit[m]) != 0 )? 1: 0;
        }

        for ( i = 0; i < 16; ++i ) {

            if ( encrypting )
                m = i << 1;
            else
                m = (15-i) << 1;
            n = m+1;
            kn[m] = kn[n] = 0;
            for ( j = 0; j < 28; ++j ) {
                l = j+totrot[i];
                if ( l < 28 )
                    pcr[j] = pc1m[l];
                else
                    pcr[j] = pc1m[l-28];
            }
            for ( j=28; j < 56; ++j ) {
                l = j+totrot[i];
                if ( l < 56 )
                    pcr[j] = pc1m[l];
                else
                    pcr[j] = pc1m[l-28];
            }
            for ( j = 0; j < 24; ++j ) {
                if ( pcr[pc2[j]] != 0 )
                    kn[m] |= bigbyte[j];
                if ( pcr[pc2[j+24]] != 0 )
                    kn[n] |= bigbyte[j];
            }
        }
        cookey( kn, KnL );
    }

    private void cookey( int[] raw, int KnL[] )     {
        int raw0, raw1;
        int rawi, KnLi;
        int i;

        for ( i = 0, rawi = 0, KnLi = 0; i < 16; ++i )   {
            raw0 = raw[rawi++];
            raw1 = raw[rawi++];
            KnL[KnLi]  = (raw0 & 0x00fc0000) <<   6;
            KnL[KnLi] |= (raw0 & 0x00000fc0) <<  10;
            KnL[KnLi] |= (raw1 & 0x00fc0000) >>> 10;
            KnL[KnLi] |= (raw1 & 0x00000fc0) >>>  6;
            ++KnLi;
            KnL[KnLi]  = (raw0 & 0x0003f000) <<  12;
            KnL[KnLi] |= (raw0 & 0x0000003f) <<  16;
            KnL[KnLi] |= (raw1 & 0x0003f000) >>>  4;
            KnL[KnLi] |= (raw1 & 0x0000003f);
            ++KnLi;
        }
    }

    private void encrypt( byte[] clearText, int clearOff, byte[] cipherText, int cipherOff ) {

        squashBytesToInts( clearText, clearOff, tempInts, 0, 2 );
        des( tempInts, tempInts, encryptKeys );
        spreadIntsToBytes( tempInts, 0, cipherText, cipherOff, 2 );
    }

    private void decrypt( byte[] cipherText, int cipherOff, byte[] clearText, int clearOff ) {

        squashBytesToInts( cipherText, cipherOff, tempInts, 0, 2 );
        des( tempInts, tempInts, decryptKeys );
        spreadIntsToBytes( tempInts, 0, clearText, clearOff, 2 );
    }

    private void des( int[] inInts, int[] outInts, int[] keys ) {

        int fval, work, right, leftt;
        int round;
        int keysi = 0;

        leftt = inInts[0];
        right = inInts[1];

        work   = ((leftt >>>  4) ^ right) & 0x0f0f0f0f;
        right ^= work;
        leftt ^= (work << 4);

        work   = ((leftt >>> 16) ^ right) & 0x0000ffff;
        right ^= work;
        leftt ^= (work << 16);

        work   = ((right >>>  2) ^ leftt) & 0x33333333;
        leftt ^= work;
        right ^= (work << 2);

        work   = ((right >>>  8) ^ leftt) & 0x00ff00ff;
        leftt ^= work;
        right ^= (work << 8);
        right  = (right << 1) | ((right >>> 31) & 1);

        work   = (leftt ^ right) & 0xaaaaaaaa;
        leftt ^= work;
        right ^= work;
        leftt  = (leftt << 1) | ((leftt >>> 31) & 1);

        for ( round = 0; round < 8; ++round )  {
            work   = (right << 28) | (right >>> 4);
            work  ^= keys[keysi++];
            fval   = SP7[ work         & 0x0000003f ];
            fval  |= SP5[(work >>>  8) & 0x0000003f ];
            fval  |= SP3[(work >>> 16) & 0x0000003f ];
            fval  |= SP1[(work >>> 24) & 0x0000003f ];
            work   = right ^ keys[keysi++];
            fval  |= SP8[ work         & 0x0000003f ];
            fval  |= SP6[(work >>>  8) & 0x0000003f ];
            fval  |= SP4[(work >>> 16) & 0x0000003f ];
            fval  |= SP2[(work >>> 24) & 0x0000003f ];
            leftt ^= fval;
            work   = (leftt << 28) | (leftt >>> 4);
            work  ^= keys[keysi++];
            fval   = SP7[ work         & 0x0000003f ];
            fval  |= SP5[(work >>>  8) & 0x0000003f ];
            fval  |= SP3[(work >>> 16) & 0x0000003f ];
            fval  |= SP1[(work >>> 24) & 0x0000003f ];
            work   = leftt ^ keys[keysi++];
            fval  |= SP8[ work         & 0x0000003f ];
            fval  |= SP6[(work >>>  8) & 0x0000003f ];
            fval  |= SP4[(work >>> 16) & 0x0000003f ];
            fval  |= SP2[(work >>> 24) & 0x0000003f ];
            right ^= fval;
        }

        right  = (right << 31) | (right >>> 1);
        work   = (leftt ^ right) & 0xaaaaaaaa;
        leftt ^= work;
        right ^= work;

⌨️ 快捷键说明

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