Cross-platform encryption algorithm Xxtea encapsulation

Source: Internet
Author: User

cross-platform encryption algorithm Xxtea encapsulation

The structure of the Xxtea algorithm is very simple and requires only the addition, XOR, and storage of the hardware, and the software implementation of the code is very short, portable.

Wikipedia address: Http://en.wikipedia.org/wiki/XXTEA

Xxtea can be used for fast and easy encryption and decryption of continuous memory data, and it is more secure, but its use is not very convenient, so this package.

The encapsulated interfaces are as follows:

xxtea.h////Created by yuming on 12-7-24.//Copyright (c) 2012 __mycompanyname__. All Rights reserved.///* * This class is an encapsulation of the Xxtea official algorithm, Xxtea information see Http://en.wikipedia.org/wiki/XXTEA */#ifndef Xxtea_h#define Xxtea_h#include "Stdlib.h" #include "stdio.h" #include <string.h> #ifndef xxtea_type#define xxtea_type int//One    32-bit Long data type, because int in 32bit and 64bit system are 32 bits, so directly with Int#endifclass Xxtea {Public:xxtea ();    ~xxtea ();     Public:/** * @brief Get the length of the output buffer * * @param nlength Input buffer length * @param isencode TRUE is encryption, FALSE is decryption * * @return returns the desired length */static size_t xxteagetbufferlength (size_t nlength, BOOL        Isencode);     /** * @brief Encrypt the data stream * * @param lpdstbuffer Target buffer * @param lpsrcbuffer Source Buffer * @param nlength Source buffer length * @param lpkey Key * * @return Encryption succeeded */static BOOL Xxteaencode (char* lpdstbuffer, const char* Lpsrcbuffer, size_t Nlength, const xxtea_type* lpkey);     /** * @brief decrypt the data stream * * @param lpdstbuffer Target buffer * @param lpsrcbuffer Source Buffer * @param nlength Source buffer length * @param lpkey Key * * @return decryption succeeded */static B     Ool Xxteadecode (char* lpdstbuffer, const char* lpsrcbuffer, size_t nlength, const xxtea_type* lpkey);p rivate:/** * @brief Xxtea The official algorithm, this class is to encapsulate it, see Http://en.wikipedia.org/wiki/XXTEA * * @param v plus decrypt the data stream * @p    Aram N plus decryption length, n > 1 for encryption, n <-1 for decryption * @param k Key Note: Form parameter K corresponds to K in MX * * @return    Returns 0 for the decryption success, 1 for failure */static long Btea (xxtea_type* V, long n, const xxtea_type* k);     /** * @brief Normal string with 16 binary save * * @param surbuffer Source buffer * @param length source buffer lengths * @param dstbuffer Target buffer */static void Stringtohex (const char* surbuffer, int length, char* dstbuffer)    ;   /**  * @brief 16 binary string parsed into normal string * * @param surbuffer Source buffer * @param dstbuffer target Buffer * * @return The length of the target buffer */static int hextostring (const char* surbuffer, char* dstbuffer);}; #endif

Cryptographic decryption Sample code:

    Char srcbuffer[] = "Hello world! How are you doing! ";    size_t length = Xxtea::xxteagetbufferlength (strlen (Srcbuffer) + 1, true);    char* Encodebuffer = new Char[length];    Xxtea_type Key[4] = {0x12345678, 0X734A67FC, 0x3367a642, 0x78432562};    Xxtea::xxteaencode (Encodebuffer, Srcbuffer, strlen (srcbuffer) +1, key);    printf ("Encodebuffer:%s\r\n", encodebuffer);    char* Decodebuffer =  new Char[xxtea::xxteagetbufferlength (length, false)];    Xxtea::xxteadecode (Decodebuffer, Encodebuffer, length, key);    printf ("Decodebuffer:%s\r\n", decodebuffer);    Delete Encodebuffer;    Delete Decodebuffer;

Example results:

Demo Download: Http://files.cnblogs.com/pure/testXXTEA.zip

Cross-platform encryption algorithm Xxtea encapsulation

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.