Java☞DES加解密算法简介及实现

846 查看

DES加密介绍
DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法。DES加密算法出自IBM的研究,后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少,因为DES使用56位密钥,以现代计算能力,24小时内即可被破解。虽然如此,在某些简单应用中,我们还是可以使用DES加密算法,本文简单讲解DES的JAVA实现。

DES加密算法实现:
package com.test;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class Test {

private final static String DES = "DES";

/**
 * @param args
 */
public static void main(String[] args) {

    String encryptString= encrypt("is米食米客","wozhidao米食米客对方蜂蜜@qqcomc大幅度的deed");
    System.out.println(encryptString);
}

public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
    // DES算法要求有一个可信任的随机数源
    SecureRandom sr = new SecureRandom();

    // 从原始密匙数据创建DESKeySpec对象
    DESKeySpec dks = new DESKeySpec(key);

    // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);

    // Cipher对象实际完成加密操作
    Cipher cipher = Cipher.getInstance(DES);

    // 用密匙初始化Cipher对象
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

    // 执行加密操作
    return cipher.doFinal(src);
}

public final static String encrypt(String password, String key) {

    try {
        return byte2String(encrypt(password.getBytes(), key.getBytes()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

private static String byte2String(byte[] b) {
    String hs="";
    String stmp="";
    for(int n=0;n<b.length;n++){
        stmp=(java.lang.Integer.toHexString(b[n]&0XFF));
        if(stmp.length() == 1)
            hs+=hs+"0"+stmp;
        else
            hs=hs+stmp;
    }
    return hs.toUpperCase();
}

//输出 :3C6261D7B9B6E3BCF647C412382CFCF0

DES解密算法实现:

package com.test;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DECtest {

 private final static String DES = "DES";

    /**
     *
     * @param src 数据源
     * @param key 密钥,长度必须是8的倍数
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
        // DES算法要求有一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        // 从原始密匙数据创建一个DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);
        // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成一个SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(DES);
        // 用密匙初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        // 正式执行解密操作
        return cipher.doFinal(src);
    }

    public final static String decrypt(String data, String key) {
        try {
            return new String(decrypt(String2byte(data.getBytes()), key.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] String2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException("长度不是偶数");
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }

    public static void main(String[] args){
        String desencryptString = decrypt("3C6261D7B9B6E3BCF647C412382CFCF0","wozhidao米食米客对方蜂蜜@qqcomc大幅度的deed");
        System.out.println(desencryptString);
    }
    }
}
    //输出:is米食米客