找回密码
 注册
搜索
查看: 37544|回复: 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轮):
+ o- a3 q9 P5 U. R" w, `2 w微型加密算法(TEA)及其相关变种(XTEA,Block TEA,XXTEA) 都是分组加密算法,它们很容易被描述,实现也很简单(典型的几行代码)。
8 X+ N3 D$ w; C9 w( g3 `9 e2 R* LTEA 算法最初是由剑桥计算机实验室的 David Wheeler 和 Roger Needham 在 1994 年设计的。该算法使用 128 位的密钥为 64 位的信息块进行加密,它需要进行 64 轮迭代,尽管作者认为 32 轮已经足够了。该算法使用了一个神秘常数δ作为倍数,它来源于黄金比率,以保证每一轮加密都不相同。但δ的精确值似乎并不重要,这里 TEA 把它定义为 δ=「(√5 - 1)231」(也就是程序中的 0×9E3779B9)。 : f& l4 q: X* R/ U; y' F2 k
之后 TEA 算法被发现存在缺陷,作为回应,设计者提出了一个 TEA 的升级版本——XTEA(有时也被称为“tean”)。XTEA 跟 TEA 使用了相同的简单运算,但它采用了截然不同的顺序,为了阻止密钥表攻击,四个子密钥(在加密过程中,原 128 位的密钥被拆分为 4 个 32 位的子密钥)采用了一种不太正规的方式进行混合,但速度更慢了。 0 [# E) i! U( G, P$ q- p: j& _
在跟描述 XTEA 算法的同一份报告中,还介绍了另外一种被称为 Block TEA 算法的变种,它可以对 32 位大小任意倍数的变量块进行操作。该算法将 XTEA 轮循函数依次应用于块中的每个字,并且将它附加于它的邻字。该操作重复多少轮依赖于块的大小,但至少需要 6 轮。该方法的优势在于它无需操作模式(CBC,OFB,CFB 等),密钥可直接用于信息。对于长的信息它可能比 XTEA 更有效率。 ; c" T6 @5 G' p: z7 G: m5 j
在 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. ) z& }- D! l0 S
  2. void encrypt(unsigned long *v, unsigned long *k) {
    7 A# a# j% U7 u$ Z
  3.      unsigned long y=v[0], z=v[1], sum=0, i;         /* set up */
    4 z0 Q) L! U9 _3 l, R' V
  4.      unsigned long delta=0x9e3779b9;                 /* a key schedule constant */ ( s" \8 a5 Q8 X' q, r: ?6 B
  5.      unsigned long a=k[0], b=k[1], c=k[2], d=k[3];   /* cache key */
    & _0 G" ]4 z' L
  6.      for (i=0; i < 32; i++) {                        /* basic cycle start */ % j. H8 m' }2 h. T
  7.          sum += delta; ' n6 `: \5 U, R7 g& {0 g
  8.          y += ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b); ; q* \* g8 B/ d9 b# \4 l$ `
  9.          z += ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);/* end cycle */
    + @( G% z" N1 W# H# s$ r
  10.      }
    " ]) r/ C/ F5 h- Q% R
  11.      v[0]=y;
    $ y% [9 o, A1 r9 H* w8 W0 `
  12.      v[1]=z; 2 }/ k2 L2 I* P0 ~8 W2 A
  13. } " s* R9 a! v9 W& f6 d: Z; o
  14.   
    1 }5 f4 r3 z* i' i4 ~6 i0 ~
  15. void decrypt(unsigned long *v, unsigned long *k) { $ ?' j# d' @, q5 M
  16.      unsigned long y=v[0], z=v[1], sum=0xC6EF3720, i; /* set up */
    ( r. }6 l7 ^- o# Y4 c( i
  17.      unsigned long delta=0x9e3779b9;                  /* a key schedule constant */ . N; O$ D" M4 k' U
  18.      unsigned long a=k[0], b=k[1], c=k[2], d=k[3];    /* cache key */
    ; e; g- ~2 w# x1 `
  19.      for(i=0; i<32; i++) {                            /* basic cycle start */ 4 e8 K) X9 y4 G9 q
  20.          z -= ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);
    0 Q( K  V# u- y* [9 ]& t
  21.          y -= ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
    + v: l  p! z4 G
  22.          sum -= delta;                                /* end cycle */
    % N3 \4 \# B5 Z1 a
  23.      }
    ) q/ ]! G( o* n: L4 Q
  24.      v[0]=y; / [+ r) e4 |) U) y, i
  25.      v[1]=z;   I5 B" q; A, j# S. n+ A' D
  26. }; q4 ]  Z2 C4 t, v) K" b* A8 j( B4 s
