123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package com.yihu.platform.utils;
- import java.io.IOException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.security.SecureRandom;
- import javax.crypto.Cipher;
- import javax.crypto.SecretKey;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.DESKeySpec;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- /**
- * ClassName: DesUtil
- * @Description: 字符串加密 、解密 类
- * @author houzq
- * @Company www.yihu.com
- * @date 2017-8-31 下午03:12:54
- */
- public class DesUtil {
- private final static String DES = "DES";
- private final static String key = "jkzl_!@#$%_";
- /**
- * Description 根据键值进行加密
- *
- * @param data
- * @param key
- * 加密键byte数组
- * @return
- * @throws Exception
- */
- public static String encrypt(String data, String key) throws Exception {
- byte[] bt = encrypt(data.getBytes(), key.getBytes());
- String strs = new BASE64Encoder().encode(bt);
- return strs;
- }
- public static String encrypt(String data) throws Exception {
- byte[] bt = encrypt(data.getBytes(), key.getBytes());
- String strs = new BASE64Encoder().encode(bt);
- return strs;
- }
- /**
- * Description 根据键值进行解密
- *
- * @param data
- * @param key
- * 加密键byte数组
- * @return
- * @throws IOException
- * @throws Exception
- */
- public static String decrypt(String data, String key) throws IOException, Exception {
- if (data == null)
- return null;
- BASE64Decoder decoder = new BASE64Decoder();
- byte[] buf = decoder.decodeBuffer(data);
- byte[] bt = decrypt(buf, key.getBytes());
- return new String(bt);
- }
- public static String decrypt(String data) throws IOException, Exception {
- if (data == null)
- return null;
- BASE64Decoder decoder = new BASE64Decoder();
- byte[] buf = decoder.decodeBuffer(data);
- byte[] bt = decrypt(buf, key.getBytes());
- return new String(bt);
- }
-
- /**
- * Description 根据键值进行加密
- *
- * @param data
- * @param key
- * 加密键byte数组
- * @return
- * @throws Exception
- */
- private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
- // 生成一个可信任的随机数源
- SecureRandom sr = new SecureRandom();
- // 从原始密钥数据创建DESKeySpec对象
- DESKeySpec dks = new DESKeySpec(key);
- // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
- SecretKey securekey = keyFactory.generateSecret(dks);
- // Cipher对象实际完成加密操作
- Cipher cipher = Cipher.getInstance(DES);
- // 用密钥初始化Cipher对象
- cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
- return cipher.doFinal(data);
- }
- /**
- * Description 根据键值进行解密
- *
- * @param data
- * @param key
- * 加密键byte数组
- * @return
- * @throws Exception
- */
- private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
- // 生成一个可信任的随机数源
- SecureRandom sr = new SecureRandom();
- // 从原始密钥数据创建DESKeySpec对象
- DESKeySpec dks = new DESKeySpec(key);
- // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
- SecretKey securekey = keyFactory.generateSecret(dks);
- // Cipher对象实际完成解密操作
- Cipher cipher = Cipher.getInstance(DES);
- // 用密钥初始化Cipher对象
- cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
- return cipher.doFinal(data);
- }
- /**
- * MD5工具
- *
- * @param string
- * @return
- */
- public static String md5(String string) {
- MessageDigest md = null;
- try {
- md = MessageDigest.getInstance("md5");
- md.update(string.getBytes());
- byte[] md5Bytes = md.digest();
- return bytes2Hex(md5Bytes);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- return null;
- }
- private static String bytes2Hex(byte[] byteArray) {
- StringBuffer strBuf = new StringBuffer();
- for (int i = 0; i < byteArray.length; i++) {
- if (byteArray[i] >= 0 && byteArray[i] < 16) {
- strBuf.append("0");
- }
- strBuf.append(Integer.toHexString(byteArray[i] & 0xFF));
- }
- return strBuf.toString();
- }
- /**
- * BASE64 编码
- * @param s
- * @return
- */
- public static String getBASE64(String s) {
- if (s == null)
- return null;
- return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
- }
- // 将 BASE64 编码的字符串 s 进行解码
- public static String getFromBASE64(String s) {
- if (s == null)
- return null;
- BASE64Decoder decoder = new BASE64Decoder();
- try {
- byte[] b = decoder.decodeBuffer(s);
- return new String(b);
- } catch (Exception e) {
- return null;
- }
- }
- }
|