Java XxteaUtils工具类
2018-03-22 11:42
393 查看
import java.io.UnsupportedEncodingException; public class XxteaUtils { /** * Encrypt data with key. * * @param data * @param key * @return */ public static byte[] encrypt(byte[] data, byte[] key) { if (data.length == 0) { return data; } return toByteArray(encrypt(toIntArray(data, true), toIntArray(key, false)), false); } /** * Decrypt data with key. * * @param data * @param key * @return */ public static byte[] decrypt(byte[] data, byte[] key) { if (data.length == 0) { return data; } return toByteArray(decrypt(toIntArray(data, false), toIntArray(key, false)), true); } /** * Encrypt data with key. * * @param v * @param k * @return */ public static int[] encrypt(int[] v, int[] k) { int n = v.length - 1; if (n < 1) { return v; } if (k.length < 4) { int[] key = new int[4]; System.arraycopy(k, 0, key, 0, k.length); k = key; } int z = v , y = v[0], delta = 0x9E3779B9, sum = 0, e; int p, q = 6 + 52 / (n + 1); while (q-- > 0) { sum = sum + delta; e = sum >>> 2 & 3; for (p = 0; p < n; p++) { y = v[p + 1]; z = v[p] += (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z); } y = v[0]; z = v += (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z); } return v; } /** * Decrypt data with key. * * @param v * @param k * @return */ public static int[] decrypt(int[] v, int[] k) { int n = v.length - 1; if (n < 1) { return v; } if (k.length < 4) { int[] key = new int[4]; System.arraycopy(k, 0, key, 0, k.length); k = key; } int z = v , y = v[0], delta = 0x9E3779B9, sum, e; int p, q = 6 + 52 / (n + 1); sum = q * delta; while (sum != 0) { e = sum >>> 2 & 3; for (p = n; p > 0; p--) { z = v[p - 1]; y = v[p] -= (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z); } z = v ; y = v[0] -= (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z); sum = sum - delta; } return v; } /** * Convert byte array to int array. * * @param data * @param includeLength * @return */ private static int[] toIntArray(byte[] data, boolean includeLength) { int n = (((data.length & 3) == 0) ? (data.length >>> 2) : ((data.length >>> 2) + 1)); int[] result; if (includeLength) { result = new int[n + 1]; result = data.length; } else { result = new int ; } n = data.length; for (int i = 0; i < n; i++) { result[i >>> 2] |= (0x000000ff & data[i]) << ((i & 3) << 3); } return result; } /** * Convert int array to byte array. * * @param data * @param includeLength * @return */ private static byte[] toByteArray(int[] data, boolean includeLength) { int n; if (includeLength) { n = data[data.length - 1]; } else { n = data.length << 2; } byte[] result = new byte ; for (int i = 0; i < n; i++) { result[i] = (byte) (data[i >>> 2] >>> ((i & 3) << 3)); } return result; } /** * 字节数组转换为hex字符串 * * @param bArray * @return */ private static final String bytesToHexString(byte[] bArray) { StringBuffer sb = new StringBuffer(bArray.length); for (int i = 0; i < bArray.length; ++i) { String sTemp = Integer.toHexString(0xFF & bArray[i]); if (sTemp.length() < 2) sb.append(0); sb.append(sTemp.toUpperCase()); } return sb.toString(); } /** * hex字符串转换为字节数组 * * @param str * @return */ private static byte[] hexToBytes(String str) { if (str == null) { return null; } char[] hex = str.toCharArray(); int length = hex.length / 2; byte[] raw = new byte[length]; for (int i = 0; i < length; ++i) { int high = Character.digit(hex[(i * 2)], 16); int low = Character.digit(hex[(i * 2 + 1)], 16); int value = high << 4 | low; if (value > 127) value -= 256; raw[i] = (byte) value; } return raw; } /** * 使用XXTea 算法加密字符串 * * @param plain * 被加密的字符串 * @param charset * 字符集 * @param key * 密钥 * @return 加密之后的hex 字符串 * @throws UnsupportedEncodingException */ public static String encryptStr(String plain, String charset, String key) throws Exception { if (plain == null || charset == null || key == null) { return null; } byte[] bytes = encrypt(plain.getBytes(charset), key.getBytes(charset)); return bytesToHexString(bytes); } /** * 使用XXTea 算法解密字符串 * * @param hexStr * 加密之后的hex 字符串 * @param charset * 字符集 * @param key * 密钥 * @return 解密之后的字符串 * @throws UnsupportedEncodingException */ public static String decryptStr(String hexStr, String charset, String key) throws Exception { if (hexStr == null || charset == null || key == null) { return null; } byte[] bytes = decrypt(hexToBytes(hexStr), key.getBytes(charset)); return new String(bytes, charset); } }
相关文章推荐
- java将文件打包成ZIP压缩文件的工具类实例
- Java判断文件编码工具类
- JAVA工具类(16)---3des加解密工具类
- 【死磕Java并发】—- J.U.C之并发工具类:CyclicBarrier
- java实现将ZIP压缩文件解压的工具类
- 字符串工具类 StringUtil.java
- java日期工具类DateUtil-续二
- 【死磕Java并发】-----J.U.C之并发工具类:CountDownLatch
- java工具类mht转html格式文件
- java常用类解析六:IO系统文件读写工具类
- IPUtil工具类,java 获取IP所属地
- (二)java生成随机数工具类RandomUtils详解
- java读取文件并获得文件编码,转换为指定编码的工具类代码
- Java复制文件工具类
- Java时间处理工具类
- java并发编程的艺术笔记第八章——java中的并发工具类
- java操作文件工具类( 新建、复制、删除、移动文件(夹))
- Java工具类六 -- 其它类型
- JAVA Properties工具类
- (十五)Java工具类StringUtils中isAlpha、isAlphanumeric、isAlphanumericSpace、isAlphaSpace方法详解