复制代码
C语言写的用起来当然不方便,没关系,用C++封装以下就OK了:
回复

使用道具 举报

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

unit.h

#ifndef UTIL_H / J+ ^* h8 N8 f0 ?
#define UTIL_H ' o( B+ ]" t: k. e9 W

4 o$ L# V$ Z9 n1 H0 F: `  }#include <string> # ^: ?2 v7 j. U( t' y! l' n6 B( \
#include <cmath>
! X7 ^) m. [7 A- `#include <cstdlib>
1 j  U' X6 r% ` # s1 a) q" V0 h/ E; Z6 l9 t' Q0 |
typedef unsigned char byte;
8 E% d$ R5 v5 C, Z" Mtypedef unsigned long ulong; / \+ {- }8 t( s

8 d( P7 C. G! ]( @" V3 Qinline double logbase(double base, double x) {
3 v0 @' H6 E& z8 y% e    return log(x)/log(base);
# h; @! @( `! j4 ^/ y  N' j  k' M}
/ F6 ^2 e1 K3 @5 m$ }$ _& m% b
$ i8 i. L. Z2 A+ B# a2 b& E5 g/*
2 }7 n3 h: P0 f# k$ G*convert int to hex char.
1 x/ `, P5 }, |# L6 G3 C*example:10 -> 'A',15 -> 'F'
8 \& R. ~% ^( T( L*/ ( |) L7 f8 l7 W% U, ?% M# M* K7 l
char intToHexChar(int x); & V' J+ s% t5 V- ~
  i6 E+ O4 m* S6 A) E5 ^
/* ( v0 ?1 q8 f% Z/ {
*convert hex char to int.
+ Q8 q- ?2 j& i% {, \' G" _*example:'A' -> 10,'F' -> 15
% O1 l: ?( w/ ?& W*/
$ H/ X% X# |3 Pint hexCharToInt(char hex); 3 c8 o( l8 m  ~2 Z' ]% `1 T! f

" g2 V8 m$ N: ]- Jusing std::string; 8 N4 r! I2 M, h) n# S% T
/*
7 ?$ D6 Y0 M% y# g) a, v*convert a byte array to hex string.
3 N" P( P( O! `*hex string format example:"AF B0 80 7D"
6 D9 A/ B' B6 F7 @$ P" U- c, h*/
! w! K. |: [6 a6 I. I* z- C1 Rstring bytesToHexString(const byte *in, size_t size); ; D2 K2 \) h& s* A1 i
0 m) n  f- k4 I, c/ G+ U
/* 2 G7 e/ o7 Z$ F) |- P5 b: I" a' g' r
*convert a hex string to a byte array. # R5 ]4 Q2 G! `- r
*hex string format example:"AF B0 80 7D" ! R; u" Z& P  f+ f9 r
*/
) T0 H% V' a" W6 H% k, Q8 csize_t hexStringToBytes(const string &str, byte *out);
, [$ ?0 ^! ]' f1 k- t2 k, q  E6 I. Q 1 i5 f) _( T! x( ?# A  m1 W
#endif/*UTIL_H*/
回复

使用道具 举报

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

util.cpp

