基于java数组的DES加密、解密的算法实现

      这是数据加密标准(DES)算法实现,网上有很多C/C++版本的,特写一个JAVA版本的,主要是想练练自己JAVA的编程能力,如果有不妥当的地方希望大家能指正,或者给出更好的建议,其实现代码如下:

/**
 *
 * @author Davy Chen
 */
public class DES {
    //初始换位IP表

    byte IP[] = {
        58, 50, 42, 34, 26, 18, 10, 2,
        60, 52, 44, 36, 28, 20, 12, 4,
        62, 54, 46, 38, 30, 22, 14, 6,
        64, 56, 48, 40, 32, 24, 16, 8,
        57, 49, 41, 33, 25, 17, 9, 1,
        59, 51, 43, 35, 27, 19, 11, 3,
        61, 53, 45, 37, 29, 21, 13, 5,
        63, 55, 47, 39, 31, 23, 15, 7
    };
    //逆初始换位IP^-1表
    byte IIP[] = {
        40, 8, 48, 16, 56, 24, 64, 32,
        39, 7, 47, 15, 55, 23, 63, 31,
        38, 6, 46, 14, 54, 22, 62, 30,
        37, 5, 45, 13, 53, 21, 61, 29,
        36, 4, 44, 12, 52, 20, 60, 28,
        35, 3, 43, 11, 51, 19, 59, 27,
        34, 2, 42, 10, 50, 18, 58, 26,
        33, 1, 41, 9, 49, 17, 57, 25
    };
    //密钥置换PC-1表
    byte PC_1[] = {
        57, 49, 41, 33, 25, 17, 9,
        1, 58, 50, 42, 34, 26, 18,
        10, 2, 59, 51, 43, 35, 27,
        19, 11, 3, 60, 52, 44, 36,
        63, 55, 47, 39, 31, 23, 15,
        7, 62, 54, 46, 38, 30, 22,
        14, 6, 61, 53, 45, 37, 29,
        21, 13, 5, 28, 20, 12, 4
    };
    //密钥置换PC-2表
    byte PC_2[] = {
        14, 17, 11, 24, 1, 5,
        3, 28, 15, 6, 21, 10,
        23, 19, 12, 4, 26, 8,
        16, 7, 27, 20, 13, 2,
        41, 52, 31, 37, 47, 55,
        30, 40, 51, 45, 33, 48,
        44, 49, 39, 56, 34, 53,
        46, 42, 50, 36, 29, 32
    };
    //E的选位表
    byte[] E_box = {
        32, 1, 2, 3, 4, 5,
        4, 5, 6, 7, 8, 9,
        8, 9, 10, 11, 12, 13,
        12, 13, 14, 15, 16, 17,
        16, 17, 18, 19, 20, 21,
        20, 21, 22, 23, 24, 25,
        24, 25, 26, 27, 28, 29,
        28, 29, 30, 31, 32, 1
    };
    //DES的中的S-Boxes
    byte[][][] S_box = {
        {
            {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
            {0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
            {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
            {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}
        },
        {
            {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
            {3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
            {0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
            {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}
        },
        {
            {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
            {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
            {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
            {1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}
        },
        {
            {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
            {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
            {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
            {3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}
        },
        {
            {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
            {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
            {4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
            {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}
        },
        {
            {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
            {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
            {9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
            {4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}
        },
        {
            {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
            {13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
            {1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
            {6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}
        },
        {
            {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
            {1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
            {7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
            {2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}
        }
    };
    //S-Box之后的32位置换P表
    byte[] P_box = {
        16, 7, 20, 21,
        29, 12, 28, 17,
        1, 15, 23, 26,
        5, 18, 31, 10,
        2, 8, 24, 14,
        32, 27, 3, 9,
        19, 13, 30, 6,
        22, 11, 4, 25
    };
    byte[][] Lpart = new byte[4][8];
    byte[][] Rpart = new byte[4][8];
    //声明一个三维数组用来存储加密过程产生的16个密码
    byte[][][] saveKey = new byte[16][8][6];
    //保存加密后的二进制密文
    //public byte[][] encryptTextBits;

    //初始置换IP
    private byte[][] initailDisplacementIP(byte[][] in) {
        byte[][] out = new byte[8][8];
        for (int i = 0; i < 64; i++) {
            out[i / 8][i % 8] = in[(IP[i] - 1) / 8][(IP[i] - 1) % 8];
        }
        //System.arraycopy(out, 0, in, 0, 8);//源数组中位置在 srcPos 到 srcPos+length-1 之间的组件被分别复制到目标数组中的 destPos 到 destPos+length-1 位置
        return out;
    }//ip结束

    public byte[][][] generateKey(byte[] key) {
        //压缩变换PC-1后分割的结果C、D
        byte[][] C = new byte[4][7];
        byte[][] D = new byte[4][7];
        //生成子密钥
        byte[][] bitArray2 = new byte[8][8];
        for (int i = 0; i < 8; i++) {
            bitArray2[i] = Change.byteToBitArray(key[i]);
        }
        //压缩变换PC-1
        bitArray2 = compressionTransformPC_1(bitArray2);
        //密钥分割
        // 将bitArray2经过PC1后的数组拆分成2组
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 7; j++) {
                if (i < 4) {
                    C[i][j] = bitArray2[i][j];
                } else {
                    D[i - 4][j] = bitArray2[i][j];
                }
            }
        }
        for (int i = 0; i < 16; i++) {
            //向左移1位的情况
            if (i == 0 || i == 1 || i == 8 || i == 15) {
                leftShift(C);
                leftShift(D);
            } //向左移动2位的情况
            else {
                leftShift(C);
                leftShift(C);
                leftShift(D);
                leftShift(D);
            }
            //在一个数组中存储K1-K16以便解密使用
            saveKey[i] = compressionTransformPC_2(C, D);
        }
        return saveKey;
    }

    /**
     * DES加密
     * @param plainText
     * @param key
     */
    public String encrypt(byte[] plainText, byte[][][] key) {
        //将byteArray中的位取出并扩展成字节放在bitArray中
        byte[][] bitArray = new byte[8][8];
        for (int i = 0; i < 8; i++) {
            bitArray[i] = Change.byteToBitArray(plainText[i]);
        }
        //完成初始置换IP
        bitArray = initailDisplacementIP(bitArray);
        //将bitArray分配给2个数组Lpart和Rpart
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (i < 4) {
                    Lpart[i][j] = bitArray[i][j];
                } else {
                    Rpart[i - 4][j] = bitArray[i][j];
                }
            }
        }
        byte[][] temp;
        //进行16轮的加密变换
        for (int i = 0; i < 16; i++) {
            // 用临时数组temp保存明文右半部分
            temp = (byte[][]) Rpart.clone();
            XOR(Lpart, f(Rpart, key[i]));
            Rpart = Lpart;
            Lpart = temp;
        }
        // 左右两部分合并然后最终置换,由于最后一次循环之后左边和右边进行了一次交换,所以这里参数的顺序是Rpart在前Lpart在后
        byte[][] encryptTextBits = contraryInitailDisplacementIIP(Rpart, Lpart);
        //  转化为字节,然后转化为字符
        byte[] out = new byte[8];
        for (int i = 0; i < 8; i++) {
            out[i] = Change.bitToByteArray(encryptTextBits[i]);
        }
        //输出16进制形式的字符串
        return Change.bytesToHexString(out);
    }

    /**
     * DES解密
     * @param plainText
     * @param key
     */
    public String decrypt(byte[] encryptText, byte[][][] key) {
        //将byteArray中的位取出并扩展成字节放在bitArraye中
        byte[][] bitArray = new byte[8][8];
        for (int i = 0; i < 8; i++) {
            bitArray[i] = Change.byteToBitArray(encryptText[i]);
        }
        //完成初始置换IP
        bitArray = initailDisplacementIP(bitArray);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 8; j++) {
                Rpart[i][j] = bitArray[i][j];
                Lpart[i][j] = bitArray[i + 4][j];
            }
        }
        byte[][] temp;
        for (int i = 15; i >= 0; i--) {
            temp = (byte[][]) Lpart.clone();
            XOR(Rpart, f(Lpart, key[i]));
            Lpart = Rpart;
            Rpart = temp;
        }
        byte[][] result = contraryInitailDisplacementIIP(Lpart, Rpart);
        //转化为字节,然后转化为字符
        byte[] out = new byte[8];
        for (int i = 0; i < 8; i++) {
            out[i] = Change.bitToByteArray(result[i]);
        }
        //输出16进制形式的字符串
        return Change.bytesToHexString(out);
    }

    public static void main(String[] args) {
        DES des = new DES();
        //二进制形式的原文
        byte[][] plainTextBit = {
            {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01},
            {(byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01},
            {(byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01},
            {(byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x01},
            {(byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x01},
            {(byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x01},
            {(byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x01},
            {(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01}
        };
        //二进制形式的密钥
        byte[][] keyBit = {
            {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01},
            {(byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00},
            {(byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x01},
            {(byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x01},
            {(byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x01},
            {(byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00},
            {(byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01},
            {(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01}
        };
        //转换为byte数组
        byte[] plainTextByte = new byte[8];
        byte[] keyByte = new byte[8];
        for (int i = 0; i < 8; i++) {
            plainTextByte[i] = Change.bitToByteArray(plainTextBit[i]);
            keyByte[i] = Change.bitToByteArray(keyBit[i]);
        }
        byte[][][] key = des.generateKey(keyByte);
        byte[] plainText2 = {(byte) 0x11, (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17, (byte) 0x18};
        System.out.println("原文1为:" + Change.bytesToHexString(plainTextByte));
        System.out.println("原文2为:" + Change.bytesToHexString(plainText2));

        //输出密文
        String encrypt = des.encrypt(plainTextByte, key);
        String encrypt2 = des.encrypt(plainText2, key);
        System.out.println("encrypt1=" + encrypt);
        System.out.println("encrypt2=" + encrypt2);
        byte[] encryptByte = Change.hexStringToBytes(encrypt);
        byte[] encryptByte2 = Change.hexStringToBytes(encrypt2);
        //输出原文
        System.out.println("decrypt1=" + des.decrypt(encryptByte, key));
        System.out.println("decrypt2=" + des.decrypt(encryptByte, key));
        System.out.println("decrypt2=" + des.decrypt(encryptByte2, key));
    }

    /**
     * PC-1压缩变换
     */
    private byte[][] compressionTransformPC_1(byte[][] in) {
        byte[][] temp = new byte[8][7];
        for (int i = 0; i < 56; i++) {
            temp[i / 7][i % 7] = in[(PC_1[i] - 1) / 8][(PC_1[i] - 1) % 8];
        }
        return temp;
    }

    /**
     * 左移函数
     * @param C
     */
    private void leftShift(byte[][] b) {
        byte temp = b[0][0];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 6; j++) {
                b[i][j] = b[i][j + 1];
                if (i == 3) {
                    continue;
                }
                if (j == 5) {
                    b[i][6] = b[i + 1][0];
                }
            }
        }
        b[3][6] = temp;
    }

    /**
     * PC-2压缩变换
     * @param C
     * @param D
     * @return
     */
    private byte[][] compressionTransformPC_2(byte[][] C, byte[][] D) {
        byte[][] out = new byte[8][6];
        byte[][] temp = new byte[8][7];
        //将C和D合并56位的
        for (int i = 0; i < 28; i++) {
            temp[i / 7][i % 7] = C[i / 7][i % 7];
            temp[i / 7 + 4][i % 7] = D[i / 7][i % 7];
        }
        for (int i = 0; i < 48; i++) {
            out[i / 6][i % 6] = temp[(PC_2[i] - 1) / 7][(PC_2[i] - 1) % 7];
        }
        return out;
    }

    private byte[][] f(byte[][] rPart, byte[][] key) {
        byte[][] temp;
        //32位扩展到48位
        rPart = EBox(rPart);
        XOR(rPart, key);
        temp = Sbox(rPart);
        return Pbox(temp);
    }

    /**
     * 异或
     * @param a
     * @param b
     */
    private void XOR(byte[][] a, byte[][] b) {
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                a[i][j] ^= b[i][j];
            }
        }
    }

    /**
     * E-盒置换
     * @param in
     * @return
     */
    private byte[][] EBox(byte[][] in) {
        byte[][] out = new byte[8][6];
        for (int i = 0; i < 48; i++) {
            out[i / 6][i % 6] = in[(E_box[i] - 1) / 8][(E_box[i] - 1) % 8];
        }
        return out;
    }

    /**
     * S-盒替代
     * @param rPart
     * @return
     */
    private byte[][] Sbox(byte[][] in) {
        byte[] temp = new byte[8];
        byte[][] out = new byte[8][4];
        //行号,列号
        int m, n;
        for (int i = 0; i < 8; i++) {
            m = in[i][0] * 2 + in[i][5];
            n = in[i][1] * 8 + in[i][2] * 4 + in[i][3] * 2 + in[i][4];
            temp[i] = (byte) S_box[i][m][n];
        }
        for (int i = 0; i < 8; i++) {
            for (int j = 3; j >= 0; j--) {
                out[i][j] = (byte) (temp[i] & 0x01);
                temp[i] >>= 1;
            }
        }
        return out;
    }

    private byte[][] Pbox(byte[][] in) {
        byte[][] temp = new byte[4][8];
        for (int i = 0; i < 32; i++) {
            temp[i / 8][i % 8] = in[(P_box[i] - 1) / 4][(P_box[i] - 1) % 4];
        }
        return temp;
    }

    private byte[][] contraryInitailDisplacementIIP(byte[][] Rpart, byte[][] Lpart) {
        byte[][] out = new byte[8][8];
        byte[][] temp = new byte[8][8];
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 8; j++) {
                temp[i][j] = Rpart[i][j];
                temp[i + 4][j] = Lpart[i][j];
            }
        }
        for (int i = 0; i < 64; i++) {
            out[i / 8][i % 8] = temp[(IIP[i] - 1) / 8][(IIP[i] - 1) % 8];
        }
        return out;
    }
}

你可能感兴趣的:(数据结构与算法java实现)