package com.dc; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; /* ******************** Class description ******************** * class : DcDESUtil * @author : ncc * create time : 2017-12-8 05:46:02 pm * @version : 1.0 * description : DES encryption and decryption class * The DES algorithm is a symmetric encryption system in the encryption system, and it has become the US data encryption standard. It was developed by IBM in 1972. * Symmetric cryptosystem encryption algorithm. The plaintext is grouped by 64 bits, the key is 64 bits long, and the key is actually 56 bits involved in the DES operation * (The 8th, 16th, 24th, 32nd, 40th, 48th, 56th, and 64th bits are the check digit, so that each key has an odd number of 1s) the grouped plaintext group * Encryption method that forms a ciphertext group by bitwise substitution or exchange with a 56-bit key * @see : * ************************************************/ public class DcDESUtil { private static final int ENCRYPT_MODE = 1; // encryption mode private static final int DECRYPT_MODE = 2; // decryption mode // key private static byte[] DEFKEY = "wPSnMdH3".getBytes(); /* ******************************************** * method name : enc_des * description : DES encryption method * @return : String encrypted ciphertext * @param : @param showInfo the plaintext information to be encrypted * @param : @return * modified : ncc , 2017-12-8 * @see : * ********************************************/ public static String enc_des(String showInfo){ byte[] result = null; try { byte[] tmp = showInfo.getBytes("GBK"); result = cipher(tmp, DEFKEY, ENCRYPT_MODE, "DES"); } catch (Exception e) { e.printStackTrace (); } return null == result ? null : b2hex(result); } /* ******************************************** * method name : dec_des * description : DES decryption method * @return : String decrypted plaintext * @param : @param encryInfo ciphertext to decrypt * @param : @return * @param : @throws IllegalBlockSizeException * @param : @throws IllegalArgumentException * modified : ncc , 2017-12-8 * @see : * ********************************************/ public static String dec_des(String encryInfo) throws IllegalBlockSizeException,IllegalArgumentException { byte[] result = null; String resultStr = null; try { try { byte[] tmp = hex2b(encryInfo); result = cipher(tmp,DEFKEY, DECRYPT_MODE, "DES"); resultStr = new String(result,"GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace (); } return resultStr; } catch (ArrayIndexOutOfBoundsException e) {//base64 throws array out of bounds throw new IllegalBlockSizeException(e.getMessage()); } catch (NullPointerException e) { throw new IllegalBlockSizeException(e.getMessage()); } } /* ******************************************** * method name : dec_des * description : des decryption method * @return : String * @param : @param encryInfo ciphertext * @param : @param key key * @param : @return * @param : @throws IllegalBlockSizeException * @param : @throws IllegalArgumentException * modified : ncc , 2017-12-8 * @see : * ********************************************/ public static String dec_des(String encryInfo, byte[] key) throws IllegalBlockSizeException,IllegalArgumentException { byte[] result = null; String resultStr = null; try { try { byte[] tmp = hex2b(encryInfo); result = cipher(tmp,key, DECRYPT_MODE, "DES"); resultStr = new String(result,"GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace (); } return resultStr; } catch (ArrayIndexOutOfBoundsException e) {//base64 throws array out of bounds throw new IllegalBlockSizeException(e.getMessage()); } catch (NullPointerException e) { throw new IllegalBlockSizeException(e.getMessage()); } } /* ******************************************** * method name : cipher * description : encryption and decryption tool method * @return : byte[] * @param : @param binfo original character array * @param : @param bkey key * @param : @param mode mode (encryption/decryption) * @param : @param transformation encryption and decryption method * @param : @return * @param : @throws IllegalBlockSizeException * modified : ncc , 2017-12-8 * @see : * ********************************************/ private static byte[] cipher(byte[] binfo, byte[] bkey, int mode, String transformation) throws IllegalBlockSizeException { String algs[] = transformation.split("/"); SecretKey key = new SecretKeySpec(bkey, algs[0]); Cipher cipher = null; byte[] result = null; try { cipher = Cipher.getInstance(transformation); cipher.init(mode, key); result = cipher.doFinal(binfo); } catch (NoSuchAlgorithmException e) { e.printStackTrace (); } catch (NoSuchPaddingException e) { e.printStackTrace (); } catch (InvalidKeyException e) { e.printStackTrace (); } catch (BadPaddingException e) { e.printStackTrace (); } return result; } /* ******************************************** * method name : b2hex * description : binary to hexadecimal * @return : String * @param : @param bs * @param : @return * modified : ncc , 2017-12-8 * @see : * ********************************************/ private static String b2hex(byte[] bs) { int iLen = bs.length; // Each byte can be represented by two characters, so the length of the string is twice the length of the array StringBuffer sb = new StringBuffer(iLen * 2); for (int i = 0; i < iLen; i++) { int intTmp = bs[i]; // Convert negative numbers to positive numbers while (intTmp < 0) { intTmp = intTmp + 256; } // Numbers less than 0F need to be prepended with 0 if (intTmp < 16) { sb.append("0"); } sb.append(Integer.toString(intTmp, 16)); } return sb.toString(); } /* ******************************************** * method name : hex2b * description : hex to binary * @return : byte[] * @param : @param str * @param : @return * @param : @throws UnsupportedEncodingException * modified : ncc , 2017-12-8 * @see : * ********************************************/ private static byte[] hex2b(String str) throws UnsupportedEncodingException { byte[] arrB = str.getBytes("GBK"); int iLen = arrB.length; // Two characters represent one byte, so the length of the byte array is the length of the string divided by 2 byte [] arrOut = new byte [iLen / 2]; for (int i = 0; i < iLen; i = i + 2) { String strTmp = new String(arrB, i, 2,"GBK"); arrOut [i / 2] = (byte) Integer.parseInt (strTmp, 16); } return arrOut; } public static void main(String[] args) { String str = "Welcome to Decao Home"; String strEnc = DcDESUtil.enc_des (str); System.out.println("str==" + str); System.out.println("strEnc==" + strEnc); try { String strdec = DcDESUtil.dec_des(strEnc); System.out.println("strdec==" + strdec); } catch (IllegalBlockSizeException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalArgumentException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { System.out.println("******************************"); System.out.println(DcDESUtil.dec_des("747afb83ae979a5ef5d0f5742b4a72184774773ec4fc58b3", "lSbEGkW7".getBytes())); } catch (IllegalBlockSizeException e) { e.printStackTrace (); } catch (IllegalArgumentException e) { System.out.println(e); } } }