1. 程式人生 > >Des加密工具類

Des加密工具類


import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.security.SecureRandom;

public class DesUtils {
    public final static String DES = "DES";
    public final static String KEY = "123abc!@#$%";

    public static void main(String[] args) throws Exception {

        String s1 = DesUtils.encrypt("7,123456,154232323232323232323", KEY);
        String s2 = DesUtils.decrypt(s1, KEY);
        String s3 = DesUtils.encrypt("maechannel", KEY);
        String s4 = DesUtils.decrypt(s3, KEY);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
        System.out.println(DesUtils.decrypt("1okOaQkxizembyTkJpRAzlDsCIEu9K6tcWp8eSUw8cM=",KEY));

    }

    /**
     * Description 根據鍵值進行加密
     *
     * @param data
     * @param key  加密鍵byte陣列
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
        byte[] bt = encrypt(data.getBytes(), key.getBytes());
/*        String strs = new BASE64Encoder().encode(bt);*/
        String strs = new Base64().encodeBase64String(bt);
        return strs;
    }

    /**
     * Description 根據鍵值進行解密
     *
     * @param data
     * @param key  加密鍵byte陣列
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException, Exception {
        if (data == null)
            return null;
        //BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = new Base64().decodeBase64(data);
        byte[] bt = decrypt(buf, key.getBytes());
        return new String(bt);
    }

    /**
     * Description 根據鍵值進行加密
     *
     * @param data
     * @param key  加密鍵byte陣列
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 生成一個可信任的隨機數源
        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(data);
    }

    /**
     * Description 根據鍵值進行解密
     *
     * @param data
     * @param key  加密鍵byte陣列
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一個可信任的隨機數源
        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(data);
    }
}

看了網上的自己改了一下,BASE64Encoder改成了apache的Base64工具類,BASE64Encoder這個類在後面的版本會淘汰,注意點:

最好用

new Base64().encodeBase64String,
new Base64().decodeBase64
方法,如果直接用new Base64().encode,new Base64().decode方法會報

des解密時,如果加密資料不是8的整數倍 異常
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher異常,為了解決該為題,將資料加密後,再進行base64進行加密,解密時首先通過base64進行解密