A simple and effective source code encryption algorithm-tea and Xtea algorithm

Source: Internet
Author: User

Recently in the project to use the Xtea source encryption algorithm, write down to summarize:

Tea (Tiny encryption algorithm) is a small symmetric encryption and decryption algorithm that supports 128-bit ciphers, which can only encrypt/decrypt 8 bytes of data at a time, like Blowfish. Tea is characterized by its fast speed, high efficiency and simple implementation. Tea has also developed several versions, namely Xtea, Block tea and Xxtea, as attacks on tea continue to occur.

Tea algorithm implementation is very simple, less than 20 code, sharing a reinforced version of the tea algorithm Xtea algorithm implementation:

/************************************************************************

Copyright 2006-2007 Ma Bingyao

These sources is a free software. Redistributions of source code must
Retain the above copyright notice. Redistributions in binary form
Must reproduce the above copyright notice. You can redistribute it
Freely. You can use it with any free or commercial software.

These sources is distributed in the hope that it'll be useful,
But without any WARRANTY. Without even the implied warranty of
merchantability or FITNESS for A particular PURPOSE.

The author by:
e-mail: [Email protected]

*************************************************************************/

#ifndef xxtea_h
#define Xxtea_h

#include <stddef.h>/** for size_t & NULL declarations */

#if defined (_msc_ver)

typedef unsigned __INT32 Xxtea_long;

#else

#if defined (__freebsd__) && __freebsd__ < 5
/** FreeBSD 4 doesn ' t have stdint.h file */
#include <inttypes.h>
#else
#include <stdint.h>
#endif

typedef uint32_t Xxtea_long;

#endif/** end of If defined (_msc_ver) */

#define XXTEA_MX (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z)
#define XXTEA_DELTA 0X9E3779B9

void Xxtea_long_encrypt (Xxtea_long *v, Xxtea_long len, Xxtea_long *k);
void Xxtea_long_decrypt (Xxtea_long *v, Xxtea_long len, Xxtea_long *k);

#endif

/************************************************************************

Copyright 2006-2007 Ma Bingyao

These sources is a free software. Redistributions of source code must
Retain the above copyright notice. Redistributions in binary form
Must reproduce the above copyright notice. You can redistribute it
Freely. You can use it with any free or commercial software.

These sources is distributed in the hope that it'll be useful,
But without any WARRANTY. Without even the implied warranty of
merchantability or FITNESS for A particular PURPOSE.

The author by:
e-mail: [Email protected]

*************************************************************************/
#include "Xxtea.h"

void Xxtea_long_encrypt (Xxtea_long *v, Xxtea_long len, Xxtea_long *k) {
Xxtea_long n = len–1;
Xxtea_long z = v[n], y = v[0], p, q = 6 +/(n + 1), sum = 0, E;
if (n < 1) {
Return
}
while (0 < Q –) {
sum + = Xxtea_delta;
e = Sum >> 2 & 3;
for (p = 0; p < n; p++) {
y = v[p + 1];
z = v[p] + = XXTEA_MX;
}
y = v[0];
z = v[n] + = XXTEA_MX;
}
}

void Xxtea_long_decrypt (Xxtea_long *v, Xxtea_long len, Xxtea_long *k) {
Xxtea_long n = len–1;
Xxtea_long z = v[n], y = v[0], p, q = 6 +/(n + 1), sum = q * Xxtea_delta, E;
if (n < 1) {
Return
}
while (sum! = 0) {
e = Sum >> 2 & 3;
for (p = n; p > 0; p –) {
z = v[p-1];
y = v[p]-= XXTEA_MX;
}
z = v[n];
y = v[0]-= XXTEA_MX;
Sum-= Xxtea_delta;
}
}

A simple and effective source code encryption algorithm-tea and Xtea algorithm

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.