Xxtea Encryption Algorithm

Source: Internet
Author: User

/*
* It should be noted that the encrypted byte array is not a string. Because the C # string is saved according to unicode encoding.
* To encrypt a string, use the getbytes method of system. Text. encoding. utf8 (or other encoders) to convert it into a byte array before encryption. The same is true for keys.
* The key length is 128 bits, that is, the byte array of 16 elements, but it can work normally if it is less than 16 bytes or more than 16 bytes,
* When the number of bytes is less than 16, it is automatically filled with 16 bytes by Zeros. elements after more than 16 bytes are ignored.
* Note that the encrypted content is also a byte array, but you cannot convert it to a string using system. Text. encoding. utf8. Otherwise, information will be lost.
* System. Text. Encoding encoder = system. Text. encoding. utf8;
* Byte [] DATA = xxtea. Encrypt (encoder. getbytes (textbox1.text), encoder. getbytes ("1234567890 abcdef "));
* Textbox2.text = system. Convert. tobase64string (data );
**/
Using system;

Class xxtea
{
Public static byte [] encrypt (byte [] data, byte [] key)
{
If (data. Length = 0)
{
Return data;
}
Return tobytearray (encrypt (touint32array (data, true), touint32array (Key, false), false );
}
Public static byte [] decrypt (byte [] data, byte [] key)
{
If (data. Length = 0)
{
Return data;
}
Return tobytearray (decrypt (touint32array (data, false), touint32array (Key, false), true );
}

Public static uint32 [] encrypt (uint32 [] V, uint32 [] K)
{
Int32 n = V. Length-1;
If (n <1)
{
Return V;
}
If (K. Length <4)
{
Uint32 [] Key = new uint32 [4];
K. copyto (Key, 0 );
K = key;
}
Uint32 z = V [N], y = V [0], Delta = 0x9e3779b9, sum = 0, E;
Int32 p, q = 6 + 52/(n + 1 );
While (Q --> 0)
{
Sum = unchecked (sum + delta );
E = sum> 2 & 3;
For (P = 0; P <n; P ++)
{
Y = V [p + 1];
Z = unchecked (V [p] + = (z> 5 ^ y <2) + (Y> 3 ^ z <4) ^ (sum ^ y) + (K [P & 3 ^ e] ^ z ));
}
Y = V [0];
Z = unchecked (V [N] + = (z> 5 ^ y <2) + (Y> 3 ^ z <4) ^ (sum ^ y) + (K [P & 3 ^ e] ^ z ));
}
Return V;
}
Public static uint32 [] decrypt (uint32 [] V, uint32 [] K)
{
Int32 n = V. Length-1;
If (n <1)
{
Return V;
}
If (K. Length <4)
{
Uint32 [] Key = new uint32 [4];
K. copyto (Key, 0 );
K = key;
}
Uint32 z = V [N], y = V [0], Delta = 0x9e3779b9, sum, E;
Int32 p, q = 6 + 52/(n + 1 );
Sum = unchecked (uint32) (Q * delta ));
While (sum! = 0)
{
E = sum> 2 & 3;
For (P = N; P> 0; p --)
{
Z = V [p-1];
Y = unchecked (V [p]-= (z> 5 ^ y <2) + (Y> 3 ^ z <4) ^ (sum ^ y) + (K [P & 3 ^ e] ^ z ));
}
Z = V [N];
Y = unchecked (V [0]-= (z> 5 ^ y <2) + (Y> 3 ^ z <4) ^ (sum ^ y) + (K [P & 3 ^ e] ^ z ));
Sum = unchecked (Sum-delta );
}
Return V;
}
Private Static uint32 [] touint32array (byte [] data, Boolean includelength)
{
Int32 n = (data. Length & 3) = 0 )? (Data. length> 2): (data. length> 2) + 1 ));
Uint32 [] result;
If (includelength)
{
Result = new uint32 [n + 1];
Result [N] = (uint32) data. length;
}
Else
{
Result = new uint32 [N];
}
N = data. length;
For (int32 I = 0; I <n; I ++)
{
Result [I> 2] | = (uint32) data [I] <(I & 3) <3 );
}
Return result;
}
Private Static byte [] tobytearray (uint32 [] data, Boolean includelength)
{
Int32 N;
If (includelength)
{
N = (int32) data [data. Length-1];
}
Else
{
N = data. Length <2;
}
Byte [] result = new byte [N];
For (int32 I = 0; I <n; I ++)
{
Result [I] = (byte) (data [I> 2]> (I & 3) <3 ));
}
Return result;
}
}

 

Call:

Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
{
System. Text. Encoding encoder = system. Text. encoding. utf8;
Byte [] DATA = xxtea. Encrypt (encoder. getbytes (textbox1.text), encoder. getbytes ("1234567890 abcdef "));
Textbox2.text = system. Convert. tobase64string (data );
}

Private void button2_click (Object sender, eventargs E)
{
System. Text. Encoding encoder = system. Text. encoding. utf8;
Try
{
Textbox2.text = encoder. getstring (xxtea. decrypt (system. Convert. frombase64string (textbox1.text), encoder. getbytes ("1234567890 abcdef ")));
}
Catch (exception)
{
MessageBox. Show ("your input is incorrect! ");
}
}
}

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.