aes、base64转码、Json转换、md5工具类demo

编程入门 行业动态 更新时间:2024-10-08 10:28:23

aes、base64转码、Json转换、md5<a href=https://www.elefans.com/category/jswz/34/1770073.html style=工具类demo"/>

aes、base64转码、Json转换、md5工具类demo

demo包含如下几个类:

  • 1.AesUtils:
package com.lijie.utils;import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;/*** * @author Lijie**/
public class AesUtils {public static void main(String[] args) {// 密钥的种子,可以是任何形式,本质是字节数组String strKey = "lijietest";// 密钥数据byte[] rawKey = getRawKey(strKey.getBytes());// 密码的明文String clearPwd = "{\"name\":\"黎杰\",\"age\":24,\"birthday\":\"2017年02月20日\",\"email\":\"lijiexxxx@xxxx\"}";// 密码加密后的密文byte[] encryptedByteArr = encrypt(rawKey, clearPwd);String encryptedPwd = new String(encryptedByteArr);System.out.println(encryptedPwd);// 解密后的字符串String decryptedPwd = decrypt(encryptedByteArr, rawKey);System.out.println(decryptedPwd);}/*** @param rawKey*            密钥* @param clearPwd*            明文字符串* @return 密文字节数组*/public static byte[] encrypt(byte[] rawKey, String clearPwd) {try {SecretKeySpec secretKeySpec = new SecretKeySpec(rawKey, "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] encypted = cipher.doFinal(clearPwd.getBytes());return encypted;} catch (Exception e) {return null;}}/*** @param encrypted*            密文字节数组* @param rawKey*            密钥* @return 解密后的字符串*/public static String decrypt(byte[] encrypted, byte[] rawKey) {try {SecretKeySpec secretKeySpec = new SecretKeySpec(rawKey, "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] decrypted = cipher.doFinal(encrypted);return new String(decrypted);} catch (Exception e) {System.out.println();return "";}}/*** @param seed种子数据* @return 密钥数据*/public static byte[] getRawKey(byte[] seed) {byte[] rawKey = null;try {KeyGenerator kgen = KeyGenerator.getInstance("AES");SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");secureRandom.setSeed(seed);// AES加密数据块分组长度必须为128比特,密钥长度可以是128比特、192比特、256比特中的任意一个kgen.init(128, secureRandom);SecretKey secretKey = kgen.generateKey();rawKey = secretKey.getEncoded();} catch (NoSuchAlgorithmException e) {}return rawKey;}
}
  • 2.Base64Util
package com.lijie.utils;import org.glassfish.grizzly.http.util.Base64Utils;/*** * @author Lijie**/
public class Base64Util {/*** base64解密* * @param info* @return*/public static byte[] decode(byte[] info) {return Base64Utils.decode(info);}/*** base64加密* * @param info* @return*/public static byte[] encode(byte[] info) {return Base64Utils.encodeToByte(info, true);}
}
  • 3.JsonUtils(需要导入jackson包)
package com.lijie.utils;import java.io.IOException;import org.apache.htrace.fasterxml.jackson.core.JsonParseException;
import org.apache.htrace.fasterxml.jackson.databind.JsonMappingException;
import org.apache.htrace.fasterxml.jackson.databind.ObjectMapper;/*** * @author Lijie**/
public class JsonUtils {/*** 转换json* * @param obj* @return* @throws Exception*/public static String ObjToJson(Object obj) throws Exception {ObjectMapper om = new ObjectMapper();String json = om.writeValueAsString(obj);return json;}/*** json转换* * @param json* @param clazz* @return* @throws IOException* @throws JsonMappingException* @throws JsonParseException*/public static Object JsonToObj(String json, Class clazz) throws Exception {ObjectMapper om = new ObjectMapper();Object obj = om.readValue(json, clazz);return obj;}}
  • 4.Signature(MD5):
package com.lijie.utils;import java.security.MessageDigest;/*** * @author Lijie**/
public class Signature {public static String md5(String data, String pv) throws Exception {// 将字符串转为字节数组byte[] strBytes = (data + pv).getBytes();// 加密器MessageDigest md = MessageDigest.getInstance("MD5");// 执行加密md.update(strBytes);// 加密结果byte[] digest = md.digest();// 返回return byteArrayToHex(digest);}private static String byteArrayToHex(byte[] byteArray) {// 首先初始化一个字符数组,用来存放每个16进制字符char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A', 'B', 'C', 'D', 'E', 'F' };// new一个字符数组,这个就是用来组成结果字符串的(解释一下:一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方))char[] resultCharArray = new char[byteArray.length * 2];// 遍历字节数组,通过位运算(位运算效率高),转换成字符放到字符数组中去int index = 0;for (byte b : byteArray) {resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];resultCharArray[index++] = hexDigits[b & 0xf];}// 字符数组组合成字符串返回return new String(resultCharArray);}}

更多推荐

aes、base64转码、Json转换、md5工具类demo

本文发布于:2024-02-07 14:14:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工具   转码   aes   demo   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!