找回密码
 注册
搜索
查看: 37415|回复: 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轮):. f# B( p4 d1 X4 b
微型加密算法(TEA)及其相关变种(XTEA,Block TEA,XXTEA) 都是分组加密算法,它们很容易被描述,实现也很简单(典型的几行代码)。
6 X, f: d. I, v9 _TEA 算法最初是由剑桥计算机实验室的 David Wheeler 和 Roger Needham 在 1994 年设计的。该算法使用 128 位的密钥为 64 位的信息块进行加密,它需要进行 64 轮迭代,尽管作者认为 32 轮已经足够了。该算法使用了一个神秘常数δ作为倍数,它来源于黄金比率,以保证每一轮加密都不相同。但δ的精确值似乎并不重要,这里 TEA 把它定义为 δ=「(√5 - 1)231」(也就是程序中的 0×9E3779B9)。 8 o: \* Y0 K; B0 A: t" O
之后 TEA 算法被发现存在缺陷,作为回应,设计者提出了一个 TEA 的升级版本——XTEA(有时也被称为“tean”)。XTEA 跟 TEA 使用了相同的简单运算,但它采用了截然不同的顺序,为了阻止密钥表攻击,四个子密钥(在加密过程中,原 128 位的密钥被拆分为 4 个 32 位的子密钥)采用了一种不太正规的方式进行混合,但速度更慢了。 * {* \# h# ^8 t
在跟描述 XTEA 算法的同一份报告中,还介绍了另外一种被称为 Block TEA 算法的变种,它可以对 32 位大小任意倍数的变量块进行操作。该算法将 XTEA 轮循函数依次应用于块中的每个字,并且将它附加于它的邻字。该操作重复多少轮依赖于块的大小,但至少需要 6 轮。该方法的优势在于它无需操作模式(CBC,OFB,CFB 等),密钥可直接用于信息。对于长的信息它可能比 XTEA 更有效率。 7 k/ F4 z+ [' m7 s2 V& ]& c4 w  B
在 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.   j6 l9 j# o7 p/ r& l! j
  2. void encrypt(unsigned long *v, unsigned long *k) {
    6 o) b0 Z, N# b
  3.      unsigned long y=v[0], z=v[1], sum=0, i;         /* set up */ + u1 s/ z/ P/ j+ I" Z
  4.      unsigned long delta=0x9e3779b9;                 /* a key schedule constant */ . X# m# o  \, }+ y. W% A
  5.      unsigned long a=k[0], b=k[1], c=k[2], d=k[3];   /* cache key */ 2 u1 j* }) q* y& Z+ S
  6.      for (i=0; i < 32; i++) {                        /* basic cycle start */
    " ~- I% |5 n* B. S' [
  7.          sum += delta;
    ) ]* o3 t2 b8 w; j6 K: n
  8.          y += ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
    . }# }% J" O) i; z# ]
  9.          z += ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);/* end cycle */ ( D* J& r' k2 Q. I$ \
  10.      } 8 ^1 b$ z- I8 A2 a
  11.      v[0]=y;
    + j- K! u" M; j+ f$ t- p, Y
  12.      v[1]=z; 7 z6 u" X/ s. U# @- J$ k$ a
  13. } ! P) N  A6 V! }3 B$ H: ]
  14.   
    , {# p; `1 q% n: _! z
  15. void decrypt(unsigned long *v, unsigned long *k) {
    ) E7 U" @1 p. J  W' n3 r( ]
  16.      unsigned long y=v[0], z=v[1], sum=0xC6EF3720, i; /* set up */ + m" `3 `+ S* V& V7 t
  17.      unsigned long delta=0x9e3779b9;                  /* a key schedule constant */ : D$ k2 q% I# Z; m. g; {' N; W
  18.      unsigned long a=k[0], b=k[1], c=k[2], d=k[3];    /* cache key */
    & C0 T1 {& p1 U  T- u  K
  19.      for(i=0; i<32; i++) {                            /* basic cycle start */
    7 z- |8 D# V7 y/ v& R6 B
  20.          z -= ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d); . d8 @! I- D7 r4 ]
  21.          y -= ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
    % r" ?+ T/ J7 ~0 F& ~' w" Q) {
  22.          sum -= delta;                                /* end cycle */
    ; q6 \/ d& o5 q0 I; q
  23.      } . y4 |1 t! G6 v. s1 V
  24.      v[0]=y;
      [3 Q8 b, {1 `1 r
  25.      v[1]=z; / a% Y; P1 i3 j+ L
  26. }
    % M% j  }/ Q/ a' L
复制代码
C语言写的用起来当然不方便,没关系,用C++封装以下就OK了:
回复

使用道具 举报

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

unit.h

#ifndef UTIL_H ' d2 y1 Y7 J: w# l
#define UTIL_H
+ L+ ]# H1 _7 e8 f; Z
  @. {( g3 R' v8 M% \( E* ?( L) c#include <string> % Q6 j$ Q: c% v8 s. s; o7 ]
#include <cmath>
: e" n) Y7 I, f7 N, X$ u6 E* ^6 |#include <cstdlib> , n# a" C4 T; y- x6 o9 g4 p# M

7 b' h$ n: d5 I! i" ^4 d4 ltypedef unsigned char byte;
( c* L% K$ _+ Atypedef unsigned long ulong;
- o* c5 B! H& H" l  W$ f
: j, x6 U& [2 l. ninline double logbase(double base, double x) { 8 p! [9 }2 a7 H  P$ ^9 Y
    return log(x)/log(base); / L: r7 I5 k) @7 ^; O. w, K& ?* Y. v0 e) @
}
; q" [! Q- L- ^
2 C% p- g0 p/ o$ G1 a- r/* / m  [* h3 U0 ?( q8 J( }4 \& Z) }( ~' `- |
*convert int to hex char. ( I. R( e2 R, a# }% z
*example:10 -> 'A',15 -> 'F' 6 i; g( M4 H: v2 o  n" C' J
*/ $ k& ]/ \1 P; Z* t, c7 L+ h. l# ]
char intToHexChar(int x); 0 Y% E' ~4 B* i- g& P# j
& g7 M- h2 M6 Q. i& \
/* & ^# y! I* X+ S8 e  L
*convert hex char to int. , P/ q9 o4 y- x
*example:'A' -> 10,'F' -> 15 # S1 c  `7 ^( f% s* _7 ~% X/ ?
*/ 2 `+ k& o$ ~2 s4 @
int hexCharToInt(char hex); 5 l# g% p4 \" d3 }: D" @2 T

/ b5 Q# `. B: ~8 b. ^using std::string; , s& O1 e6 f! L  Q  L9 d5 h
/*
7 u# p, s, v4 b* l; I8 P2 X*convert a byte array to hex string.
+ v  l; Z8 \% q! y( O( ^*hex string format example:"AF B0 80 7D"
. c: x0 L7 C4 n*/ 3 e; T  q1 ]* y( O0 `! R2 K* H+ F
string bytesToHexString(const byte *in, size_t size); - g! x) [# S8 O
7 E" ~5 q# A$ D$ w7 y
/* ) M) ?2 m( p; V+ e( \, W6 @: x
*convert a hex string to a byte array. 1 i7 U8 _/ T0 N) L, \3 Z/ t
*hex string format example:"AF B0 80 7D"
% n0 p8 _7 ^; u/ ]*/
5 |% ~/ {6 q+ Z: @+ }  z/ jsize_t hexStringToBytes(const string &str, byte *out); + i  j" k% ~% |8 S/ {- y+ q

7 _. ?( H; D% o: E#endif/*UTIL_H*/
回复

使用道具 举报

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

util.cpp

#include "util.h"
" O! p* u6 P, [! d; Q#include <vector> % Q/ l4 H- R( u1 k! r1 M5 ^

. {. R, ~' q* F1 B/ Jusing namespace std; ) w% o) r" r1 j. ?7 U/ @
, T9 g/ b) U1 X" y2 \" k( x/ S, j* V
char intToHexChar(int x) { 6 U; E% X! M& H
    static const char HEX[16] = {
7 y* e6 K* I9 g        '0', '1', '2', '3', $ X  `: w5 d% u6 E. Z9 j3 L4 L& c
        '4', '5', '6', '7', . W$ g& [, D- a* U2 m
        '8', '9', 'A', 'B',
4 p5 ^  B$ A+ ?1 n        'C', 'D', 'E', 'F' 7 }' U- @# d, f9 t3 A( n# A  P
    }; + d/ _/ c9 U% u
    return HEX[x]; 5 T5 o( v% [4 U, Y) e9 x% Y* R8 E
}
! x+ X- _$ N7 E  P5 }4 l) k) _: O- {
, P, Z" {) \8 [3 g- ]( i9 N; R( Zint hexCharToInt(char hex) {
% Z7 a$ M3 x* O7 D7 N5 G    hex = toupper(hex);
, E& d- q& f& D    if (isdigit(hex)) ) J  z+ }$ E+ T( J6 K+ [0 g1 D
        return (hex - '0');
( @2 [$ r; g1 P! j% S. n    if (isalpha(hex)) - R& t6 f/ y- S% \" n: U) q% K
        return (hex - 'A' + 10);
5 k& G$ N- G6 I$ f    return 0; 6 S6 K) h( U- f7 w
} , D2 W: B1 w2 i. U9 y* W1 l
, L: f2 q5 H) m* I  g) x1 b9 j
string bytesToHexString(const byte *in, size_t size) { ( U: l  e8 I( j/ g6 r( m
    string str; " F/ Y4 I% C4 D, P& s
    for (size_t i = 0; i < size; ++i) {
8 `0 _/ ]6 m( K' S; `+ X. b        int t = in[i]; , h- g1 S! j! D6 U2 j3 G3 g' d
        int a = t / 16; : B( M* |% J' d) d: Z
        int b = t % 16; 5 V* l; {- y" p. r  d" a5 U8 E7 n
        str.append(1, intToHexChar(a)); - f  m3 k* V7 C
        str.append(1, intToHexChar(b)); 1 m2 C* d0 a0 S: u  j; g# R
        if (i != size - 1) % H) @' v1 O4 v9 _3 z
            str.append(1, ' '); ) ]' B9 _( }) }* x; @9 C3 F
    }
. V" f( |( Z4 T3 i; L' l* m    return str; ! A) s  u2 A2 j% n- Z/ g# ?
}
. X3 V) S& H' b0 k  i. G
, \  L+ X1 o( v: w1 psize_t hexStringToBytes(const string &str, byte *out) { ; G  ~2 J9 G6 Q8 w# G* B

+ O0 `$ R! T* z7 \; u* j    vector<string> vec; 7 P& u2 |- \/ C! ]- M8 t- K; [
    string::size_type currPos = 0, prevPos = 0;
" E+ j6 Z& E+ x/ N    while ((currPos = str.find(' ', prevPos)) != string::npos) {
$ N; O0 I0 r" h5 X+ q: v$ d+ Y        string b(str.substr(prevPos, currPos - prevPos)); . e% s# [% A, r; |" h8 Y
        vec.push_back(b); 3 ~( S6 _( `( {' U% `
        prevPos = currPos + 1; # E4 Z7 m9 ~$ W) \9 O0 F- j' V
    }
, c4 o1 [. e9 I8 h' O$ Q    if (prevPos < str.size()) { % V" \  f& |/ B6 j# F$ ]6 O. B
        string b(str.substr(prevPos));
: ]; r9 |# B2 p  }8 T- b        vec.push_back(b);
$ t: u% ?* b9 D' K$ e0 \9 [, T: V: w$ L% \    } - P3 {7 J; s" V6 x
    typedef vector<string>::size_type sz_type; / C4 I: I0 I, B
    sz_type size = vec.size(); * |0 m2 d0 j8 B
    for (sz_type i = 0; i < size; ++i) {   `3 F( e8 w2 |" W$ p; O
        int a = hexCharToInt(vec[i][0]);
& s8 k" C; i. ^/ [  N0 x5 T1 ]1 |        int b = hexCharToInt(vec[i][1]);   K9 x" m! q! A0 k  E# e( \
        out[i] = a * 16 + b;
3 E/ d' j0 U" v  o+ p7 \7 R7 }    }
; }7 d4 K) [5 G1 `* l0 A    return size;
4 q4 Y4 Y5 e, T" H' E}
回复

使用道具 举报

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

tea.h

#ifndef TEA_H   R4 d$ o6 x- u
#define TEA_H 5 V0 I+ f9 u9 v% a$ N4 P! ]1 h% D
: S1 [4 f* w. a1 I3 }- r
/* / M' T* Y( ?6 L1 O3 k
*for htonl,htonl 3 C' L4 ^* X% W8 _2 J3 V
*do remember link "ws2_32.lib"
9 N( W$ Y$ g- G2 u9 r# }9 |*/ ; ]. d$ u2 ~2 y2 H3 J9 o* B1 r+ n
#include <winsock2.h> & p" H& P; O. q; u2 h$ m- o! E( K
#include "util.h" + H* J. h( v1 a! X

) }; g! f& z* h! d1 Cclass TEA {
) \7 k/ J& l0 O  |2 M5 rpublic:
. l4 T6 o# |8 O    TEA(const byte *key, int round = 32, bool isNetByte = false);
0 }4 ?: t  `* ?( O$ |) f    TEA(const TEA &rhs); / r9 B, ?! J" ]/ b
    TEA& operator=(const TEA &rhs); * a' `: _+ {* J* X5 {8 f
    void encrypt(const byte *in, byte *out); 3 O( W7 d/ W; t  j/ B
    void decrypt(const byte *in, byte *out); 3 X. t. C! }. l8 T
private: ' e6 P6 p' }/ r/ ]( \. h- w
    void encrypt(const ulong *in, ulong *out); ( h: d4 T+ P3 L4 C$ {
    void decrypt(const ulong *in, ulong *out); % C* U- b) B0 a& v
    ulong ntoh(ulong netlong) { return _isNetByte ? ntohl(netlong) : netlong; } 0 @+ w8 |5 y- t. F+ v: @/ l
    ulong hton(ulong hostlong) { return _isNetByte ? htonl(hostlong) : hostlong; } 4 _' q+ O9 H# S& a  N
private:
- A1 C+ |; q) I& ~2 Z$ \( ]7 C    int _round; //iteration round to encrypt or decrypt
0 j3 l2 ?) z  Y6 a# o! {    bool _isNetByte; //whether input bytes come from network 9 c$ p9 k  z7 L5 J5 M* t
    byte _key[16]; //encrypt or decrypt key
+ p0 `0 a' c  ]7 v: N6 ]8 c: G" I- a};
( o8 I7 u4 K/ K) U ! ?; \# K# b- B8 L& t
#endif/*TEA_H*/
回复

使用道具 举报

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

tea.cpp

1 #include "tea.h" + c$ ]( _) E! C, t$ ]9 I8 P
2 #include <cstring> //for memcpy,memset
* `$ G4 p, d1 Z: j6 c+ C9 L7 x8 u0 ? 3  
# g/ ~! V: w9 B7 e7 N4 ^& ]1 f 4 using namespace std; 2 P) t7 o2 D9 `  b8 _5 [7 L
5  ; z! ^/ R1 B& M$ O" F
6 TEA::TEA(const byte *key, int round /*= 32*/, bool isNetByte /*= false*/)
8 Y3 W9 A: Y3 J 7 :_round(round) 1 F7 q) C% V  w: p- i# q0 V
8 ,_isNetByte(isNetByte) {
" ]  Y7 [1 H9 o3 O! L& F1 O. G 9     if (key != 0) * p' [: g- N9 T0 g6 O7 ~
10         memcpy(_key, key, 16);
9 m, s/ B% @2 L4 k$ e1 }11     else
% R( c% ~: y" e0 L9 X12         memset(_key, 0, 16);
3 u6 S& |; W% O2 o13 }   V  {) s7 P- e! f: p/ q" J
14  + {% b' ^8 c2 N  U6 {/ L1 x% O( z6 v
15 TEA::TEA(const TEA &rhs) : v! _0 S9 O0 o. u4 T4 k
16 :_round(rhs._round) ) ?3 Y( G0 A* ~. B( [0 V
17 ,_isNetByte(rhs._isNetByte) {
" }. l0 v( i/ K5 H' R8 ?- C& X18     memcpy(_key, rhs._key, 16);
1 S+ K( G. ]- t19 } ! o9 `3 E- f1 }2 F# D0 h% q8 a. [
20  ( \- H3 n- y* X0 V' @
21 TEA& TEA::operator=(const TEA &rhs) {
! D; L9 i+ q; u1 ^- ?22     if (&rhs != this) {
+ Q% p) G: r9 M: Y2 J23         _round = rhs._round; 4 X% C7 E+ ^. _7 M$ u
24         _isNetByte = rhs._isNetByte;
9 ]( o7 `# X  F; ]6 P% N7 U7 z0 y) o$ R25         memcpy(_key, rhs._key, 16);
( P4 v6 E: A6 h5 j- i7 j8 Z' c- v  ?26     }
0 T- k7 G7 |0 }. ]4 H# F, l27     return *this; 5 Y9 ]$ G5 u4 O, x
28 } + i% e" q0 q, s
29  
# C6 h: o+ ^& I) F+ s4 W' U$ E/ h30 void TEA::encrypt(const byte *in, byte *out) {
7 w" n5 a. f. ~! s31     encrypt((const ulong*)in, (ulong*)out); / W; @8 D5 t3 A4 `: o9 C
32 }
) W* I# r5 Q7 z7 o8 l33  ' C, V* x9 ^- _9 O  x
34 void TEA::decrypt(const byte *in, byte *out) {
% t+ |- c. i" `4 y* b35     decrypt((const ulong*)in, (ulong*)out);
! H! [! _" g) A' e0 g4 U/ b4 c36 }
0 K0 o4 e. T5 u9 x5 c/ J37  
& @* k2 ]$ {9 i, D: `38 void TEA::encrypt(const ulong *in, ulong *out) {
" [+ \5 s# X; ]5 z  b3 }, X39  
& H. \$ O0 V; i3 w- `40     ulong *k = (ulong*)_key; ! u% w8 b4 D3 s) }1 Y
41     register ulong y = ntoh(in[0]);
) C6 h/ z0 s, h4 U- J0 D42     register ulong z = ntoh(in[1]); 6 k5 K% e5 o# D$ ?
43     register ulong a = ntoh(k[0]);
% Y7 h( H+ f9 g8 {8 T44     register ulong b = ntoh(k[1]);
+ _* r% i% ^" {" j  L0 A6 F45     register ulong c = ntoh(k[2]);
1 l( Y1 x& K0 W46     register ulong d = ntoh(k[3]);
  e2 S2 G& C7 _" ^. o; o47     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */ : h) j% a5 q& M; m1 e6 r( r+ N5 f
48     register int round = _round;
* x2 n# B, q5 ?/ Y) C49     register ulong sum = 0; 6 q) G3 _* a/ X3 i
50  
, g0 |$ Z& {" n9 }3 J1 y9 f51     while (round--) {    /* basic cycle start */ ' B  {( v( v) y3 _- N6 B( r
52         sum += delta; - _3 r5 m; y; X% B6 U0 s
53         y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); ) c7 |4 ?! S: d9 Z
54         z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
7 X, S9 N" v, H; Z/ ]6 I55     }    /* end cycle */
! ^6 d0 l" J) p" @56     out[0] = ntoh(y);
: Q9 @: a: A/ P8 }) E* G/ O! n, R57     out[1] = ntoh(z);
- D3 j5 v, X: ~. H58 } 6 h7 C, f; ~0 ^
59  
! W! r( t- L! h. n% E6 j/ n60 void TEA::decrypt(const ulong *in, ulong *out) { + F% L  @+ H$ p% [; [& I
61  
+ G& i9 @% \) [5 E6 ?1 M8 c+ U62     ulong *k = (ulong*)_key;
1 G, a9 \2 v. T, a  P( l63     register ulong y = ntoh(in[0]); 4 E9 g) }" D2 a* |( F* i, v: Z
64     register ulong z = ntoh(in[1]); ) D5 P* c$ V9 T/ Q! f
65     register ulong a = ntoh(k[0]); 4 s2 \  _) u0 a% Z8 c4 W1 l
66     register ulong b = ntoh(k[1]);
' ?8 B% {3 n- l8 V67     register ulong c = ntoh(k[2]); ; f; p1 z3 r: n) {' A. m
68     register ulong d = ntoh(k[3]);
1 J& h/ N: |9 o" E1 f69     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */ ( s7 n' f4 ?3 y" g& T) J2 r" m+ P
70     register int round = _round; . P0 o' F' s; \5 a  S7 v
71     register ulong sum = 0; 0 i* H5 Z, ?$ ^; W9 e. i
72  ! N  K. K) I& k1 ]: c
73     if (round == 32)
, [2 F& w$ e( N$ J74         sum = 0xC6EF3720; /* delta << 5*/
( n  w+ C6 a1 r6 W6 s6 h9 x75     else if (round == 16)
& J, q3 s. ]1 K, y& ^( s9 ^76         sum = 0xE3779B90; /* delta << 4*/
: V4 m7 l* v' m$ D/ W& j77     else 6 {3 C0 C6 ?% `. }/ L
78         sum = delta << static_cast<int>(logbase(2, round)); * S- o5 p9 ?8 x  G' Q( c2 l1 z6 V
79  
, u% N+ Z8 N6 r7 G# r& d+ L80     while (round--) {    /* basic cycle start */
7 x* Y4 Z/ N2 H. k. B- L3 r  t81         z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); 8 M* w2 M! Y& K. E& L
82         y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
3 B6 \9 V- q# K83         sum -= delta;
3 t, E0 W, \6 n0 Z84     }    /* end cycle */ - Y" J# I1 I8 w+ b' F
85     out[0] = ntoh(y); " p% h- r8 h: n$ a0 s" g# r2 l* x
86     out[1] = ntoh(z); 5 W$ E1 x+ C1 I# k
87 }
& [9 K: v+ {. X1 X# z: [
, D; J% L9 D( S# o+ i' M需要说明的是TEA的构造函数:
& C* m8 j; t* ^6 l4 B( G9 @' \TEA(const byte *key, int round = 32, bool isNetByte = false); 7 j& b( l; s7 B3 Z& Y
1.key - 加密或解密用的128-bit(16byte)密钥。
2 F1 M2 D% }* V+ E; w2.round - 加密或解密的轮数,常用的有64,32,16。
/ X+ w8 c. g$ C. j3.isNetByte - 用来标记待处理的字节是不是来自网络,为true时在加密/解密前先要转换成本地字节,执行加密/解密,然后再转换回网络字节。偷偷告诉你,QQ就是这样做的!
2 `' \2 m8 u3 i" E; r4 O9 r+ S9 _* f
- G. s5 R  W1 K最后当然少不了测试代码:
回复

使用道具 举报

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

test.cpp

1 #include "tea.h" # q+ h+ O) s1 K8 d* E7 I
2 #include "util.h"
4 G( G0 P5 B! i+ d0 o 3 #include <iostream>
4 U' {! b- U5 W8 {* ~$ H& ^0 s! ` 4  ! Z1 N  u" _  `8 k
5 using namespace std;
5 G1 l* w! Y: w4 O6 ~5 k 6  
3 w0 w' A* O  W# j# S/ S 7 int main() { : S4 z# c. c% u! x  x
8  2 r) Q3 d8 `* Y9 }' X
9     const string plainStr("AD DE E2 DB B3 E2 DB B3"); + m3 @  k6 d8 @/ b1 P% x+ a
10     const string keyStr("3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4"); , G- b+ s* D/ E& H& |5 u
11     const int SIZE_IN = 8, SIZE_OUT = 8, SIZE_KEY = 16;
3 a- \$ X9 b( r' z12     byte plain[SIZE_IN], crypt[SIZE_OUT], key[SIZE_KEY]; ! i  G5 y6 z/ l& F- s0 S
13  ' }+ R  S4 ^" j9 q/ c
14     size_t size_in = hexStringToBytes(plainStr, plain); 0 E! E5 i+ R- d7 y
15     size_t size_key = hexStringToBytes(keyStr, key);
& A/ _8 U- q- y6 H16  " G- M8 Z3 L( {; E7 S9 r
17     if (size_in != SIZE_IN || size_key != SIZE_KEY)
; o7 O' P+ H; h8 H2 }; \  i6 L18         return -1; 0 Y+ J/ m2 Y* j. l( B: I
19  
* T8 H/ ~) R0 `; c! f4 o20     cout << "Plain: " << bytesToHexString(plain, size_in) << endl;
& m% k! O* h4 z! y1 b) e5 S' F21     cout << "Key  : " << bytesToHexString(key, size_key) << endl;
  B- u6 ^: s2 F  A) f22  
