Chinaunix首页 | 论坛 | 博客
  • 博客访问: 338305
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1134
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-25 09:41
个人简介

目前任职于搜狐~

文章分类

全部博文(31)

文章存档

2014年(12)

2013年(19)

分类: Java

2013-10-17 17:47:32

java实现DES加密算法:

点击(此处)折叠或打开

  1. package com.jiexun.ers.utils;

  2. import javax.crypto.Cipher;
  3. import javax.crypto.SecretKey;
  4. import javax.crypto.SecretKeyFactory;
  5. import javax.crypto.spec.DESKeySpec;
  6. import javax.crypto.spec.IvParameterSpec;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;

  9. /**
  10.  *
  11.  * FileName:EncryptUtil.java                
  12.  *             
  13.  * Description:DES加密解密工具类
  14.  *
  15.  */
  16. public class EncryptUtil {
  17.     
  18.     /**
  19.      * 日志输出对象
  20.      */
  21.     public static final Log log = LogFactory.getLog(EncryptUtil.class);
  22.     
  23.     /**
  24.      * 加密逻辑方法
  25.      * @param message
  26.      * @param key
  27.      * @return
  28.      * @throws Exception
  29.      */
  30.     private static byte[] encryptProcess(String message, String key) throws Exception {
  31.         Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  32.         DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
  33.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  34.         SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
  35.         IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
  36.         cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
  37.         return cipher.doFinal(message.getBytes("UTF-8"));
  38.     }
  39.     
  40.     /**
  41.      * 解密逻辑方法
  42.      * @param message
  43.      * @param key
  44.      * @return
  45.      * @throws Exception
  46.      */
  47.     private static String decryptProcess(String message,String key) throws Exception {
  48.             byte[] bytesrc =convertHexString(message);
  49.             Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  50.             DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
  51.             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  52.             SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
  53.             IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
  54.             cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
  55.             byte[] retByte = cipher.doFinal(bytesrc);
  56.             return new String(retByte);
  57.     }
  58.     
  59.     /**
  60.      * 16进制数组数转化
  61.      * @param ss
  62.      * @return
  63.      */
  64.     private static byte[] convertHexString(String ss) throws Exception {
  65.         byte digest[] = new byte[ss.length() / 2];
  66.         for(int i = 0; i < digest.length; i++)
  67.         {
  68.         String byteString = ss.substring(2 * i, 2 * i + 2);
  69.         int byteValue = Integer.parseInt(byteString, 16);
  70.         digest[i] = (byte)byteValue;
  71.         }
  72.         return digest;
  73.     }
  74.     
  75.     /**
  76.      * 十六进制数转化
  77.      * @param b
  78.      * @return
  79.      * @throws Exception
  80.      */
  81.     private static String toHexString(byte b[]) throws Exception {
  82.         StringBuffer hexString = new StringBuffer();
  83.         for (int i = 0; i < b.length; i++) {
  84.             String plainText = Integer.toHexString(0xff & b[i]);
  85.             if (plainText.length() < 2)
  86.                 plainText = "0" + plainText;
  87.             hexString.append(plainText);
  88.         }
  89.            
  90.         return hexString.toString();
  91.     }
  92.     
  93.     /**
  94.      * 加密方法
  95.      */
  96.     public static String encrypt(String message,String key){
  97.         log.info("加密原串为:" + message);
  98.         String enStr = null;
  99.         try {
  100.              String orignStr=java.net.URLEncoder.encode(message, "utf-8");
  101.              enStr=toHexString(encryptProcess(orignStr, key));
  102.         } catch (Exception e) {
  103.             log.error("参数加密异常!", e);
  104.         }
  105.         return enStr;
  106.     }
  107.     
  108.     
  109.     /**
  110.      * 解密方法
  111.      */
  112.     public static String decrypt(String message,String key){
  113.         String decStr = null;
  114.         try {
  115.             log.debug("解密串:" + message);
  116.             log.debug("解密KEY:" + key);
  117.             decStr = java.net.URLDecoder.decode(decryptProcess(message,key), "utf-8") ;
  118.             log.info("参数解密结果:" + decStr);
  119.         }catch (Exception e) {
  120.             log.error("参数解密异常!", e);
  121.         }
  122.         return decStr;
  123.     }
  124.     
  125.     /**
  126.      * 测试Main方法
  127.      * @param args
  128.      * @throws Exception
  129.      */
  130.     public static void main(String[] args) throws Exception {
  131.          String key = "12345678"; //密码只能为8位
  132.          String message ="sign=026ec875336739dee0132d39e8361f10";
  133.          String enStr = encrypt(message,key);
  134.          System.out.println("加密后:" + enStr);
  135.          String decStr = decrypt(enStr,key);
  136.          System.out.println("解密后:" + decStr);
  137.     }
  138. }

阅读(9137) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~