#include "util.h"
' H  j" B( a4 p, P#include <vector>
! s0 e7 ?( y( G3 N* H5 M8 Y( I
# k- J1 F: h) Pusing namespace std;
9 P- v9 a9 H4 B' R # W" i* D& R  P- c, @, @- u
char intToHexChar(int x) {
) A' |, P& b% g5 V4 r: k( ]    static const char HEX[16] = { 0 k# R+ `) g2 e( o
        '0', '1', '2', '3', 9 N) p# _4 o/ W+ f9 b
        '4', '5', '6', '7', # E9 _& K5 ?: {: N( f3 G
        '8', '9', 'A', 'B',
7 ?9 O! n2 _  k  i& z5 G        'C', 'D', 'E', 'F' % C6 c4 a- d; \% k" A$ N' C8 _- e
    };
3 W: k0 H7 n) o% M' E; w    return HEX[x]; - Z. o1 x. V2 M  u1 z
}
0 {  ?% \" y6 J. l" q: I" Y ! W- q1 t: c& n" Z' Q- x
int hexCharToInt(char hex) { ( `5 f5 n0 l+ `1 q# u/ S( E
    hex = toupper(hex); 8 F# P  w- K& ~- T' O" d1 `+ r1 r
    if (isdigit(hex))
/ X! B6 ^" e: k        return (hex - '0');
- Z# G# z6 d( l& J9 w    if (isalpha(hex)) , F6 ?5 N+ Y7 v2 ]/ c  h9 L- p" {6 a
        return (hex - 'A' + 10);
3 g: W: V- c! g( S. i3 h    return 0;
/ O* M2 r9 B/ _. p  A} 6 U0 Y" x5 D9 x; d, T

; S% r7 c' {3 k5 Cstring bytesToHexString(const byte *in, size_t size) {
9 K! l  b( E- P* p2 [1 p# F5 i: b    string str;
7 K1 Q5 `8 m4 j! d( U) |6 v    for (size_t i = 0; i < size; ++i) { : L* W, v3 B7 O: b8 _) Z
        int t = in[i]; 6 F7 k) B! y3 q# @% ~5 _
        int a = t / 16; % m/ ]# W. P( p9 W. l' A1 m$ h
        int b = t % 16;
# ~3 T4 E4 t# U9 k        str.append(1, intToHexChar(a));
8 I: `6 k2 g# `6 p! F9 ^; L, A        str.append(1, intToHexChar(b));
: `4 f% N) g- ?" T+ i% _+ }        if (i != size - 1) 8 e: q/ r$ q4 ?3 g) M
            str.append(1, ' '); , G6 l& |/ }: F
    } . Z! ~6 ^* Y; F  w) v1 @& e) B
    return str; . x0 s( K' J! f
} 4 T+ O4 Q+ j- H) V, U

8 R9 j: l8 Z4 J& v" Jsize_t hexStringToBytes(const string &str, byte *out) { 5 R5 `' H- s$ x8 r  z% @7 q% j4 l

% E2 Q: H; m( F! O    vector<string> vec;
- K. \- Y/ f* a! ~6 G$ e3 r    string::size_type currPos = 0, prevPos = 0; # }! Z3 |% w/ S: w, i- h
    while ((currPos = str.find(' ', prevPos)) != string::npos) {
( z! J4 h7 Q6 S: Y; o/ u7 A6 k        string b(str.substr(prevPos, currPos - prevPos)); ( W# B1 ~* a  S- b' V! E% I" ~
        vec.push_back(b); . u! i4 n$ }/ e' {' t6 B) Q
        prevPos = currPos + 1; : ?2 U& h$ o) H6 Y8 s3 z; X0 U
    } 6 e# C# r$ H% \, U/ z% y- |
    if (prevPos < str.size()) {
* `, O" R% v& I, Y9 V  X5 C9 j        string b(str.substr(prevPos));
2 ~  V0 r8 Q' Z/ M        vec.push_back(b); 7 h2 c2 o, x# _7 L3 E4 ?- p8 D, y
    } # T1 M8 F" L" p% B% t- j& X
    typedef vector<string>::size_type sz_type;
$ q8 P) ?/ ^6 k9 F8 X    sz_type size = vec.size();
( S$ z% I9 n& C' u8 F5 K4 Y9 ]    for (sz_type i = 0; i < size; ++i) { % O7 f% f+ p1 s9 N
        int a = hexCharToInt(vec[i][0]);   z+ p# [- D% \2 R1 _
        int b = hexCharToInt(vec[i][1]);
( e+ r- \  P) g' `, s2 l& d5 s        out[i] = a * 16 + b; - b5 C1 [0 I$ h4 u8 Q: Z
    }
! o" ?* z, |9 c+ h; L% h# a3 w; @; A    return size;
3 M  o1 X: t1 l7 m+ x! X/ b}
回复

使用道具 举报

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

tea.h

#ifndef TEA_H   u& `# n7 @' `9 q
#define TEA_H ( i( P( B. b% {8 a

/ i+ K+ b/ v+ x/*
1 a0 n9 R* w! ]/ Z' X*for htonl,htonl ; i! i4 y1 }2 Z2 y
*do remember link "ws2_32.lib" 2 w% A1 J1 U5 a( r
*/
5 N& c2 O  S5 V: B#include <winsock2.h> - M2 s7 j# q6 _& ~( L2 Z2 k
#include "util.h" & i5 P, L% y1 d8 _7 F8 o
  A) ?* v0 `8 j7 u
class TEA { , W# c1 N) r: M2 Q. J
public: 6 Z  W; O- y" T/ D9 ~  \
    TEA(const byte *key, int round = 32, bool isNetByte = false); ( n* |( r: [! U8 r: D9 t
    TEA(const TEA &rhs);
' R2 I4 h" D3 Y! Q0 L' y    TEA& operator=(const TEA &rhs); % @5 @/ h  \) G  @+ ~% J1 ^
    void encrypt(const byte *in, byte *out);
; X5 N/ G. z6 J9 }    void decrypt(const byte *in, byte *out);
9 {  }4 s1 e: K5 J: q9 N2 kprivate:
! g3 [- r2 v' l: r. Y, }. m, ?- X    void encrypt(const ulong *in, ulong *out);
% u! ~/ {; f$ V0 P    void decrypt(const ulong *in, ulong *out); " f! H, x( g- F3 b8 `* e
    ulong ntoh(ulong netlong) { return _isNetByte ? ntohl(netlong) : netlong; }
+ y1 e3 N3 h8 W) e; W    ulong hton(ulong hostlong) { return _isNetByte ? htonl(hostlong) : hostlong; }   E' }* g% r' Q2 O3 u+ ?, z
private:
) `5 d! \) U7 N% Q    int _round; //iteration round to encrypt or decrypt
( o9 j1 v# i. h: [5 q    bool _isNetByte; //whether input bytes come from network % O* Y; x& P. n5 e, B* L1 Q
    byte _key[16]; //encrypt or decrypt key
: G# f& j; T3 f3 P) S6 p};
! W/ l& A0 r) W6 v
" |6 H( K+ C  B& d, W#endif/*TEA_H*/
回复

使用道具 举报

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

tea.cpp

1 #include "tea.h"
" a- X- j; M( f4 B- J 2 #include <cstring> //for memcpy,memset
) g$ D* ?5 m3 U: ~4 Z5 Y! c 3  5 O6 ]# k* b6 O. F; B2 o9 `: u
4 using namespace std; 3 c/ }1 L! Y3 I+ m$ C
5  
2 }/ N0 [0 _2 C1 L5 m  f2 H 6 TEA::TEA(const byte *key, int round /*= 32*/, bool isNetByte /*= false*/) 1 @: `; J( A7 m5 }- p, n. R  L' q
7 :_round(round)
7 C" J0 G( D* V6 U( K 8 ,_isNetByte(isNetByte) {
* q) t4 A- d: s8 T% O, Y. G 9     if (key != 0)
0 X6 e1 _1 W5 K% f: b10         memcpy(_key, key, 16);
& h0 P3 ?8 A3 ?# K& M8 I6 O11     else . d1 ^& {, u/ h- E) z
12         memset(_key, 0, 16);
# v. |/ C! c! `* O/ P- ~; r13 } " w9 R3 t1 k( f( Q; G4 ~$ o+ G
14  
: a0 p  V. p& L: b1 R) K6 `/ j* i15 TEA::TEA(const TEA &rhs) ( a% p  t2 j5 C5 z1 ]
16 :_round(rhs._round) 8 k2 {8 \2 T( \/ k
17 ,_isNetByte(rhs._isNetByte) {
8 V$ k  I. I4 c+ S18     memcpy(_key, rhs._key, 16); 7 Y; a2 f) t& d3 J
19 }
5 f1 V. ]: p' G+ e+ a20  
9 V8 g% |6 n  {4 z) W5 E" m6 P% w21 TEA& TEA::operator=(const TEA &rhs) {
" M% ~& o; T6 X0 _. \8 c0 o9 u22     if (&rhs != this) { ; P& F, \5 R6 k5 Q( s4 j
23         _round = rhs._round; 6 P# a. e9 B5 B& @
24         _isNetByte = rhs._isNetByte; 4 g$ l- M4 N9 [: E
25         memcpy(_key, rhs._key, 16); , k+ ^: ?5 N8 u& U
26     } $ q( Z$ q, _6 K8 J4 V& ~0 a
27     return *this; 9 Z6 l8 c1 b6 C4 v! Q& _# T9 n
28 }
  u( \. P& h' a6 [; w: @$ N& ^* u29  
: s) M9 S2 P! U! C30 void TEA::encrypt(const byte *in, byte *out) {
1 L  U* L- _: d: ~2 p31     encrypt((const ulong*)in, (ulong*)out);
3 D" S4 W9 ?4 b/ U' i" m32 }
0 T8 Q$ ~& A/ e% c  r  |33  
: s9 I8 H3 L! G$ H# s; S7 U34 void TEA::decrypt(const byte *in, byte *out) { & u% }. P& u$ W8 ]  k
35     decrypt((const ulong*)in, (ulong*)out);
/ ^9 L9 x8 ~9 p2 v: V4 D' L36 }
" |6 v$ I# b- c" _; P4 w4 `37  
2 h# N' K: C+ ?  y- |38 void TEA::encrypt(const ulong *in, ulong *out) { % N+ h" L8 L5 {4 }
39  
- I  G  |  u$ \0 o" Q7 [/ x40     ulong *k = (ulong*)_key;
4 b  X' \+ H/ e$ g( N, j41     register ulong y = ntoh(in[0]);
2 p7 x, t" f3 Y2 E42     register ulong z = ntoh(in[1]); & f2 m( r+ Q9 ?6 x4 {' d
43     register ulong a = ntoh(k[0]); ( Y( D4 a- h  h* e1 _
44     register ulong b = ntoh(k[1]);
2 ^) Y6 P) k6 T; t45     register ulong c = ntoh(k[2]); / C5 T* {, c# f) J9 ]
46     register ulong d = ntoh(k[3]); $ F8 o5 V" u. F" G/ ~
47     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */ - Z" V. P: J* V3 V& ~% s, e8 ], N
48     register int round = _round;
' \% T* o' c' L1 B4 v# x: ~0 G49     register ulong sum = 0;
7 ]$ O; s" Y( E) `- y( J( w* f! \$ s50  
/ i/ Z6 l+ X* {+ [5 O* E  ~7 _$ t8 m51     while (round--) {    /* basic cycle start */
) O1 k# c. T( d$ N5 E. F& o  K52         sum += delta;
8 S$ }& e# ]- s. p9 I. o9 n8 I! ]53         y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
1 Q2 ^+ I1 T! O2 o54         z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); 2 L) Y& a# ~3 C; b+ X
55     }    /* end cycle */
: {  ^* p" ]; J4 k+ z0 M! l2 q56     out[0] = ntoh(y); 8 @+ }" @2 H! K
57     out[1] = ntoh(z);
1 w# [7 o; d% i, `7 ^58 }
* c* h9 {* ]! j59  
/ X' R! y7 v6 x) f! C4 c60 void TEA::decrypt(const ulong *in, ulong *out) {
2 U7 j! f5 M  y7 q61  
/ E: N* L: ?. {9 I# q* a& e62     ulong *k = (ulong*)_key; * b( d3 _$ j% @/ O
63     register ulong y = ntoh(in[0]); : D/ v5 I! Z2 o& A& ^
64     register ulong z = ntoh(in[1]);
5 A& A/ ^. P5 r65     register ulong a = ntoh(k[0]); 4 e  _$ z7 j4 x8 G0 W3 C9 a
66     register ulong b = ntoh(k[1]); ( f4 J% O4 d; I# y) G
67     register ulong c = ntoh(k[2]);
) q6 j1 g7 @& l5 n* p7 G) i68     register ulong d = ntoh(k[3]);
2 p* N% m  S* `- P, N69     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */ 4 o3 Y8 l5 U: Y# {
70     register int round = _round;
  I0 x* T1 ?+ H3 T71     register ulong sum = 0;
% O* |  V  {" e0 I( S72  ( `$ F* o3 k9 j( A& m' K
73     if (round == 32) % R6 O, F! P8 G
74         sum = 0xC6EF3720; /* delta << 5*/ 0 B$ t8 n6 W9 c3 G
75     else if (round == 16) ( C' R/ |" I1 B( I4 H
76         sum = 0xE3779B90; /* delta << 4*/
& b; Y/ Y: E3 H4 |  L) ]0 s; _( C77     else 4 ]2 h" ?; G9 y% p: [
78         sum = delta << static_cast<int>(logbase(2, round)); 0 ~8 `! x& u3 B. L* a1 M# Z
79  
7 V0 ^. c$ K) U! m% a! |: L+ C80     while (round--) {    /* basic cycle start */ $ v' E$ h  d0 V1 _
81         z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); ' x. I, {4 D& r+ q6 J/ u4 Y
82         y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
; \! v6 t6 f7 k3 j' ?83         sum -= delta; 4 r" N* M4 B2 d4 d. R# ~
84     }    /* end cycle */   S. H& c$ b9 k' X  |
85     out[0] = ntoh(y); ; ^; `  A; \# m; i! M9 M$ Z
86     out[1] = ntoh(z);
: ^; F/ ^) T. y- [' l87 }
9 \/ e$ v; F4 o) W: E0 b* J3 u( U
需要说明的是TEA的构造函数: ' r% v+ N3 C1 ?- O$ C+ K! S& ]
TEA(const byte *key, int round = 32, bool isNetByte = false); & q% Q7 G, }0 }, W) W
1.key - 加密或解密用的128-bit(16byte)密钥。
8 ]" q- M; z. Q4 X1 r! r2.round - 加密或解密的轮数,常用的有64,32,16。
, i( l, r5 E$ q8 s4 W) Y3.isNetByte - 用来标记待处理的字节是不是来自网络,为true时在加密/解密前先要转换成本地字节,执行加密/解密,然后再转换回网络字节。偷偷告诉你,QQ就是这样做的!
' A& z7 f! @* g$ ^; l# _( m7 V4 ^5 {, Q* h9 V# f3 J2 \# x
最后当然少不了测试代码:
回复

