【搬运】Tea算法Java实现工具类

最近在做数据加密,目标是实现平台app的数据安全性,所以准备使用AES+Base64进行加密,适逢一个特长的json串AES加密不了,于是在谷歌了各种算法,判断是否合用,参见 各种加密算法比较 一文中对各种算法进行了详尽的比较。由于长字符串AES对其束手无策,所以打算试试这个最快的TEA算法,遂扒了扒,找到一个07年的老帖子,帖子链接 TEA加密算法java版 ,又因为作者是为了完成游戏的存储,所以有一个类找不到,所以我把代码拿过来进行了小小的修改,分享之。侵权删。

  1 package com.wode369.Test;
  2 
  3  
  4 /**
  5  * Tea算法
  6  * 每次操作可以处理8个字节数据
  7  * KEY为16字节,应为包含4个int型数的int[],一个int为4个字节
  8  * 加密解密轮数应为8的倍数,推荐加密轮数为64轮
  9  * */
 10 public class TeaUtil {
 11     private final static int[] KEY = new int[]{//加密解密所用的KEY
 12         0x789f5645, 0xf68bd5a4,
 13         0x81963ffa, 0x458fac58
 14     };
 15     //加密
 16     public static byte[] encrypt(byte[] content, int offset, int[] key, int times){//times为加密轮数
 17         int[] tempInt = byteToInt(content, offset);
 18         int y = tempInt[0], z = tempInt[1], sum = 0, i;
 19         int delta=0x9e3779b9; //这是算法标准给的值
 20         int a = key[0], b = key[1], c = key[2], d = key[3]; 
 21 
 22         for (i = 0; i < times; i++) {   
 23             
 24             sum += delta;
 25             y += ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
 26             z += ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);
 27         }
 28         tempInt[0]=y;
 29         tempInt[1]=z; 
 30         return intToByte(tempInt, 0);
 31     }
 32     //解密
 33     public static byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times){
 34         int[] tempInt = byteToInt(encryptContent, offset);
 35         int y = tempInt[0], z = tempInt[1], sum = 0, i;
 36         int delta=0x9e3779b9; //这是算法标准给的值
 37         int a = key[0], b = key[1], c = key[2], d = key[3];
 38         if (times == 32)
 39             sum = 0xC6EF3720; /* delta << 5*/
 40         else if (times == 16)
 41             sum = 0xE3779B90; /* delta << 4*/
 42         else
 43             sum = delta * times;
 44 
 45         for(i = 0; i < times; i++) { 
 46             z -= ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);
 47             y -= ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
 48             sum -= delta; 
 49         }
 50         tempInt[0] = y;
 51         tempInt[1] = z;
 52 
 53         return intToByte(tempInt, 0);
 54     }
 55     //byte[]型数据转成int[]型数据
 56     private static int[] byteToInt(byte[] content, int offset){
 57 
 58         int[] result = new int[content.length >> 2];//除以2的n次方 == 右移n位 即 content.length / 4 == content.length >> 2
 59         for(int i = 0, j = offset; j < content.length; i++, j += 4){
 60             result[i] = transform(content[j + 3]) | transform(content[j + 2]) << 8 |
 61             transform(content[j + 1]) << 16 | (int)content[j] << 24;
 62         }
 63         return result;
 64         
 65     }
 66     //int[]型数据转成byte[]型数据
 67     private static byte[] intToByte(int[] content, int offset){
 68         byte[] result = new byte[content.length << 2];//乘以2的n次方 == 左移n位 即 content.length * 4 == content.length << 2
 69         for(int i = 0, j = offset; j < result.length; i++, j += 4){
 70             result[j + 3] = (byte)(content[i] & 0xff);
 71             result[j + 2] = (byte)((content[i] >> 8) & 0xff);
 72             result[j + 1] = (byte)((content[i] >> 16) & 0xff);
 73             result[j] = (byte)((content[i] >> 24) & 0xff);
 74         }
 75         return result;
 76     }
 77     //若某字节为负数则需将其转成无符号正数
 78     private static int transform(byte temp){
 79         int tempInt = (int)temp;
 80         if(tempInt < 0){
 81             tempInt += 256;
 82         }
 83         return tempInt;
 84     }
 85     
 86     //通过TEA算法加密信息
 87     public static byte[] encryptByTea(String info){
 88         byte[] temp = info.getBytes();
 89         int n = 8 - temp.length % 8;//若temp的位数不足8的倍数,需要填充的位数
 90         byte[] encryptStr = new byte[temp.length + n];
 91         encryptStr[0] = (byte)n;
 92         System.arraycopy(temp, 0, encryptStr, n, temp.length);
 93         byte[] result = new byte[encryptStr.length];
 94         for(int offset = 0; offset < result.length; offset += 8){
 95             byte[] tempEncrpt = TeaUtil.encrypt(encryptStr, offset, KEY, 32);
 96             System.arraycopy(tempEncrpt, 0, result, offset, 8);
 97         }
 98         return result;
 99     }
100     //通过TEA算法解密信息
101     public static String decryptByTea(byte[] secretInfo){
102         byte[] decryptStr = null;
103         byte[] tempDecrypt = new byte[secretInfo.length];
104         for(int offset = 0; offset < secretInfo.length; offset += 8){
105             decryptStr = TeaUtil.decrypt(secretInfo, offset, KEY, 32);
106             System.arraycopy(decryptStr, 0, tempDecrypt, offset, 8);
107         }
108         
109         int n = tempDecrypt[0];
110         return new String(tempDecrypt, n, decryptStr.length - n);
111         
112     }
113     
114     public static void main(String[] args){
115         String info =  "{"
116                             + "'name':'xyz',"
117                             + "'age':'16',"
118                             + "'gender':'male'"
119                         + "}";
120         System.out.println("原数据:" + info);
121         System.out.println("开始时间:"+System.currentTimeMillis());
122         byte[] encryptInfo = encryptByTea(info);
123         System.out.println("加密后的数据:");
124         for(byte i : encryptInfo)
125             System.out.print(i + " ");
126         System.out.println();
127         String decryptInfo = decryptByTea(encryptInfo);
128         System.out.println("结束时间:"+System.currentTimeMillis());
129         System.out.print("解密后的数据:");
130         System.out.println(decryptInfo);
131         
132     }
133     
134 }

输出:

1 原数据:{'name':'xyz','age':'16','gender':'male'}
2 开始时间:1512461129393
3 加密后的数据:
4 -21 -88 6 86 88 57 -75 18 -56 -77 91 17 8 46 -109 17 -123 5 64 -95 -65 32 41 26 109 54 49 17 -20 -84 -111 -3 39 67 -44 21 60 80 4 -79 -52 -125 -15 65 -99 61 100 93 
5 结束时间:1512461129394
6 解密后的数据:{'name':'xyz','age':'16','gender':'male'}

 

其实,这个工具类修改完成后,我再次使用那个比较长的字符串去试验了下,还是不可以……这就很尴尬了- .-||  但是经过测试,这个TEA算法的确很快。

转载于:https://www.cnblogs.com/hellxz/p/7987624.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值