DES 双向加密
2016-05-18 14:58
211 查看
public class DESUtil { /** * 日志输出对象 */ public static final Log log = LogFactory.getLog(DESUtil.class); /** * 加密逻辑方法 * @param message * @param key * @return * @throws Exception */ private static byte[] encryptProcess(String message, String key) throws Exception { Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); return cipher.doFinal(message.getBytes("UTF-8")); } /** * 解密逻辑方法 * @param message * @param key * @return * @throws Exception */ private static String decryptProcess(String message,String key) throws Exception { byte[] bytesrc =convertHexString(message); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); byte[] retByte = cipher.doFinal(bytesrc); return new String(retByte); } /** * 16进制数组数转化 * @param ss * @return */ private static byte[] convertHexString(String ss) throws Exception { byte digest[] = new byte[ss.length() / 2]; for(int i = 0; i < digest.length; i++) { String byteString = ss.substring(2 * i, 2 * i + 2); int byteValue = Integer.parseInt(byteString, 16); digest[i] = (byte)byteValue; } return digest; } /** * 十六进制数转化 * @param b * @return * @throws Exception */ private static String toHexString(byte b[]) throws Exception { StringBuffer hexString = new StringBuffer(); for (int i = 0; i < b.length; i++) { String plainText = Integer.toHexString(0xff & b[i]); if (plainText.length() < 2) plainText = "0" + plainText; hexString.append(plainText); } return hexString.toString(); } /** * 加密方法 */ public static String encrypt(String message,String key){ log.info("加密原串为:" + message); String enStr = null; try { String orignStr=java.net.URLEncoder.encode(message, "utf-8"); enStr=toHexString(encryptProcess(orignStr, key)); } catch (Exception e) { log.error("参数加密异常!", e); } return enStr; } /** * 解密方法 */ public static String decrypt(String message,String key){ String decStr = null; try { // log.debug("解密串:" + message); // log.debug("解密KEY:" + key); decStr = java.net.URLDecoder.decode(decryptProcess(message,key), "utf-8") ; // log.info("参数解密结果:" + decStr); }catch (Exception e) { log.error("参数解密异常!", e); } return decStr; } }
相关文章推荐
- Socket编程------TCP文件传输(文档、声音、图片、视频和压缩包等)
- Qt使用QSplitter实现分割窗口
- hdu3415Max Sum of Max-K-sub-sequence(单调队列)
- WEB开发报错集合2
- JavaScript中getBoundingClientRect()方法详解
- 利用Code128字体将文本转换为code128条形码
- RecyclerView的使用
- iOSCoreAnimation动画系列教程(一):CABasicAnimation【包会】
- 地图索引 R-tree
- AsyncTask实现多任务多线程断点续传下载
- iOS创建framework,并使用cocoapods管理
- Java——反射(1.动态加载类)
- 国内领先的容器云平台和解决方案提供商有哪些
- 小飞机游戏C语言程序&划拳
- 使用commons-fileupload和commoms-ioJar上传文件到服务器
- 如何判断一个Unicode表示的字符为一个中文字
- Android之网络学习资源汇集(更新)
- iOSm转化Km
- Linux 查看系统硬件信息(实例详解)
- linux下生成bmp图片的测试程序