DES加密解密 Java中運用

DES全稱Data Encryption Standard,是一種使用密匙加密的塊算法。如今認爲是一種不安全的加密算法,由於如今已經有用窮舉法攻破DES密碼的報道了。儘管如此,該加密算法仍是運用很是廣泛,是一種標準的加密算法。3DES是DES的增強版本。java

 

Des:算法

package com.blog.d201706.encrypt;

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


public class Des {


    /**
     * 加密
     * @param data
     * @param sKey
     * @return
     */
    public static byte[] encrypt(byte[] data, String sKey) {
        try {
            byte[] key = sKey.getBytes();
            // 初始化向量
            IvParameterSpec iv = new IvParameterSpec(key);
            DESKeySpec desKey = new DESKeySpec(key);
            // 建立一個密匙工廠,而後用它把DESKeySpec轉換成securekey
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            // Cipher對象實際完成加密操做
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            // 用密匙初始化Cipher對象
            cipher.init(Cipher.ENCRYPT_MODE, securekey, iv);
            // 如今,獲取數據並加密
            // 正式執行加密操做
            return cipher.doFinal(data);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     * @param src
     * @param sKey
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] src, String sKey) throws Exception {
        byte[] key = sKey.getBytes();
        // 初始化向量
        IvParameterSpec iv = new IvParameterSpec(key);
        // 建立一個DESKeySpec對象
        DESKeySpec desKey = new DESKeySpec(key);
        // 建立一個密匙工廠
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // 將DESKeySpec對象轉換成SecretKey對象
        SecretKey securekey = keyFactory.generateSecret(desKey);
        // Cipher對象實際完成解密操做
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        // 用密匙初始化Cipher對象
        cipher.init(Cipher.DECRYPT_MODE, securekey, iv);
        // 真正開始解密操做
        return cipher.doFinal(src);
    }

    /**
     * 將二進制轉換成16進制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 將16進制轉換爲二進制
     *
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
}
View Code

 

DesUtil:安全

package com.blog.d201706.encrypt;

import java.nio.charset.Charset;


public class DesUtil {

    /**
     * 加密
     * @param srcStr
     * @param charset
     * @param sKey
     * @return
     */
    public static String encrypt(String srcStr, Charset charset, String sKey) {
        byte[] src = srcStr.getBytes(charset);
        byte[] buf = Des.encrypt(src, sKey);
        return Des.parseByte2HexStr(buf);
    }

    /**
     * 解密
     *
     * @param hexStr
     * @param sKey
     * @return
     * @throws Exception
     */
    public static String decrypt(String hexStr, Charset charset, String sKey) throws Exception {
        byte[] src = Des.parseHexStr2Byte(hexStr);
        byte[] buf = Des.decrypt(src, sKey);
        return new String(buf, charset);
    }
}
View Code

MainApp:
package com.blog.d201706.encrypt;

import java.nio.charset.Charset;

public class MainApp {
    private static final String  SKEY    = "abcdefgh";
    private static final Charset CHARSET = Charset.forName("gb2312");

    public static void main(String[] args) {
        // 待加密內容
        String str = "測試內容,今天週四";
        String encryptResult = DesUtil.encrypt(str, CHARSET, SKEY);
        System.out.println(encryptResult);
        // 直接將如上內容解密
        String decryResult = "";
        try {
            decryResult = DesUtil.decrypt(encryptResult, CHARSET, SKEY);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        System.out.println(decryResult);
    }
}
View Code
相關文章
相關標籤/搜索