XXTEA+BASE64 JAVA
2015-11-16 10:50
579 查看
package com.cardvlaue.sys; import java.io.UnsupportedEncodingException; public final class XXTEA2 { private static final int DELTA = 0x9E3779B9; private static int MX(int sum, int y, int z, int p, int e, int[] k) { return (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z); } private XXTEA2() {} public static final byte[] encrypt(byte[] data, byte[] key) { if (data.length == 0) { return data; } return toByteArray( encrypt(toIntArray(data, true), toIntArray(fixKey(key), false)), false); } public static final byte[] encrypt(String data, byte[] key) { try { return encrypt(data.getBytes("UTF-8"), key); } catch (UnsupportedEncodingException e) { return null; } } public static final byte[] encrypt(byte[] data, String key) { try { return encrypt(data, key.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { return null; } } public static final byte[] encrypt(String data, String key) { try { return encrypt(data.getBytes("UTF-8"), key.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { return null; } } public static final String encryptToBase64String(byte[] data, byte[] key) { byte[] bytes = encrypt(data, key); if (bytes == null) return null; return Base64.encode(bytes); } public static final String encryptToBase64String(String data, byte[] key) { byte[] bytes = encrypt(data, key); if (bytes == null) return null; return Base64.encode(bytes); } public static final String encryptToBase64String(byte[] data, String key) { byte[] bytes = encrypt(data, key); if (bytes == null) return null; return Base64.encode(bytes); } public static final String encryptToBase64String(String data, String key) { byte[] bytes = encrypt(data, key); if (bytes == null) return null; return Base64.encode(bytes); } public static final byte[] decrypt(byte[] data, byte[] key) { if (data.length == 0) { return data; } return toByteArray( decrypt(toIntArray(data, false), toIntArray(fixKey(key), false)), true); } public static final byte[] decrypt(byte[] data, String key) { try { return decrypt(data, key.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { return null; } } public static final byte[] decryptBase64String(String data, byte[] key) { return decrypt(Base64.decode(data), key); } public static final byte[] decryptBase64String(String data, String key) { return decrypt(Base64.decode(data), key); } public static final String decryptToString(byte[] data, byte[] key) { try { byte[] bytes = decrypt(data, key); if (bytes == null) return null; return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException ex) { return null; } } public static final String decryptToString(byte[] data, String key) { try { byte[] bytes = decrypt(data, key); if (bytes == null) return null; return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException ex) { return null; } } public static final String decryptBase64StringToString(String data, byte[] key) { try { byte[] bytes = decrypt(Base64.decode(data), key); if (bytes == null) return null; return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException ex) { return null; } } public static final String decryptBase64StringToString(String data, String key) { try { byte[] bytes = decrypt(Base64.decode(data), key); if (bytes == null) return null; return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException ex) { return null; } } private static int[] encrypt(int[] v, int[] k) { int n = v.length - 1; if (n < 1) { return v; } int p, q = 6 + 52 / (n + 1); int z = v , y, sum = 0, e; while (q-- > 0) { sum = sum + DELTA; e = sum >>> 2 & 3; for (p = 0; p < n; p++) { y = v[p + 1]; z = v[p] += MX(sum, y, z, p, e, k); } y = v[0]; z = v += MX(sum, y, z, p, e, k); } return v; } private static int[] decrypt(int[] v, int[] k) { int n = v.length - 1; if (n < 1) { return v; } int p, q = 6 + 52 / (n + 1); int z, y = v[0], sum = q * DELTA, e; while (sum != 0) { e = sum >>> 2 & 3; for (p = n; p > 0; p--) { z = v[p - 1]; y = v[p] -= MX(sum, y, z, p, e, k); } z = v ; y = v[0] -= MX(sum, y, z, p, e, k); sum = sum - DELTA; } return v; } private static byte[] fixKey(byte[] key) { if (key.length == 16) return key; byte[] fixedkey = new byte[16]; if (key.length < 16) { System.arraycopy(key, 0, fixedkey, 0, key.length); } else { System.arraycopy(key, 0, fixedkey, 0, 16); } return fixedkey; } 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; } private static byte[] toByteArray(int[] data, boolean includeLength) { int n = data.length << 2; if (includeLength) { int m = data[data.length - 1]; n -= 4; if ((m < n - 3) || (m > n)) { return null; } n = m; } byte[] result = new byte ; for (int i = 0; i < n; ++i) { result[i] = (byte) (data[i >>> 2] >>> ((i & 3) << 3)); } return result; } }
相关文章推荐
- Java 内存结构备忘录
- java正则表达式中 \d \D 有什么区别?
- java heap space 问题 java.lang.OutOfMemoryError: ChunkPool::allocate
- Java工作利器之常用工具类(四)——Json工具类,使用正则支持xml与json互转
- Java工作利器之常用工具类(三)——字符串工具类-智能截取
- Java工作利器之常用工具类(二)——数字工具类-大数乘法、加法、减法运算
- Java 反射
- Java工作利器之常用工具类(一)——数字工具类-数字转汉字
- Could not publish to the server. java.lang.NullPointerException
- FatMouse and JavaBean II
- springMvc时间格式化model的时间字段上填写
- Java 内存结构备忘录
- java泛型编程
- java代码封装
- bzoj 3198 spring
- Java中String转化为其他类型方法汇总
- JAVA基础编程之兔子问题
- spring mvc controller中获取request head内容
- 【学习日记】javaIO流的知识点总结(2)
- 编写线程安全的Java缓存读写机制 (原创)