找回密码
 注册
搜索
查看: 37522|回复: 6

TEA加密算法的C/C++实现

[复制链接]
发表于 2010-1-19 17:58:25 | 显示全部楼层 |阅读模式
TEA(Tiny Encryption Algorithm) 是一种简单高效的加密算法,以加密解密速度快,实现简单著称。算法真的很简单,TEA算法每一次可以操作64-bit(8-byte),采用128-bit(16-byte)作为key,算法采用迭代的形式,推荐的迭代轮数是64轮,最少32轮。目前我只知道QQ一直用的是16轮TEA。没什么好说的,先给出C语言的源代码(默认是32轮):
4 m2 J. o; F* _' B) `$ I/ S+ I$ R/ R微型加密算法(TEA)及其相关变种(XTEA,Block TEA,XXTEA) 都是分组加密算法,它们很容易被描述,实现也很简单(典型的几行代码)。 : R7 s/ I" l9 b, N4 G
TEA 算法最初是由剑桥计算机实验室的 David Wheeler 和 Roger Needham 在 1994 年设计的。该算法使用 128 位的密钥为 64 位的信息块进行加密,它需要进行 64 轮迭代,尽管作者认为 32 轮已经足够了。该算法使用了一个神秘常数δ作为倍数,它来源于黄金比率,以保证每一轮加密都不相同。但δ的精确值似乎并不重要,这里 TEA 把它定义为 δ=「(√5 - 1)231」(也就是程序中的 0×9E3779B9)。
9 K$ S$ T% ?2 [: l6 r- m; p( G之后 TEA 算法被发现存在缺陷,作为回应,设计者提出了一个 TEA 的升级版本——XTEA(有时也被称为“tean”)。XTEA 跟 TEA 使用了相同的简单运算,但它采用了截然不同的顺序,为了阻止密钥表攻击,四个子密钥(在加密过程中,原 128 位的密钥被拆分为 4 个 32 位的子密钥)采用了一种不太正规的方式进行混合,但速度更慢了。
* G9 N# ~; _1 j& f在跟描述 XTEA 算法的同一份报告中,还介绍了另外一种被称为 Block TEA 算法的变种,它可以对 32 位大小任意倍数的变量块进行操作。该算法将 XTEA 轮循函数依次应用于块中的每个字,并且将它附加于它的邻字。该操作重复多少轮依赖于块的大小,但至少需要 6 轮。该方法的优势在于它无需操作模式(CBC,OFB,CFB 等),密钥可直接用于信息。对于长的信息它可能比 XTEA 更有效率。 " s2 ~& f* ?$ B( c1 O
在 1998 年,Markku-Juhani Saarinen 给出了一个可有效攻击 Block TEA 算法的代码,但之后很快 David J. Wheeler 和 Roger M. Needham 就给出了 Block TEA 算法的修订版,这个算法被称为 XXTEA。XXTEA 使用跟 Block TEA 相似的结构,但在处理块中每个字时利用了相邻字。它利用一个更复杂的 MX 函数代替了 XTEA 轮循函数,MX 使用 2 个输入量。
发表于 2010-1-19 19:47:00 | 显示全部楼层
  1.   c$ U( B5 X& B
  2. void encrypt(unsigned long *v, unsigned long *k) {
    ; }9 n# O8 e' i0 s) L
  3.      unsigned long y=v[0], z=v[1], sum=0, i;         /* set up */ 5 N1 x$ ?2 X) J% \+ Q! b
  4.      unsigned long delta=0x9e3779b9;                 /* a key schedule constant */
    / d. s/ R( o* c
  5.      unsigned long a=k[0], b=k[1], c=k[2], d=k[3];   /* cache key */
    3 b2 v$ g; [0 N
  6.      for (i=0; i < 32; i++) {                        /* basic cycle start */
    5 g' [& V; N- m0 S: c
  7.          sum += delta; ( U% w0 q* T6 v. E( f) k# h9 q9 U- k
  8.          y += ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
    ' y; y7 W' y5 P8 B& o5 t& ~
  9.          z += ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);/* end cycle */ + f4 `0 m) ]8 {
  10.      } 3 @0 G+ Q+ [. m" W! \" X/ a. |$ A" C
  11.      v[0]=y;
    / y  c9 N' @6 ]$ ~* y5 z* t
  12.      v[1]=z; & n8 q. C0 c- P' q
  13. } 0 ?. Y  P! i3 V& T
  14.   
    & v+ w, A. r, J! R9 h' Q( y+ j
  15. void decrypt(unsigned long *v, unsigned long *k) {
    . m6 t! l' J: b9 w3 J5 `$ \( z, C
  16.      unsigned long y=v[0], z=v[1], sum=0xC6EF3720, i; /* set up */
    1 R9 U! j- M1 g" Q
  17.      unsigned long delta=0x9e3779b9;                  /* a key schedule constant */ 5 E) u3 v) A: L" h  B
  18.      unsigned long a=k[0], b=k[1], c=k[2], d=k[3];    /* cache key */ / E. y( ]9 M0 H+ w3 }& X7 s& j
  19.      for(i=0; i<32; i++) {                            /* basic cycle start */ # V8 ?5 D; G& p4 z6 f
  20.          z -= ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d); ) q, |# V7 Q+ [8 A
  21.          y -= ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b); : w7 l1 c" \: O# L
  22.          sum -= delta;                                /* end cycle */
    1 |4 c( Z% K! @
  23.      }
    2 H3 p# _( g5 l
  24.      v[0]=y; # |+ q, V7 G' \; f$ }6 H: ?5 j
  25.      v[1]=z; / S2 ~7 A5 r8 M4 g; V4 W. Q% @
  26. }
    4 _8 @4 O. F7 }, Y" j0 K
