Xxtea encryption Algorithm C # C # compatible version number

Source: Internet
Author: User
Tags rounds

1. A good reversible encryption algorithm Xxtea


Before inadvertently saw Texturepacker used Xxtea to encrypt the picture resources. So it took some time to see the next Xxtea.

There are generally two cryptographic algorithms: 1. Like MD5,SHA1, the hash algorithm is irreversible. This is the username and password that are stored in the general database.

2. The Xxtea algorithm mentioned in this paper is reversible and has a key that can be encrypted.

The security aspect is quite a lot of things, when it comes to key, what public key, private key, I have returned to the teacher. Forget it.


The demand for reversible encryption algorithms is still relatively broad, such as Encrypting game archives (discovering or not preventing memory changes), encrypting logs, encrypting pictures, and so on.

Reversible encryption algorithm My requirements are simpler:

1. Safe enough, 2. Fast, 3. Cross-language


2.XXTEA Code

#include <stdint.h> #define DELTA 0x9e3779b9 #define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((s    Um^y) + (key[(p&3) ^e] ^ z)) void Btea (uint32_t *v, int n, uint32_t const key[4]) {uint32_t y, z, sum;    Unsigned p, rounds, E;      if (n > 1) {/* Coding part */rounds = 6 + 52/n;      sum = 0;      z = v[n-1];        do {sum + = DELTA;        E = (sum >> 2) & 3;           for (p=0; p<n-1; p++) {y = v[p+1];        z = v[p] + = MX;        } y = v[0];      z = v[n-1] + = MX;    } while (--rounds);      } else if (n <-1) {/* decoding part */n = n;      Rounds = 6 + 52/n;      sum = Rounds*delta;      y = v[0];        do {e = (sum >> 2) & 3;          for (p=n-1; p>0; p--) {z = v[p-1];        y = v[p]-= MX;        } z = v[n-1];      y = v[0]-= MX;    } while ((Sum-= DELTA)! = 0); }  }

This is a copy of the code on the wiki, can see the code is very short, encryption and decryption together is only this point.

C language is really not familiar with the internet to find a C + + and C # code, found to write a good. Ability to encrypt random strings (including Chinese) and string as key. The result is that the Base64 becomes a simple string, which is easier to handle.

int _tmain (int argc, _tchar* argv[]) {string paintext = "Hello world"; char * temp = (char *) paintext.c_str (); string key = "1234567890abcdef";//Key cout<< "Pain text:" <<painText<<endl;    string result = Xxtea_encrypt (Paintext,key);//encryption cout<< "Encrypt result:" <<result<<endl;string result_1 = Xxtea_decrypt (Result,key);//decryption cout<< "Decrypt result:" <<result_1<<endl;system ("Pause" ); return 0;}

Try to find the next Java version number, do not know why incompatible, look later.


3.

The resources are downloaded by HTTP://MY.CSDN.NET/JXJGSSYLSG.

http://www.waitingfy.com/?attachment_id=1159


References:

Xxtea reversible encryption and decryption algorithm

Xxtea encryption Algorithm C # C # compatible version number

Related Article

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.