3DES工具类

it2022-05-05  132

package com.modules.utils; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.util.Arrays; public class EncryFor3DESUtil { private static final String Algorithm = "DESede"; // 定义 加密算法,可用 private static final char[] BASE = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; /** * 加密 * @param data 待加密明文 * @param strKey 传入密钥:16位数字或字母,大小写敏感(密钥长度由项目要求的,非固定长度) * @return * @throws Exception */ public static String encrypt(String data , String strKey) throws Exception{ data = data + " "; byte[] bMsg = data.getBytes("gbk"); int l = (bMsg.length / 16 + 1) * 16; byte[] btMsg = Arrays.copyOf(bMsg, l); byte[] digestOfPassword = strKey.getBytes(); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); int j = 0; for(int var8 = 16; j < 8; keyBytes[var8++] = keyBytes[j++]) { ; } SecretKey key = new SecretKeySpec(keyBytes, Algorithm); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE, key); String rtn = StringUtil.byte2hex(c1.doFinal(btMsg)); return rtn.substring(0, (bMsg.length / 8 + 1) * 16); } /** * 解密 * @param message 密文 * @param strKey 传入密钥:16位数字或字母,大小写敏感 * @return * @throws Exception */ public static String decrypt(String message, String strKey) throws Exception { message = message + getAdd(message.length(), strKey); byte[] digestOfPassword = strKey.getBytes(); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); int j = 0; for(int var5 = 16; j < 8; keyBytes[var5++] = keyBytes[j++]) { ; } SecretKey key = new SecretKeySpec(keyBytes, Algorithm); Cipher decipher = Cipher.getInstance(Algorithm); decipher.init(Cipher.DECRYPT_MODE, key); byte[] plainText = decipher.doFinal(StringUtil.hex2byte(message)); return new String(plainText, "gbk"); } private static String getAdd(int length, String strKey) throws Exception { byte[] btMsg = new byte[length / 2]; byte[] digestOfPassword = strKey.getBytes("utf-8"); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); int j = 0; for(int var6 = 16; j < 8; keyBytes[var6++] = keyBytes[j++]) { ; } SecretKey key = new SecretKeySpec(keyBytes, Algorithm); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE, key); String rtn = StringUtil.byte2hex(c1.doFinal(btMsg)); return rtn.substring(length); } /** * 生成key * @param len * @return */ public static String newKey(int len) { Random rd = new Random(); StringBuffer sb = new StringBuffer(); for(int i = 0; i < len; ++i) { sb.append(BASE[Math.abs(rd.nextInt()) % 62]); } return sb.toString(); } }

<!-- StringUtil工具类 -->

package com.modules.utils; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringUtil { public static boolean isChinese(String strName) { char[] ch = strName.toCharArray(); for (int i = 0; i < ch.length; i++) { char c = ch[i]; if (isChinese(c)) { return true; } } return false; } public static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { return true; } return false; } public static String isChineseToUniCode(String str){ StringBuilder sb = new StringBuilder(); char[] ch = str.toCharArray(); for (int i = 0; i < ch.length; i++) { char c = ch[i]; if (StringUtil.isChinese(c)) { sb.append(cnToUnicode(Character.toString(c))); }else { sb.append(Character.toString(c)); } } return sb.toString(); } public static String isUniCodeToChinese(String unicode){ List<String> list = new ArrayList<>(); String reg= "\\\\u[0-9,a-f,A-F]{4}"; Pattern p = Pattern.compile(reg); Matcher m=p.matcher(unicode); while (m.find()){ list.add(m.group()); } for (int i = 0, j = 2; i < list.size(); i++) { String code = list.get(i).substring(j, j + 4); char ch = (char) Integer.parseInt(code, 16); unicode = unicode.replace(list.get(i),String.valueOf(ch)); } return unicode; } public static String unicodeToCn(String unicode) { /** 以 \ u 分割,因为java注释也能识别unicode,因此中间加了一个空格*/ String[] strs = unicode.split("\\\\u"); String returnStr = ""; // 由于unicode字符串以 \ u 开头,因此分割出的第一个字符是""。 for (int i = 1; i < strs.length; i++) { returnStr += (char) Integer.valueOf(strs[i], 16).intValue(); } return returnStr; } /** * 如果字符串包含中文,则将中文转为unicode编码 * @param cn * @return */ public static String cnToUnicode(String cn) { char[] chars = cn.toCharArray(); String returnStr = ""; for (int i = 0; i < chars.length; i++) { returnStr += "\\u" + Integer.toString(chars[i], 16); } return returnStr; } public static String getRandomString(int arg0){ String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random=new Random(); StringBuffer sb=new StringBuffer(); for(int i = 0;i < arg0;i++){ int number=random.nextInt(62); sb.append(str.charAt(number)); } return sb.toString(); } public static byte uniteBytes(String src0, String src1) { byte b0 = Byte.decode("0x" + src0); b0 = (byte)(b0 << 4); byte b1 = Byte.decode("0x" + src1); byte ret = (byte)(b0 | b1); return ret; } /** * 字符串(16进制)转byte数组 * @param src * @return */ public static byte[] hexStr2Bytes(String src) { // int m = false; // int n = false; int l = src.length() / 2; byte[] ret = new byte[l]; for(int i = 0; i < l; ++i) { int m = i * 2 + 1; int n = m + 1; ret[i] = StringUtil.uniteBytes(src.substring(i * 2, m), src.substring(m, n)); } return ret; } /** * byte数组转字符串(16进制) * @param data * @param digits * @return */ public static String toHex(byte[] data , String digits) { return toHex(data, data.length , digits); } /** * byte数组转字符串(16进制) * @param data * @param length * @param digits * @return */ public static String toHex(byte[] data, int length , String digits) { StringBuffer buf = new StringBuffer(); for(int i = 0; i != length; ++i) { int v = data[i] & 255; buf.append(digits.charAt(v >> 4)); buf.append(digits.charAt(v & 15)); } return buf.toString(); } /** * byte数组转HEX字符串 * @param b * @return */ public static String byte2hex(byte[] b) { String hs = ""; String stmp = ""; for(int n = 0; n < b.length; ++n) { stmp = Integer.toHexString(b[n] & 255); if (stmp.length() == 1) { hs = hs + "0" + stmp; } else { hs = hs + stmp; } } return hs.toUpperCase(); } public static byte[] hex2byte(String s) { int l = s.length() / 2; byte[] b = new byte[l]; for(int i = 0; i < l; ++i) { int m = i * 2 + 1; int n = m + 1; b[i] = StringUtil.uniteBytes(s.substring(i * 2, m), s.substring(m, n)); } return b; } }

 


最新回复(0)