使用道具 举报

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

test.cpp

1 #include "tea.h"
" B6 G5 o8 g& j 2 #include "util.h" / o! E2 e4 {# E1 P8 t+ w
3 #include <iostream> ; c1 }' D' @; ^) E! o3 f
4  
2 `! O, g. Z; E6 t 5 using namespace std; 2 L# ]; o6 T7 a& G
6  
7 Y: ~1 j: ~2 t# G0 n 7 int main() {
; O. @" m$ l/ I- r! E1 s 8  ; k( B1 R6 U8 ]
9     const string plainStr("AD DE E2 DB B3 E2 DB B3");
. F' u: b4 v7 w$ c5 B10     const string keyStr("3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4"); 1 W- G& h8 p3 {
11     const int SIZE_IN = 8, SIZE_OUT = 8, SIZE_KEY = 16; - O. Q. J7 \4 O. u4 e
12     byte plain[SIZE_IN], crypt[SIZE_OUT], key[SIZE_KEY]; % J1 L( }3 O3 }( q* F' O2 e
13  + U$ b. z- J1 i4 _/ o: L
14     size_t size_in = hexStringToBytes(plainStr, plain); + J- w/ S: v7 s, T- f- Y
15     size_t size_key = hexStringToBytes(keyStr, key);
/ |) U  U% h; {* [. u$ q16  ( c) U6 w% y) |- e8 t3 {
17     if (size_in != SIZE_IN || size_key != SIZE_KEY)
/ g- H3 Z' V& y% n! `18         return -1;
- `9 W: o' v: L# h19  8 |: C% n5 ~# v/ M
20     cout << "Plain: " << bytesToHexString(plain, size_in) << endl; 4 ~/ x1 @" n( ]( r) N3 m& w
21     cout << "Key  : " << bytesToHexString(key, size_key) << endl;
% o" m  k: s+ B: @! t/ B% H2 O' t6 _22  1 W* a: L2 W8 o1 {6 k! w2 \
23     TEA tea(key, 16, true);
$ S" w+ u2 {9 f$ ]+ R1 R1 M24     tea.encrypt(plain, crypt);
0 x8 c1 K% p6 G  V% ~25     cout << "Crypt: " << bytesToHexString(crypt, SIZE_OUT) << endl;
2 j8 m1 P; s  p- ]2 l/ [* v/ j' G! F26  : _( w* Y6 a2 a3 u& f
27     tea.decrypt(crypt, plain); 9 D* l9 G, [. n5 `2 E( e$ `& j* i) J
28     cout << "Plain: " << bytesToHexString(plain, SIZE_IN) << endl; ( E5 D- [$ R6 M% |/ q, {* @4 C
29     return 0; . t6 V. \  s5 }2 n! S6 t8 G
30 }
: D' k1 u: O! {3 M4 ~# K
; u! V  G, w# i' n7 }) N2 @本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sailing0123/archive/2008/04/28/2339231.aspx  `# W  S9 D, \4 s: L3 ~
运行结果: & d" u- p* z* q- e
Plain: AD DE E2 DB B3 E2 DB B3
( B! D+ i6 X; `; ~2 }# B7 u6 MKey  : 3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4
; S" M9 R* v/ w. gCrypt: 3B 3B 4D 8C 24 3A FD F2 ( L; B: d/ k" `9 f( |8 X1 U" y
Plain: AD DE E2 DB B3 E2 DB B3
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-1 08:24 , Processed in 0.021008 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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