java加密算法之DES篇

閒話很少說,咱們直接上乾貨java

加密
public static String encrypt(String datasource) {
     try{
         DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
         //建立一個密匙工廠,獲取secretKey
         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
         SecretKey secretKey = keyFactory.generateSecret(desKey);
         //指定獲取DES的Cipher對象
         Cipher cipher = Cipher.getInstance("DES");
         //用密匙初始化Cipher對象
         cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom());
         //數據加密
         return parseByte2HexStr(cipher.doFinal(datasource.getBytes("utf-8")));
     }catch(Throwable e){
         e.printStackTrace();
     }
     return null;
 }
解密
public static String decrypt(String src){
    try{
        // 建立一個DESKeySpec對象,PASSWORD可任意指定
        DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
        // 建立一個密匙工廠
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // 生成密鑰
        SecretKey secretkey = keyFactory.generateSecret(desKey);
        // 指定獲取DES的Cipher對象
        Cipher cipher = Cipher.getInstance("DES");
        // 用密匙初始化Cipher對象
        cipher.init(Cipher.DECRYPT_MODE, secretkey, new SecureRandom());
        // 真正開始解密操做
        return new String(cipher.doFinal(parseHexStr2Byte(src)));
    }catch(Throwable e){
        e.printStackTrace();
    }
    return null;
}

測試

public static void main(String[] args) {
      String str = "I'm encode content";

      String result = DesUtils.encrypt(str);
      System.out.println("encode result is :"+result);
      //直接將如上內容解密
      try {
          String decryResult = DesUtils.decrypt(result);
          System.out.println("Decode result is :"+decryResult);
      } catch (Exception e1) {
          e1.printStackTrace();
      }
 }

測試結果

在這裏插入圖片描述

全代碼
public class DesUtils {


    private static final String PASSWORD = "9588028820109132570743325311898426347857298773549468758875018579537757772163084478873699447306034466200616411960574122434059469100235892702736860872901247123456";

    public DesUtils() {
    }
    
    /** * 加密 * @param datasource * @return */
    public static String encrypt(String datasource) {
        try{
            DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
            //建立一個密匙工廠,獲取secretKey
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKey);
            //指定獲取DES的Cipher對象
            Cipher cipher = Cipher.getInstance("DES");
            //用密匙初始化Cipher對象
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom());
            //數據加密
            return parseByte2HexStr(cipher.doFinal(datasource.getBytes("utf-8")));
        }catch(Throwable e){
            e.printStackTrace();
        }
        return null;
    }
    /** * 解密 * @param src * @return */
    public static String decrypt(String src){
        try{
            // 建立一個DESKeySpec對象,PASSWORD可任意指定
            DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
            // 建立一個密匙工廠
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            // 生成密鑰
            SecretKey secretkey = keyFactory.generateSecret(desKey);
            // 指定獲取DES的Cipher對象
            Cipher cipher = Cipher.getInstance("DES");
            // 用密匙初始化Cipher對象
            cipher.init(Cipher.DECRYPT_MODE, secretkey, new SecureRandom());
            // 真正開始解密操做
            return new String(cipher.doFinal(parseHexStr2Byte(src)));
        }catch(Throwable e){
            e.printStackTrace();
        }
        return null;
    }

    /**將二進制轉換成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;
    }

}

>>>源碼下載連接>>>git

相關文章
相關標籤/搜索