修改加密算法,减少临时数组的使用

back_recharge
王永芳 2019-06-10 14:35:54 +08:00
parent eac6670f19
commit 87d27731de
1 changed files with 18 additions and 43 deletions

View File

@ -46,62 +46,40 @@ public class Tea {
bys[offset] = (byte) ((content >> 24) & 0xff);
}
//byte[]型数据转成int[]型数据
private static int[] byteToIntArray(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] = byteToInt(content, j);
}
return result;
}
//int[]型数据转成byte[]型数据
private static byte[] intArrayToByte(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) {
intToByte(result, j, content[i]);
}
return result;
}
//加密
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)
for(int m=offset; m<content.length; m+=8)
{
int y = tempInt[m], z = tempInt[m+1], sum = 0, i;
int y = byteToInt(content, m), z = byteToInt(content, m+4), 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;
intToByte(content, m, y);
intToByte(content, m+4, z);
}
return intArrayToByte(tempInt, 0);
return content;
}
//解密
public static byte[] decrypt(byte[] encryptContent, int offset, int[] key, int times){
int[] tempInt = byteToIntArray(encryptContent, 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 = 0xC6EF3720, i;
for(int m=offset; m<encryptContent.length; m+=8) {
int y = byteToInt(encryptContent, m), z = byteToInt(encryptContent, m+4), 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;
}
return intArrayToByte(tempInt, 0);
intToByte(encryptContent, m, y);
intToByte(encryptContent, m+4, z);
}
return encryptContent;
}
public static void shortToByte(byte[] bys, int offset, int content) {
@ -115,16 +93,13 @@ public class Tea {
}
public static byte[] encrypt2(byte[] content, int[] key) {
byte[] bys = content;
if (bys.length % 8 != 0) {
bys = new byte[(bys.length + 7) / 8 * 8];
System.arraycopy(content, 0, bys, 0, content.length);
}
byte[] encryptContent = encrypt(bys, 0, key, TIMES);
byte[] result = new byte[encryptContent.length + 2];
shortToByte(result, 0, content.length);
System.arraycopy(encryptContent, 0, result, 2, encryptContent.length);
return result;
int tobalLen = (content.length+7)/8*8 + 2;
byte[] bys = new byte[tobalLen];
System.arraycopy(content, 0, bys, 2, content.length);
byte[] encryptContent = encrypt(bys, 2, key, TIMES);
shortToByte(encryptContent, 0, content.length);
return encryptContent;
}