复制代码
C语言写的用起来当然不方便,没关系,用C++封装以下就OK了:
回复

使用道具 举报

发表于 2010-1-19 19:48:25 | 显示全部楼层

unit.h

#ifndef UTIL_H
$ L% S/ y1 N- W' e% W/ O2 T! x4 Q#define UTIL_H
$ O  Y7 D# @7 W& l
4 Y% N7 s! u' h( S  l#include <string>
9 _" w5 p% P( n#include <cmath>
& ]( e* K/ E$ r% n6 n! k#include <cstdlib>
8 C7 ^. s! K9 h) `2 K3 z  ?! L ! N( H1 p# e! q/ H$ B
typedef unsigned char byte;
6 h% {/ i. m7 B4 i: f* ntypedef unsigned long ulong;
) t5 X: I! x* g3 _$ w( A
8 G, w' G6 M) M' Kinline double logbase(double base, double x) {
. o6 X4 v" O" ~$ ]$ Q) S% @: o    return log(x)/log(base); * F& a6 n9 n: z/ T
}
7 ^% ?8 r6 |( B2 N  a4 p % W+ [9 }1 U/ a# z' h* b7 _( t
/*
# q3 @9 N$ r- {- j*convert int to hex char. " }, k2 W% l8 z2 b3 y& M
*example:10 -> 'A',15 -> 'F'
) A9 m- d' [) V! c( H. {* ^" b8 v2 r5 D*/
# [1 ~. d7 ^' [( ~! `* Hchar intToHexChar(int x); * ^$ ?5 G( y8 A8 B  l  X$ x( y, g
( B  H% ~2 b5 @8 C3 H
/*
; P- M: K1 Y/ C' J*convert hex char to int.
7 T' J- ^7 m" n* d$ \*example:'A' -> 10,'F' -> 15
, a5 w* F/ i2 |9 f5 L5 g0 T9 l: G*/ ' |# W1 O% @4 Q3 k0 p
int hexCharToInt(char hex);
6 _% o# J% A: s/ Q2 x
$ }4 k" e5 @0 n8 F6 n. Susing std::string;
* I# H0 `$ Q. e! S  b, b/*
- j4 x/ ^8 U  f# I2 H*convert a byte array to hex string.
1 P. n- Z/ u7 ~# P*hex string format example:"AF B0 80 7D"
. j: E7 g& N8 e+ P2 w8 |/ f1 D*/
# \- o4 J' |; j: fstring bytesToHexString(const byte *in, size_t size); # o. c1 F8 h! g' U% ]
( g2 W0 u+ U# A
/*
& o( M9 w, Q2 Y0 X& w*convert a hex string to a byte array.
5 _0 H% {8 N+ v1 u. k! N0 v*hex string format example:"AF B0 80 7D" 4 z) V9 q7 \! e6 y0 w+ r1 \! G
*/ ) }  g( d) X. c6 p& l0 H/ c1 K1 H
size_t hexStringToBytes(const string &str, byte *out);
0 [$ A( m) L! T5 i) e; j$ V) N! U
0 X0 u" v3 u6 _. Z- z& r  \: f#endif/*UTIL_H*/
回复

