XXTea 加密/解密 字符串
来源:互联网 发布:淘宝哪天买东西最便宜 编辑:程序博客网 时间:2024/05/21 10:12
xxtea 加密和解密的参数都是字节数组, 开发中的加密内容和密钥一般是字符串, 于是自己写了一个类实现 XXtea 加密/解密字符串功能;
类:XXTeaUtil.java
package xxtea;import java.io.UnsupportedEncodingException;/** * XXTea加密/解密 字节数组和字符串 * @author WWP * @date 2016-11-6 下午5:25:45 */public class XXTeaUtil { /** * 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[n], 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[n] += (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[n], 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[n]; 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[n] = data.length; } else { result = new int[n]; } 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[n]; 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 UnsupportedEncodingException { 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 UnsupportedEncodingException { if (hexStr == null || charset == null || key == null) { return null; } byte[] bytes = decrypt(hexToBytes(hexStr), key.getBytes(charset)); return new String(bytes, charset); } /* -------------------------------自定义方法结束----------------------------------- */}
测试类:
package xxtea;public class Test { public static void main(String[] args) throws Exception { String str = "ab23e武汉321"; String key = "flds武汉djs"; String hexResult = XXTeaUtil.encryptStr(str, "utf-8", key); System.out.println("myencrypt: " + hexResult); str = XXTeaUtil.decryptStr(hexResult, "utf-8", key); System.out.println("myDecrypt: " + str); }}
最后输出结果:
myencrypt: 9DBDEED5952D10F2F8EA0054A97BAF363B76B408
myDecrypt: ab23e武汉321
0 0
- XXTea 加密/解密 字符串
- xxtea 加密解密
- PHP和Javascript的xxtea字符串加密/解密方法
- C++ XXTEA加密解密算法
- TEA、XTEA、XXTEA加密解密算法
- 使用xxtea加密或者解密文件
- PHP加密解密函数之Xxtea
- C++ XXTEA分组加密解密算法
- cocos2d-x 3.x学习笔记<三> 加密解密 xxtea
- cocos2dx lua XXTEA来加密解密实现详解
- XXTea加密 c++ 与 js 互相加解密
- xxtea官方解密
- 加密解密字符串
- 字符串加密解密函数
- 字符串加密解密
- .net字符串加密解密
- JavaScript字符串加密解密
- 字符串加密解密类
- 数据库优化_零碎
- C\C++中计时、延时函数
- struts2下载文件时出现乱码问题
- 城市地理空间数据可视化
- CNN在中文文本分类的应用
- XXTea 加密/解密 字符串
- Redis 复制原理及分析
- 勾股定理一日一证连载153
- /etc/ansible/ansible.cfg
- Java并发----ConcurrentHashMap01--(Happens-Before规则)
- 语音合成-----ekho a Chinese text-to-speech engine
- 电脑没有声音怎么办
- 函数的重载
- 我看到的最棒的Twisted入门教程!