des加密算法

参考网上的朋友的,自己根据自己的实际情况做了一些小改动。
  1 package com.vimtech.bms.webchat.commom.util;
  2 
  3 
  4 import org.apache.commons.codec.binary.Base64;
  5 
  6 import javax.crypto.Cipher;
  7 import javax.crypto.SecretKey;
  8 import javax.crypto.SecretKeyFactory;
  9 import javax.crypto.spec.DESKeySpec;
 10 import java.io.IOException;
 11 import java.security.SecureRandom;
 12 //@^@代替+ 防止+参数传递变成空格
 13 public class DESUtil {
 14 
 15     private final static String DES = "DES";
 16     private final static String key = "abc";
 17     /**
 18      * Description 根据键值进行加密
 19      * @return
 20      * @throws Exception
 21      */
 22     public static String encryptjm(String data) throws Exception {
 23         byte[] bt = encrypt(data.getBytes("utf-8"), key.getBytes());
 24         String strs = Base64.encodeBase64String(bt);
 25         strs = strs.replaceAll("\\+","u0049");
 26         return strs;
 27     }
 28     /**
 29      * Description 根据键值进行解密
 30      *
 31      * @param data
 32      *            加密键byte数组
 33      * @return
 34      * @throws IOException
 35      * @throws Exception
 36      */
 37     public static String decryptjm(String data) throws IOException, Exception {
 38         if (data == null)
 39             return null;
 40         data = data.replaceAll("u0049","\\+");
 41         byte[] buf = Base64.decodeBase64(data);
 42         byte[] bt = decrypt(buf, key.getBytes());
 43         return new String(bt,"utf-8");
 44     }
 45 
 46     /**
 47      * Description 根据键值进行加密
 48      *
 49      * @param data
 50      * @param key
 51      *            加密键byte数组
 52      * @return
 53      * @throws Exception
 54      */
 55     private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
 56         // 生成一个可信任的随机数源
 57         SecureRandom sr = new SecureRandom();
 58 
 59         // 从原始密钥数据创建DESKeySpec对象
 60         DESKeySpec dks = new DESKeySpec(key);
 61 
 62         // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
 63         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
 64         SecretKey securekey = keyFactory.generateSecret(dks);
 65 
 66         // Cipher对象实际完成加密操作
 67         Cipher cipher = Cipher.getInstance(DES);
 68 
 69         // 用密钥初始化Cipher对象
 70         cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
 71 
 72         return cipher.doFinal(data);
 73     }
 74 
 75     /**
 76      * Description 根据键值进行解密
 77      *
 78      * @param data
 79      * @param key
 80      *            加密键byte数组
 81      * @return
 82      * @throws Exception
 83      */
 84     private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
 85         // 生成一个可信任的随机数源
 86         SecureRandom sr = new SecureRandom();
 87 
 88         // 从原始密钥数据创建DESKeySpec对象
 89         DESKeySpec dks = new DESKeySpec(key);
 90 
 91         // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
 92         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
 93         SecretKey securekey = keyFactory.generateSecret(dks);
 94 
 95         // Cipher对象实际完成解密操作
 96         Cipher cipher = Cipher.getInstance(DES);
 97 
 98         // 用密钥初始化Cipher对象
 99         cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
100 
101         return cipher.doFinal(data);
102     }
103 
104     public static void main(String[] args) throws Exception {
105         String data = "{\"userid\":\"chenggong\",\"message\":\"活生生的大富豪的挥挥手说的回复\"}";
106         System.out.println(data);
107         System.out.println(encryptjm(data));
108         System.out.println(decryptjm("FRGar1wWT7scWdlnq5c/YypcinPDXqLE4ige9sN8eF/AsXrGeqmgBRsOc7tfQDkEdAwJVmz2J0FbO91LrPu0049OYGXF6MCNu0049Y25OjeObNesTZVkZobhQRvqPA=="));
109         boolean base64 = Base64.isBase64(data);
110         System.out.println(base64);
111     }
112  }
des算法

 

posted @ 2019-03-11 11:38  升级打怪猴  阅读(232)  评论(0编辑  收藏  举报