修改加密解密

back_recharge
wangyuan 2019-05-06 12:03:45 +08:00
parent f85bdde48e
commit db08e4a7f8
1 changed files with 25 additions and 21 deletions

View File

@ -65,37 +65,41 @@ public class Tea {
return result;
}
//加密
public static byte[] encrypt(byte[] content, int offset, int[] key, int times) {//times为加密轮数
int[] tempInt = byteToIntArray(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);
//加密
public static byte[] encrypt(byte[] content, int offset, int[] key, int times){//times为加密轮数
int[] tempInt = byteToIntArray(content, offset);
int delta=0x9e3779b9; //这是算法标准给的值
int a = key[0], b = key[1], c = key[2], d = key[3];
for(int m=0; m<tempInt.length; m+=2)
{
int y = tempInt[m], z = tempInt[m+1], sum = 0, i;
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[m]=y;
tempInt[m+1]=z;
}
tempInt[0] = y;
tempInt[1] = z;
return intArrayToByte(tempInt, 0);
}
//解密
public static byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times) {
public static byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times){
int[] tempInt = byteToIntArray(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;
for(int m=0; m<tempInt.length; m+=2) {
int y = tempInt[m], z = tempInt[m+1], sum = 0xC6EF3720, i;
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[m] = y;
tempInt[m+1] = z;
}
tempInt[0] = y;
tempInt[1] = z;
return intArrayToByte(tempInt, 0);
}