一般加密和解密工具类

编程技术  /  houtizong 发布于 3年前   74
package com.snail.util;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import com.d1xn.common.base.SystemConfig;import com.d1xn.common.log.Log;/** * 一般加密解密工具<br> * DESede/DES/BlowFish<br> * DES的密钥(key)长度是为8个字节,加密解密的速度是最快的<br> * DESede的密钥(key)长度是24个字节<br> * BlowFish的密钥(key)是可变的 加密最快,强度最高,密钥长度范围(1<=key<=16)<br> *  * @author penghuaiyi * @version 1.0 */public class SecretCodeUtil {public final static String DES = "DES";public final static String DESEDE = "DESede";public final static String BLOWFISH = "BlowFish";public final static String MD5 = "MD5";/** * md5加密 *  * @param plainText * @return 加密后的大写字符串 */public static String getMD5ofStr(String plainText) {try {return getMD5ofStr(string2Bytes(plainText));} catch (Exception e) {e.printStackTrace();}return null;}/** * md5加密 *  * @param str * @return 加密后的大写字符串 */public static String getMD5ofStr(byte str[]) {try {MessageDigest md = MessageDigest.getInstance(MD5);md.update(str);byte b[] = md.digest();return bytes2HexString(b);} catch (Exception e) {e.printStackTrace();}return null;}/** * 字符串加密 *  * @param data *            字符串数据 * @param key *            密钥 * @param name *            算法名称 * @throws Exception */public static String encrypt(String data, String key, String name) {try {return bytes2HexString(encrypt(data.getBytes(), string2Bytes(key), name));} catch (Exception e) {e.printStackTrace();}return null;}/** * 字符串加密 *  * @param data *            字符串数据 * @param key *            密钥 * @param name *            算法名称 * @throws Exception */public static String encrypt(String data, byte[] key, String name) {try {return bytes2HexString(encrypt(data.getBytes(), key, name));} catch (Exception e) {e.printStackTrace();}return null;}/** * 字符串解密 *  * @param data *            字符串加密数据 * @param key *            密钥 * @param name *            算法名称 * @return * @throws Exception */public static String decrypt(String data, String key, String name) {try {return new String(decrypt(hex2byte(data.getBytes()), string2Bytes(key), name));} catch (Exception e) {Log.error(SecretCodeUtil.class, e, "data:" + data);}return null;}/** * 字符串解密 *  * @param data *            字符串加密数据 * @param key *            密钥 * @param name *            算法名称 * @return * @throws Exception */public static String decrypt(String data, byte[] key, String name) {try {return new String(decrypt(hex2byte(data.getBytes()), key, name));} catch (Exception e) {}return null;}/** * 对数据源进行加密 *  * @param src *            数据源 * @param key *            密钥 * @param name *            算法的名称 * @return 返回加密后的数据 * @throws Exception */private static byte[] encrypt(byte[] src, byte[] key, String name) throws Exception {SecretKeySpec securekey = new SecretKeySpec(key, name);// Cipher对象实际完成加密操作Cipher cipher = Cipher.getInstance(name);// 用密匙初始化Cipher对象cipher.init(Cipher.ENCRYPT_MODE, securekey);// 现在,获取数据并加密// 正式执行加密操作return cipher.doFinal(src);}/** * 对加密的数据源进行解密 *  * @param src *            数据源 * @param key *            密钥 * @param name *            算法的名称 * @return 返回解密后的原始数据 * @throws Exception */private static byte[] decrypt(byte[] src, byte[] key, String name) throws Exception {SecretKeySpec securekey = new SecretKeySpec(key, name);// Cipher对象实际完成解密操作Cipher cipher = Cipher.getInstance(name);// 用密匙初始化Cipher对象cipher.init(Cipher.DECRYPT_MODE, securekey);// 现在,获取数据并解密// 正式执行解密操作return cipher.doFinal(src);}/** * 二行制转字符串 *  * @param b * @return */private static String bytes2HexString(byte[] bytes) {String hs = null;if (bytes != null) {final int size = bytes.length;if (size > 0) {StringBuilder sb = new StringBuilder();for (int i = 0; i < size; i++) {String tmp = (java.lang.Integer.toHexString(bytes[i] & 0XFF));if (tmp.length() == 1) {sb.append("0" + tmp);} else {sb.append(tmp);}}hs = sb.toString().toUpperCase();}}return hs;}/** * 把字符串转化成 Unicode Bytes. *  * @param s *            String * @return byte[] */private static byte[] string2Bytes(String s) {byte[] bytes = null;if (s != null) {try {bytes = s.getBytes(SystemConfig.DEFAULT_ENCODING);} catch (UnsupportedEncodingException e) {e.printStackTrace();}}return bytes;}/** * 结字节进行转换 *  * @param b * @return */private static byte[] hex2byte(byte[] b) {if ((b.length % 2) != 0) {return null;}byte[] b2 = new byte[b.length / 2];for (int n = 0; n < b.length; n += 2) {String item = new String(b, n, 2);b2[n / 2] = (byte) Integer.parseInt(item, 16);}return b2;}public static void main(String[] args){//DES加密与解密String key = "snail001";String encrypt = SecretCodeUtil.encrypt("fuckyou2013", key, SecretCodeUtil.DES); //加密System.out.println(encrypt);String decrypt = SecretCodeUtil.decrypt(encrypt, key,  SecretCodeUtil.DES);//解密System.out.println(decrypt);//MD5加密,不可逆的String md5 = SecretCodeUtil.getMD5ofStr("fuckyou2013");System.out.println(md5);}}

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客