使用道具 举报

发表于 2010-1-19 19:51:31 | 显示全部楼层

util.cpp

#include "util.h"
5 V5 o- j8 j( ]$ k8 W9 Y#include <vector>
# n* v( y' F5 | 9 P9 L9 w- [, I( j0 k
using namespace std;
" O. P1 [# g9 ]4 h3 |+ B
8 t/ o/ G" Q1 R0 t, zchar intToHexChar(int x) { 8 M: |) Q4 i5 h2 W7 D
    static const char HEX[16] = {
$ J3 [9 B3 m2 u/ A3 I        '0', '1', '2', '3',
- z3 U$ l+ u) k/ q' b        '4', '5', '6', '7', 4 ?, g* t' |, b0 |/ {, m2 O/ J
        '8', '9', 'A', 'B', 0 u! _% q$ \2 j6 u! S  n
        'C', 'D', 'E', 'F'
- N# ~# h, p/ l8 F0 {# J    };
/ b& }& J' O5 i    return HEX[x]; 0 L. a0 e& R, {- Z9 }4 r9 F1 e
}
! e2 s8 N! I+ b- H$ b9 f& E: e 3 O  y! H; U+ m9 h
int hexCharToInt(char hex) { " ]  ^  X( a2 q6 x- f9 P" [) O
    hex = toupper(hex); 8 r) z$ B% h; N* M7 {: w0 d  t
    if (isdigit(hex))
% k7 ~5 N3 P( S( S        return (hex - '0');
! u2 F2 d$ a: e0 G3 f+ C; Z    if (isalpha(hex))
% m+ y1 ~8 J7 `        return (hex - 'A' + 10);   [! o, m$ S; L+ {
    return 0; ( n, J6 D: N1 @: c0 ^  f
}
8 m1 N9 E. t/ V; @ % N. \; C# y2 {4 A9 y. l7 u
string bytesToHexString(const byte *in, size_t size) {
" e/ G) i( _7 f9 C/ s) x9 [    string str; 8 s6 t3 D- J  p. v2 Y7 o/ D
    for (size_t i = 0; i < size; ++i) { ! P& Z- ~% T. Z  T( ~
        int t = in[i];
) B! K- H' d# e* d        int a = t / 16; ; l* t  U# n6 X
        int b = t % 16;
( N& U( f1 P6 e) n& I+ n4 l3 ~5 R        str.append(1, intToHexChar(a)); 9 q/ d3 p6 a# u/ n# P
        str.append(1, intToHexChar(b));
( j5 T4 G% V4 h; q9 E        if (i != size - 1)
5 z- e7 c8 z8 m' U: M8 J1 P            str.append(1, ' ');
5 {" J- y/ W- X    }
" n$ L4 u9 x/ O7 }    return str;
7 w. c4 y' G+ t8 C} : Q% B6 p; V9 ^$ \( x% X
, f, D: I% L8 b6 Q
size_t hexStringToBytes(const string &str, byte *out) { ' s% M+ W8 J# J) D( B* d
- C$ Y  D0 y# E# r
    vector<string> vec; / e0 \* N: M! U
    string::size_type currPos = 0, prevPos = 0; # ?5 U0 z" E; \5 r) U8 L
    while ((currPos = str.find(' ', prevPos)) != string::npos) { * d/ Y8 S7 x$ G/ d# G3 |" n
        string b(str.substr(prevPos, currPos - prevPos)); 4 i2 F+ |" ?4 a# D
        vec.push_back(b);
( ~, s3 P, x0 e* v' z        prevPos = currPos + 1; : @5 C8 a* W) |+ C7 F  @1 l
    } 5 M2 E  P& s8 ~$ ^0 f
    if (prevPos < str.size()) { . `# L4 u2 x; g
        string b(str.substr(prevPos));
2 w# v# ?) I  o        vec.push_back(b);
) W( r' C& A" Z2 `0 C6 X2 ]& n* {    }
8 ^& f, ^, V; E& @    typedef vector<string>::size_type sz_type; 0 ^/ O  M1 y9 t
    sz_type size = vec.size();
; \6 ^6 G$ I* c3 I. ~    for (sz_type i = 0; i < size; ++i) {
* w. A6 B) d# U: \        int a = hexCharToInt(vec[i][0]);
& i# p  @  i; h  u& q8 F, R  w0 Z: Z        int b = hexCharToInt(vec[i][1]); * `. b" o6 t8 R
        out[i] = a * 16 + b;
5 A  j; w2 h$ J- V& L2 e0 k/ s    }
. Y4 Y. h2 o8 z( s, a9 R    return size; 8 J- r( }' b0 q9 P$ W! h2 O
}
回复

使用道具 举报

发表于 2010-1-19 19:57:01 | 显示全部楼层

tea.h

#ifndef TEA_H ( [- F0 G0 z$ L9 b/ \
#define TEA_H
; V  D% ?2 f" U4 I 4 N- H2 i1 Y/ |
/* 0 f$ Y& R: b; ~$ r
*for htonl,htonl
7 K7 X3 `, e( q: k; k/ w& _) D*do remember link "ws2_32.lib" 3 R/ [9 f6 f/ ~! R% `; G0 T, j
*/ * g/ w. E# U+ l; E- f
#include <winsock2.h> - m1 A, |: d$ t" ~& _) H
#include "util.h"
) Q! D9 p, @" }5 ?3 @
- O8 ]8 \) C, R; K7 A* ~* eclass TEA {
" J9 l* C' P3 j9 N9 ?, w7 Cpublic: 8 j( K5 W0 c, H+ @, f1 X
    TEA(const byte *key, int round = 32, bool isNetByte = false); 1 L2 P, e& Y$ ]0 r! R7 e9 h
    TEA(const TEA &rhs);
