博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TEA加密算法java版
阅读量:4630 次
发布时间:2019-06-09

本文共 4470 字,大约阅读时间需要 14 分钟。

 这个算法简单,而且效率高,每次可以操作8个字节的数据,加密解密的KEY为16字节,即包含4个int数据的int型数组,加密轮数应为8的倍数,一般比较常用的轮数为64,32,16,推荐用64轮.

源代码如下:
/** *//**
* Tea算法
* 每次操作可以处理8个字节数据
* KEY为16字节,应为包含4个int型数的int[],一个int为4个字节
* 加密解密轮数应为8的倍数,推荐加密轮数为64轮
* */
public class Tea {
//加密
public byte[] encrypt(byte[] content, int offset, int[] key, int times){//times为加密轮数
int[] tempInt = byteToInt(content, offset);
int y = tempInt[0], z = tempInt[1], sum = 0, i;
int delta=0x9e3779b9; //这是算法标准给的值
int a = key[0], b = key[1], c = key[2], d = key[3];
for (i = 0; i < times; i++) {
sum += delta;
y += ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
z += ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);
}
tempInt[0]=y;
tempInt[1]=z;
return intToByte(tempInt, 0);
}
//解密
public byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times){
int[] tempInt = byteToInt(encryptContent, offset);
int y = tempInt[0], z = tempInt[1], sum = 0xC6EF3720, i;
int delta=0x9e3779b9; //这是算法标准给的值
int a = key[0], b = key[1], c = key[2], d = key[3];
for(i = 0; i < times; i++) {
z -= ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);
y -= ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
sum -= delta;
}
tempInt[0] = y;
tempInt[1] = z;
return intToByte(tempInt, 0);
}
//byte[]型数据转成int[]型数据
private int[] byteToInt(byte[] content, int offset){
int[] result = new int[content.length >> 2]; //除以2的n次方 == 右移n位 即 content.length / 4 == content.length >> 2
for(int i = 0, j = offset; j < content.length; i++, j += 4){
result[i] = transform(content[j + 3]) | transform(content[j + 2]) << 8 |
transform(content[j + 1]) << 16 | (int)content[j] << 24;
}
return result;
}
//int[]型数据转成byte[]型数据
private byte[] intToByte(int[] content, int offset){
byte[] result = new byte[content.length << 2]; //乘以2的n次方 == 左移n位 即 content.length * 4 == content.length << 2
for(int i = 0, j = offset; j < result.length; i++, j += 4){
result[j + 3] = (byte)(content[i] & 0xff);
result[j + 2] = (byte)((content[i] >> 8) & 0xff);
result[j + 1] = (byte)((content[i] >> 16) & 0xff);
result[j] = (byte)((content[i] >> 24) & 0xff);
}
return result;
}
//若某字节被解释成负的则需将其转成无符号正数
private static int transform(byte temp){
int tempInt = (int)temp;
if(tempInt < 0){
tempInt += 256;
}
return tempInt;
}
}
测试代码如下:
public static void main(String[] args){
int[] KEY = new int[]{//加密解密所用的KEY
0x789f5645, 0xf68bd5a4,
0x81963ffa, 0x458fac58
};
Tea tea = new Tea();
byte[] info = new byte[]{
1,2,3,4,5,6,7,8
};
System.out.print("原数据:");
for(byte i : info)
System.out.print(i + " ");
System.out.println();
byte[] secretInfo = tea.encrypt(info, 0, KEY, 32);
System.out.print("加密后的数据:");
for(byte i : secretInfo)
System.out.print(i + " ");
System.out.println();
byte[] decryptInfo = tea.decrypt(secretInfo, 0, KEY, 32);
System.out.print("解密后的数据:");
for(byte i : decryptInfo)
System.out.print(i + " ");
}
输出结果如下:
原数据:1 2 3 4 5 6 7 8
加密后的数据:92 124 -3 -125 -115 82 21 28
解密后的数据:1 2 3 4 5 6 7 8
这只是一次加密解密操作,如果你想一次处理大于8个字节的数据,需要再封装一下.
封装后的代码如下:
//通过TEA算法加密信息
private byte[] encryptByTea(String info){
byte[] temp = info.getBytes();
int n = 8 - temp.length % 8; //若temp的位数不足8的倍数,需要填充的位数
byte[] encryptStr = new byte[temp.length + n];
encryptStr[0] = (byte)n;
System.arraycopy(temp, 0, encryptStr, n, temp.length);
byte[] result = new byte[encryptStr.length];
for(int offset = 0; offset < result.length; offset += 8){
byte[] tempEncrpt = tea.encrypt(encryptStr, offset, KEY, 32);
System.arraycopy(tempEncrpt, 0, result, offset, 8);
}
return result;
}
//通过TEA算法解密信息
private String decryptByTea(byte[] secretInfo){
byte[] decryptStr = null;
byte[] tempDecrypt = new byte[secretInfo.length];
for(int offset = 0; offset < secretInfo.length; offset += 8){
decryptStr = tea.decrypt(secretInfo, offset, KEY, 32);
System.arraycopy(decryptStr, 0, tempDecrypt, offset, 8);
}
int n = tempDecrypt[0];
return new String(tempDecrypt, n, decryptStr.length - n);
}
测试代码如下:
public static void main(String[] args){
String info = "www.blogjava.net/orangehf";
System.out.println("原数据:" + info);
SaveFileIO io = new SaveFileIO();
byte[] encryptInfo = io.encryptByTea(info);
System.out.print("加密后的数据:");
for(byte i : encryptInfo)
System.out.print(i + " ");
System.out.println();
String decryptInfo = io.decryptByTea(encryptInfo);
System.out.print("解密后的数据:");
System.out.println(decryptInfo);
}
输出结果如下:
原数据:www.blogjava.net/orangehf
加密后的数据:-123 7 -64 -33 -29 32 107 -17 89 78 -78 -11 -125 -12 -59 -2 49 -112 -122 -91 -102 118 114 -11 -24 39 113 -14 66 37 -2 114
解密后的数据:www.blogjava.net/orangehf

转载于:https://www.cnblogs.com/langtianya/p/3874679.html

你可能感兴趣的文章
Django 里的HttpResponse 与 JsonResponse
查看>>
QQ,微信,新浪原生分享
查看>>
英语语法最终珍藏版笔记- 23连接词
查看>>
要在实践中去学习
查看>>
nodejs(1) fs 和 path
查看>>
演练5-2:Contoso大学校园管理2
查看>>
[LeetCode]2. Add Two Numbers
查看>>
C# 窗体相关知识
查看>>
Tensorflow学习笔记3:TensorBoard可视化学习
查看>>
WISCO信息组NOIP模拟赛-部落冲突
查看>>
Linux网络编程——原始套接字能干什么?
查看>>
Android环境变量的设置
查看>>
所谓“学术境界”
查看>>
获取时间
查看>>
设计模式之代理模式
查看>>
看盘经验一
查看>>
2015.11.23(利亚德在11点涨停,结果大笔进货,结果又下来了——被套)
查看>>
Linux下的SVN服务器搭建
查看>>
工作中几个点回想
查看>>
配合crond服务实现自定义周期备份MySQL数据库(使用innobackupex进行备份)
查看>>