3 S/ Y( a1 c# F6 T23     TEA tea(key, 16, true);
' O, q. l) b( G- C7 @5 y! _24     tea.encrypt(plain, crypt); - n+ c, r" ~( y7 Z" s4 f% o
25     cout << "Crypt: " << bytesToHexString(crypt, SIZE_OUT) << endl;
+ w7 b- ]! _- ~6 q; e2 ]26  
# q6 P8 M6 P, J& R' F/ I27     tea.decrypt(crypt, plain); : ?( `' k/ ~3 n' s6 _
28     cout << "Plain: " << bytesToHexString(plain, SIZE_IN) << endl; 7 Q9 w6 g& M" ~1 m! ]
29     return 0; 6 N) Z, I5 w; ^# _9 P" e$ F
30 }
- ]( {* D+ M3 m/ _+ S8 C& s0 |: t+ E- E# I
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sailing0123/archive/2008/04/28/2339231.aspx
7 h7 |+ X) n( o% G" P运行结果:
/ h, \  {, d, ~- w6 a- o  z7 _Plain: AD DE E2 DB B3 E2 DB B3
3 p$ u' p( ]2 B$ h$ H6 _Key  : 3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4 ! P2 g2 R5 a3 m" S# N: a( o) R
Crypt: 3B 3B 4D 8C 24 3A FD F2
& ?4 k7 q) Y+ H4 BPlain: AD DE E2 DB B3 E2 DB B3
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-14 08:55 , Processed in 0.019585 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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