! [* @6 L& `, j    TEA& operator=(const TEA &rhs);
( ?8 W. G6 m+ X0 D- h- t    void encrypt(const byte *in, byte *out);
* v8 \1 A3 G- y/ H! i  G1 G    void decrypt(const byte *in, byte *out);
8 N2 e! |  a  ~4 T  sprivate: - I, Z2 E8 G& J
    void encrypt(const ulong *in, ulong *out); 6 d& m7 v& ?) {# P- Y8 J
    void decrypt(const ulong *in, ulong *out);
; c% P3 L. D3 Y/ n) S# R0 s    ulong ntoh(ulong netlong) { return _isNetByte ? ntohl(netlong) : netlong; }
& ?7 d) u& j* l9 N1 e. y    ulong hton(ulong hostlong) { return _isNetByte ? htonl(hostlong) : hostlong; }
1 G3 G" ?5 ]" ?private:
+ E/ D. A5 M/ B    int _round; //iteration round to encrypt or decrypt - i4 l% S6 ?, M" a3 N4 o
    bool _isNetByte; //whether input bytes come from network
6 \+ \5 D/ \) W3 W; @    byte _key[16]; //encrypt or decrypt key
  |  \$ c5 R8 J$ M6 x};
- R* C& r) Q: d) B5 X ! E6 Y1 u- i7 l
#endif/*TEA_H*/
回复

使用道具 举报

发表于 2010-1-19 19:59:57 | 显示全部楼层

tea.cpp

1 #include "tea.h" # D: M4 k: w, |% A3 W; Z3 X
2 #include <cstring> //for memcpy,memset " R- I+ k" L* b; s# U* \. d
3  
( ~. j: \* M& }$ i 4 using namespace std;
2 C5 u. e3 s. ]7 o0 e+ s 5  4 g- J6 Y7 x7 G6 c( S$ q& ^" ~
6 TEA::TEA(const byte *key, int round /*= 32*/, bool isNetByte /*= false*/)
) h( ^8 d6 A. o- B( u7 x; O) S 7 :_round(round)
  ]- g5 L/ C4 p5 \8 c, ?, u 8 ,_isNetByte(isNetByte) { ( C, I3 U0 \6 l) I+ E& R
9     if (key != 0)
  l; }% e7 g9 y( @4 O; m10         memcpy(_key, key, 16);
! b" }* j# |/ {, j) l11     else
. X3 m; g3 i0 S$ F; E8 y, c! e12         memset(_key, 0, 16);
5 q+ Y. j0 b, F# z4 p  @! Z! c13 }
* x9 W: ]8 b. F! t  s14  5 S+ s5 ~8 h; F, R" ?% h
15 TEA::TEA(const TEA &rhs) & o. s- |$ s3 t$ }; p8 h2 S5 h
16 :_round(rhs._round) 5 K. i7 S! f) o; Y
17 ,_isNetByte(rhs._isNetByte) {
3 t6 \2 ~6 ~  n% {18     memcpy(_key, rhs._key, 16);
: a7 }! q/ R- H8 l5 K19 }
* z& G2 Q- M- C* T3 `0 \# A8 x20  
7 o! A: s% e1 s& Q5 ~  f0 H+ w21 TEA& TEA::operator=(const TEA &rhs) { 0 w" t7 D2 V2 W
22     if (&rhs != this) {
: y/ _5 Z& ^5 H' N# H( k23         _round = rhs._round; : \" q2 M+ Q; i- B
24         _isNetByte = rhs._isNetByte;
+ G/ S+ y& X0 R* l0 ]25         memcpy(_key, rhs._key, 16); ) d+ ?) s; M; @- F! L5 V' u
26     } * w$ w2 E0 [& p/ k' |0 H* ?" c! M
27     return *this;
$ j' g! o; z) q3 o  C$ i28 } ' \- J! w( w& d/ |$ y5 N% B/ {" z1 A
29  
5 \! F: N9 t1 r' ?8 F8 `* w30 void TEA::encrypt(const byte *in, byte *out) {
, q9 c. l! u2 s  x- \% T31     encrypt((const ulong*)in, (ulong*)out); ' g3 `/ t% ^; Z8 J
32 }
( z5 t% a$ L7 f" g& ~33  
% y3 N! O. j5 ^. H3 b- D34 void TEA::decrypt(const byte *in, byte *out) { ' I- Y) {5 J$ K3 C2 x# |( v
35     decrypt((const ulong*)in, (ulong*)out); 3 `, v& X$ Q: k, `# h  n1 ^  c
36 } 8 p& H. |3 D& r) N  H
37  4 S$ z; s0 o! ^# f- }8 {0 r7 L
38 void TEA::encrypt(const ulong *in, ulong *out) { " T# M$ \( I7 T, J3 W* H  s
39  
0 I* M/ t9 z4 ^* f  o( ~40     ulong *k = (ulong*)_key;
/ A% _& f8 r" L1 Q41     register ulong y = ntoh(in[0]); 5 i. B( T- u& T* e7 `6 u  x9 h" D
42     register ulong z = ntoh(in[1]); + c; F) O* L, e! l% l, Q% _
43     register ulong a = ntoh(k[0]);
' P- |; ]" y# v! A44     register ulong b = ntoh(k[1]); ' H8 `; J, l$ G" b# q
45     register ulong c = ntoh(k[2]); ' t( y8 ~' b( D# ^6 b7 k
46     register ulong d = ntoh(k[3]); 4 t# g# [$ v5 f) j2 {6 R
47     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */ 6 ~, A, C  C$ U. j6 A! Y
48     register int round = _round; ! z) f8 ~! a9 b
49     register ulong sum = 0;
2 R* l; `) ^/ R9 X+ _$ C50  . b( {, W3 |( d1 p( J' W" d3 u1 l
51     while (round--) {    /* basic cycle start */
% P) X& P0 O( W9 j4 U8 Z( V52         sum += delta; * m2 N! e( m8 n+ l2 i2 J5 c
53         y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); 5 T0 w% A2 H0 X. n& W$ S. Q, h
54         z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
" g  m! T( j- v# y1 \55     }    /* end cycle */
, j& O$ A+ t2 A1 y3 O56     out[0] = ntoh(y); 0 r# L5 d, X! C" j; G
57     out[1] = ntoh(z);
' l0 Q3 ~* f# Q58 } 0 q  v* b7 i3 \8 L
59  
; T' F! d" D- @2 |60 void TEA::decrypt(const ulong *in, ulong *out) {
( H; @! U" K2 j5 H# m& h1 p61  
4 n! {7 b% t  {* N62     ulong *k = (ulong*)_key;
) I- t7 {3 h% H2 n7 ]63     register ulong y = ntoh(in[0]); 3 }* c2 F- T' Y, \5 n
64     register ulong z = ntoh(in[1]);
. y; e* P. F2 ?6 Y65     register ulong a = ntoh(k[0]);
0 I% S8 N3 V  [. s66     register ulong b = ntoh(k[1]); 8 w& {- A' E0 Y; V% p0 m4 E/ P
67     register ulong c = ntoh(k[2]);
/ U: |; l/ j( Q- [68     register ulong d = ntoh(k[3]); : y7 T' P8 ^3 \$ h* n$ r" ?5 W
69     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */ 1 u9 W6 k9 t& x% q$ Q; _
70     register int round = _round; $ f/ o) \7 i( L: F6 P; T% l
71     register ulong sum = 0;
5 ?) v. s4 N7 h72  
: Q* P- [$ I1 H% a6 Q73     if (round == 32)
" M$ |* P1 \4 m0 _74         sum = 0xC6EF3720; /* delta << 5*/
( r1 ]" T0 ?7 Y4 M. B% c5 t75     else if (round == 16) ' K; J' F: ?* Q5 C" X
76         sum = 0xE3779B90; /* delta << 4*/ 6 H1 R8 x3 G" |* n. m9 `- i
77     else
1 S& R2 s4 V, O6 o78         sum = delta << static_cast<int>(logbase(2, round));
/ l% x) H, Y+ g0 t! @79  
# X8 Q3 r. @5 k/ r80     while (round--) {    /* basic cycle start */
. @% a) v6 k( `4 d4 D& M7 ~& U81         z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
# {, v9 M% n9 d$ J82         y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); * }4 R6 C. c4 Z9 K# y
83         sum -= delta;
" {0 Z( e7 U7 d+ c3 J" O) J( j84     }    /* end cycle */ 2 ~( x& \6 b; O+ H# R' w
85     out[0] = ntoh(y);
3 U2 r8 r- K' F( A- w2 a86     out[1] = ntoh(z); & h" w. c6 ?5 G% o" o4 w5 U) F. X) Q+ ~
87 }: c: y* m! E4 V3 r4 E7 C2 v- n

' k6 Q* V. ~" i  i% @, ?. ~) \需要说明的是TEA的构造函数:
  Q% {4 b# u& l6 cTEA(const byte *key, int round = 32, bool isNetByte = false);
6 l5 d7 W4 @0 L" ]1.key - 加密或解密用的128-bit(16byte)密钥。 4 v9 s( S- A! P0 U  ]4 I$ h4 o
2.round - 加密或解密的轮数,常用的有64,32,16。
7 q; I% E/ I1 W" L9 `3 O; Z4 J8 c) ?! S3.isNetByte - 用来标记待处理的字节是不是来自网络,为true时在加密/解密前先要转换成本地字节,执行加密/解密,然后再转换回网络字节。偷偷告诉你,QQ就是这样做的! + |6 P6 e9 R0 ~' H

- C, N! O4 q$ r# D最后当然少不了测试代码:
回复

使用道具 举报

发表于 2010-1-19 20:01:04 | 显示全部楼层

test.cpp

1 #include "tea.h" 4 G3 {5 c+ R) i: e; ^" x6 E' Q
2 #include "util.h" ! _4 p  p" u: h' E$ z
3 #include <iostream>
7 g# T1 f$ e8 B2 S2 y: K( c( v' a, X 4  
" O9 q& k' h2 }) i1 L 5 using namespace std;
! @# w  m8 ~1 ^  w( f  H 6  5 w; a* y) T8 U: s7 D2 ~5 G' ~
7 int main() { ( c" _) K7 L8 v, V
8  
) T0 k+ t. q5 E% ~6 o; M 9     const string plainStr("AD DE E2 DB B3 E2 DB B3"); + M2 h: j& G, c0 d0 v
10     const string keyStr("3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4");
  }" l- v. H5 o* E- B; G+ B11     const int SIZE_IN = 8, SIZE_OUT = 8, SIZE_KEY = 16; % w: h9 [% Y3 D. G
12     byte plain[SIZE_IN], crypt[SIZE_OUT], key[SIZE_KEY];
" n" u# ]# t( M/ }. s$ n, f13  
/ ^8 ?0 {* i7 J! `5 x5 D# p14     size_t size_in = hexStringToBytes(plainStr, plain);
/ b4 `: H) y* z% s$ Y' b15     size_t size_key = hexStringToBytes(keyStr, key); 6 O" q! B) ^; J# ~9 V# k; P
16  ! C# q" t' O% r
17     if (size_in != SIZE_IN || size_key != SIZE_KEY) 5 d, g, y9 \. l
18         return -1;
: I4 o6 l$ j  C- t5 A% j19  
, @% B: C# s* I9 M0 l20     cout << "Plain: " << bytesToHexString(plain, size_in) << endl; 4 M$ a; _: R) C" `! g) T
21     cout << "Key  : " << bytesToHexString(key, size_key) << endl;
* d5 N* Q# y8 T4 K' `  v+ q9 p22  / j) H$ {; K3 z+ f* g$ f
23     TEA tea(key, 16, true);
0 {+ Q7 O" S7 u- O7 S24     tea.encrypt(plain, crypt);
" t( {; g, V9 E% T0 D25     cout << "Crypt: " << bytesToHexString(crypt, SIZE_OUT) << endl;
, H6 `& e) C" f! Y/ ]' g26  $ Z3 F( k  _# O: O& O3 w, o
27     tea.decrypt(crypt, plain); # p% I) ?% E% W( z% W
28     cout << "Plain: " << bytesToHexString(plain, SIZE_IN) << endl; 5 ?9 r: K. S, K! C& w
29     return 0; ) [' U  f% E! ]$ J
30 }% d4 p5 j+ N. B% A

7 D3 s5 d7 g) o% l4 G+ [  w+ \) z本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sailing0123/archive/2008/04/28/2339231.aspx
# c8 @% u* }' @3 {3 a0 k运行结果:
5 j( f6 ^0 c7 ]3 u( {. f  V: qPlain: AD DE E2 DB B3 E2 DB B3
+ h6 U& f1 z! gKey  : 3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4
" \0 a/ g* }( O+ ]8 c* S. @Crypt: 3B 3B 4D 8C 24 3A FD F2 % u$ J; _. \# i3 u
Plain: AD DE E2 DB B3 E2 DB B3
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|宁德市腾云网络科技有限公司 ( 闽ICP备2022007940号-5|闽公网安备 35092202000206号 )

GMT+8, 2025-12-30 02:51 , Processed in 0.021371 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表