参考地址:
http://scottmaxiao.github.io/Java%E5%8A%A0%E5%AF%86%E7%AE%97%E6%B3%95-DES.html
http://blog.csdn.net/rj042/article/details/8196125
完成byte[] String转换
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; public class DesUtils { private static final String DES_ALGORITHM = "DES"; private static Logger logger = LoggerFactory.getLogger("util"); public static String encrypt(String plainData, String secretKey) throws Exception{ Cipher cipher = null; try { cipher = Cipher.getInstance(DES_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, generateKey(secretKey)); } catch (NoSuchAlgorithmException e) { logger.error(String.valueOf(e.getStackTrace())); } catch (NoSuchPaddingException e) { logger.error(String.valueOf(e.getStackTrace())); } try { byte[] buf = cipher.doFinal(plainData.getBytes()); return byteArr2HexStr(buf); } catch (IllegalBlockSizeException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("IllegalBlockSizeException", e); } catch (BadPaddingException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("BadPaddingException", e); } } public static String decrypt(String secretData, String secretKey) throws Exception{ Cipher cipher = null; try { cipher = Cipher.getInstance(DES_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, generateKey(secretKey)); } catch (NoSuchAlgorithmException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("NoSuchAlgorithmException", e); } catch (NoSuchPaddingException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("NoSuchPaddingException", e); }catch(InvalidKeyException e){ logger.error(String.valueOf(e.getStackTrace())); throw new Exception("InvalidKeyException", e); } try { byte[] buf = cipher.doFinal(hexStr2ByteArr(secretData)); return new String(buf); } catch (IllegalBlockSizeException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("IllegalBlockSizeException", e); } catch (BadPaddingException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("BadPaddingException", e); } } private static String byteArr2HexStr(byte[] arrB) throws Exception { int iLen = arrB.length; StringBuffer sb = new StringBuffer(iLen * 2); for (int i = 0; i < iLen; i++) { int intTmp = arrB[i]; while (intTmp < 0) { intTmp = intTmp + 256; } if (intTmp < 16) { sb.append("0"); } sb.append(Integer.toString(intTmp, 16)); } return sb.toString(); } private static byte[] hexStr2ByteArr(String strIn) throws Exception { byte[] arrB = strIn.getBytes(); int iLen = arrB.length; byte[] arrOut = new byte[iLen / 2]; for (int i = 0; i < iLen; i = i + 2) { String strTmp = new String(arrB, i, 2); arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); } return arrOut; } private static SecretKey generateKey(String secretKey) throws NoSuchAlgorithmException,InvalidKeyException,InvalidKeySpecException{ SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM); DESKeySpec keySpec = new DESKeySpec(secretKey.getBytes()); keyFactory.generateSecret(keySpec); return keyFactory.generateSecret(keySpec); } }
使用base64转换
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; public class DESUtils { private static final String DES_ALGORITHM = "DES"; private static Logger logger = LoggerFactory.getLogger("util"); public static String encrypt(String plainData, String secretKey) throws Exception{ Cipher cipher = null; try { cipher = Cipher.getInstance(DES_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, generateKey(secretKey)); } catch (NoSuchAlgorithmException e) { logger.error(String.valueOf(e.getStackTrace())); } catch (NoSuchPaddingException e) { logger.error(String.valueOf(e.getStackTrace())); } try { byte[] buf = cipher.doFinal(plainData.getBytes()); // return byteArr2HexStr(buf); return new BASE64Encoder().encode(buf); } catch (IllegalBlockSizeException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("IllegalBlockSizeException", e); } catch (BadPaddingException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("BadPaddingException", e); } } public static String decrypt(String secretData, String secretKey) throws Exception{ Cipher cipher = null; try { cipher = Cipher.getInstance(DES_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, generateKey(secretKey)); } catch (NoSuchAlgorithmException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("NoSuchAlgorithmException", e); } catch (NoSuchPaddingException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("NoSuchPaddingException", e); }catch(InvalidKeyException e){ logger.error(String.valueOf(e.getStackTrace())); throw new Exception("InvalidKeyException", e); } try { //byte[] buf = cipher.doFinal(hexStr2ByteArr(secretData)); byte[] buf = cipher.doFinal(new BASE64Decoder().decodeBuffer(secretData)); return new String(buf); } catch (IllegalBlockSizeException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("IllegalBlockSizeException", e); } catch (BadPaddingException e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("BadPaddingException", e); } catch (Exception e) { logger.error(String.valueOf(e.getStackTrace())); throw new Exception("Exception", e); } } private static SecretKey generateKey(String secretKey) throws NoSuchAlgorithmException,InvalidKeyException,InvalidKeySpecException{ SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM); DESKeySpec keySpec = new DESKeySpec(secretKey.getBytes()); keyFactory.generateSecret(keySpec); return keyFactory.generateSecret(keySpec); } }