Tea encryption algorithm-Java version

Source: Internet
Author: User
Tags rounds

This algorithm is simple and efficient. You can operate 8 bytes of data each time. The key for encryption and decryption is 16 bytes, that is, an int array containing 4 int data, the number of encryption rounds should be a multiple of 8. Generally, the number of commonly used rounds is 64, 32, 16. We recommend that you use 64.

The source code is as follows:
/***//**
* Tea Algorithm
* Each operation can process 8 bytes of data
* The Key is 16 bytes. It should contain four int types of int [], and one Int Is 4 bytes.
* The number of encryption and decryption rounds should be a multiple of 8. We recommend that you set the number of encryption rounds to 64.
**/
Public class tea {
// Encryption
Public byte [] encrypt (byte [] content, int offset, int [] key, int times) {// times indicates the number of encryption cycles.
Int [] tempint = bytetoint (content, offset );
Int y = tempint [0], Z = tempint [1], sum = 0, I;
Int Delta = 0x9e3779b9; // This is the standard value of the algorithm.
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 );
}
// Decrypt
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; // This is the standard value of the algorithm.
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 );
}
// Convert byte [] type data to int [] type data
Private int [] bytetoint (byte [] content, int offset ){

Int [] result = new int [content. length> 2]; // divide by the N power of 2 = shifted right to N, that is, 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;

}
// Convert int [] type data to byte [] type data
Private byte [] inttobyte (INT [] content, int offset ){
Byte [] result = new byte [content. length <2]; // multiply by the Npower of 2 = shift left N bits, that is, 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;
}
// If a byte is interpreted as negative, it must be converted to an unsigned positive number.
Private Static int transform (byte temp ){
Int tempint = (INT) temp;
If (tempint <0 ){
Tempint + = 256;
}
Return tempint;
}

}
The test code is as follows:
Public static void main (string [] ARGs ){

Int [] Key = new int [] {// key used for encryption and decryption
0x789f5645, 0xf68bd5a4,
0x81963ffa, 0x458fac58
};
TEA Tea = new tea ();

Byte [] info = new byte [] {

1, 2, 3, 4, 5, 6, 7, 8
};
System. Out. Print ("original data :");
For (byte I: INFO)
System. Out. Print (I + "");
System. Out. println ();

Byte [] secretinfo = tea. Encrypt (Info, 0, key, 32 );
System. Out. Print ("encrypted data :");
For (byte I: secretinfo)
System. Out. Print (I + "");
System. Out. println ();

Byte [] decryptinfo = tea. decrypt (secretinfo, 0, key, 32 );
System. Out. Print ("decrypted data :");
For (byte I: decryptinfo)
System. Out. Print (I + "");

}
The output result is as follows:

Original data: 1 2 3 4 5 6 7 8
Encrypted data: 92 124-3-125-115 82 21 28
Decrypted data: 1 2 3 4 5 6 7 8

This is only one encryption and decryption operation. If you want to process data larger than 8 bytes at a time, you need to encapsulate it again.

The encapsulated code is as follows:
// Encrypt information using the tea Algorithm
Private byte [] encryptbytea (string info ){
Byte [] temp = info. getbytes ();
Int n = 8-Temp. Length % 8; // The number of digits to be filled if the number of digits of temp is less than a multiple of 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;
}
// Decrypt the information through the tea Algorithm
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 );

}
The test code is as follows:
Public static void main (string [] ARGs ){
String info = "www.blogjava.net/orangehf ";
System. Out. println ("original data:" + info );

Savefileio IO = new savefileio ();

Byte [] encryptinfo = Io. encryptbytea (Info );
System. Out. Print ("encrypted data :");
For (byte I: encryptinfo)
System. Out. Print (I + "");
System. Out. println ();

String decryptinfo = Io. decryptbytea (encryptinfo );
System. Out. Print ("decrypted data :");
System. Out. println (decryptinfo );

}
The output result is as follows:
Original data: www.blogjava.net/orangehf
Encrypted data: -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
Decrypted data: www.blogjava.net/orangehf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.