TEA密碼編譯演算法-Java版

來源:互聯網
上載者:User

  這個演算法簡單,而且效率高,每次可以操作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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.