找回密码
 注册
搜索
查看: 37714|回复: 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轮):. J* i! L! ]" m( C
微型加密算法(TEA)及其相关变种(XTEA,Block TEA,XXTEA) 都是分组加密算法,它们很容易被描述,实现也很简单(典型的几行代码)。
& `9 m! i, Y- H6 zTEA 算法最初是由剑桥计算机实验室的 David Wheeler 和 Roger Needham 在 1994 年设计的。该算法使用 128 位的密钥为 64 位的信息块进行加密,它需要进行 64 轮迭代,尽管作者认为 32 轮已经足够了。该算法使用了一个神秘常数δ作为倍数,它来源于黄金比率,以保证每一轮加密都不相同。但δ的精确值似乎并不重要,这里 TEA 把它定义为 δ=「(√5 - 1)231」(也就是程序中的 0×9E3779B9)。 : t' b/ N' M) d5 K' p1 P* K- D- Y
之后 TEA 算法被发现存在缺陷,作为回应,设计者提出了一个 TEA 的升级版本——XTEA(有时也被称为“tean”)。XTEA 跟 TEA 使用了相同的简单运算,但它采用了截然不同的顺序,为了阻止密钥表攻击,四个子密钥(在加密过程中,原 128 位的密钥被拆分为 4 个 32 位的子密钥)采用了一种不太正规的方式进行混合,但速度更慢了。
" E9 E$ r6 y& I% u, V! y3 X& s4 F在跟描述 XTEA 算法的同一份报告中,还介绍了另外一种被称为 Block TEA 算法的变种,它可以对 32 位大小任意倍数的变量块进行操作。该算法将 XTEA 轮循函数依次应用于块中的每个字,并且将它附加于它的邻字。该操作重复多少轮依赖于块的大小,但至少需要 6 轮。该方法的优势在于它无需操作模式(CBC,OFB,CFB 等),密钥可直接用于信息。对于长的信息它可能比 XTEA 更有效率。 # z) p/ P; h" i3 F) X% D$ K' V
在 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. ( C6 ^7 R& {% x; n3 v( D
  2. void encrypt(unsigned long *v, unsigned long *k) {
      W% X# o% R3 _
  3.      unsigned long y=v[0], z=v[1], sum=0, i;         /* set up */ 6 g6 }. O, I$ u8 l
  4.      unsigned long delta=0x9e3779b9;                 /* a key schedule constant */ 4 f5 g. F7 J1 n; w6 W
  5.      unsigned long a=k[0], b=k[1], c=k[2], d=k[3];   /* cache key */ : }5 b' p) h3 w1 H- a' \
  6.      for (i=0; i < 32; i++) {                        /* basic cycle start */
    ; t. d+ ^& y  Q
  7.          sum += delta;
    ! Y) F& r9 Q' I( s
  8.          y += ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b); 9 {& b6 d0 m: \+ o
  9.          z += ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);/* end cycle */ - k7 `3 S7 X# [8 g% {$ W
  10.      }
    % M( v5 }! w  }
  11.      v[0]=y; 4 H, C# w, z5 v
  12.      v[1]=z;
    % r. U5 U% U4 a- ^! X; U( D
  13. } # Y3 H4 q9 ?: A( y) e3 b: t% U4 b
  14.   " N! i7 {% j$ B) u$ X
  15. void decrypt(unsigned long *v, unsigned long *k) {
    7 T. B9 C7 N; t. y, O
  16.      unsigned long y=v[0], z=v[1], sum=0xC6EF3720, i; /* set up */
    4 M& x  P2 _% B% V' b0 O
  17.      unsigned long delta=0x9e3779b9;                  /* a key schedule constant */ - K, t* x# R8 K/ C4 H  t9 f, M
  18.      unsigned long a=k[0], b=k[1], c=k[2], d=k[3];    /* cache key */
    & ^5 s4 e* Q# Q5 K
  19.      for(i=0; i<32; i++) {                            /* basic cycle start */
    , D  ~0 E/ m$ y. m: x
  20.          z -= ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d); ! Q! [- x1 K/ }% B! n1 a6 b
  21.          y -= ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
    5 O/ L* Z) P  E; S' ^9 D- m, v# p0 I' ~
  22.          sum -= delta;                                /* end cycle */ 3 }  m' x* e) A% l
  23.      } $ `/ F# D( y# j
  24.      v[0]=y; 7 r1 X1 X' f* H% K- v5 I" l
  25.      v[1]=z; 3 g/ j4 w# ^/ ^' ?6 E) i+ _( i
  26. }
    % d- w& h! u- s3 T6 f( D* m( Z7 f: \
复制代码
C语言写的用起来当然不方便,没关系,用C++封装以下就OK了:
回复

使用道具 举报

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

unit.h

#ifndef UTIL_H 8 m+ Q# Y2 [. P
#define UTIL_H
$ ^+ b' E; f6 N
( X7 a' a2 d3 W1 N2 p& Z3 {#include <string> 4 k: O+ ?7 f! U& G
#include <cmath>
! C( s$ I) y, D/ w4 K3 |) v#include <cstdlib> 7 h" j; U+ I8 n/ q) P& ~- ~- y
, M, V: S# d& c! b1 m
typedef unsigned char byte;
% l. k5 w+ A& x: U2 d9 ]6 Dtypedef unsigned long ulong;
; }; t/ q. V1 G5 }& @% J
$ g" k! L5 {& b: Q% Y3 ?inline double logbase(double base, double x) { ) A, U2 U3 d) `
    return log(x)/log(base); , y* F7 O! V- B. k
}
+ _& v2 O$ w# r6 M ' D3 J/ Y/ `) j8 `# y) F
/*
% {7 P" a- Q, Y3 N*convert int to hex char.
9 v9 u' D* E1 ]1 w*example:10 -> 'A',15 -> 'F' 2 n) z  B( |- s2 P5 a8 X$ a3 u
*/ 4 k# N4 L% y4 z
char intToHexChar(int x);
3 t+ N; t2 c6 P+ R! D+ ]- K9 O4 J3 _ * R4 V- Z& Y* `
/* ! i) q8 ~8 E, Q
*convert hex char to int. * A* b/ m# {! q" X: Z5 V" p& k6 u2 [
*example:'A' -> 10,'F' -> 15 2 O3 D$ ]( H& O  Z0 K
*/ 9 `  M  T, p6 l$ j" E* ^# y2 t
int hexCharToInt(char hex);
- r* v  x6 Q$ @) t9 l   N8 \0 J, x7 J% P: c: b: r" ?; T! k
using std::string;   r7 {: Z" ^9 N: G  h8 w( ^  L
/*
6 \; X+ n( K/ E" |*convert a byte array to hex string. . @/ O1 H2 U# }6 }& h" M
*hex string format example:"AF B0 80 7D"
  a9 c: e' M, ^*/
: G4 p+ y' _$ W# I, Lstring bytesToHexString(const byte *in, size_t size); , n0 |' S  B( g& e) @4 K* A

  e% Q: [5 [: R2 T" F4 l& ~- m/* / [; ?, l7 f. }2 K' Q8 W# u, `
*convert a hex string to a byte array. : ?" S, \# _3 f" G8 a8 o- j+ B
*hex string format example:"AF B0 80 7D"
3 [  |; C5 `% k3 ]4 Y+ E  V" L*/ : E4 |  d3 e% g; `
size_t hexStringToBytes(const string &str, byte *out);
% C; G1 Z) r( j. H' h( g
* D' |+ D$ \9 ]. [4 e5 e( p0 F#endif/*UTIL_H*/
回复

使用道具 举报

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

util.cpp

#include "util.h"
1 u% c: N! r7 ?: R#include <vector>
$ J; ^& q9 G4 h7 N. x/ H/ L$ f1 C ( h! g) D5 N4 I$ Q/ e
using namespace std;
. e' |! G" O% e% V: F. [
4 \4 ^5 f) T- ichar intToHexChar(int x) { 0 _& _6 [& L3 M% B
    static const char HEX[16] = { $ b: R9 R% E: I# S/ D% [3 C
        '0', '1', '2', '3',
; \! \/ r8 |9 z$ }9 v( d        '4', '5', '6', '7',
8 J; d3 X7 h3 y$ N        '8', '9', 'A', 'B', ; a: N, K3 s' W5 [0 I5 D7 p% F
        'C', 'D', 'E', 'F'
* I3 q8 V" H8 M! |, f$ }6 U( ]- e9 @    }; 5 W7 W7 x7 T9 t9 D3 r) f2 p
    return HEX[x];
" I6 Z, Z* d. g3 U+ @2 {} % j; w" S0 ^4 P7 A. s4 ~5 b

, h' L  K3 u; N6 C9 Gint hexCharToInt(char hex) {
% D( {$ j1 d- Z' |& Q, }! s( e    hex = toupper(hex);
7 R! e7 w5 w0 b    if (isdigit(hex))
; Y& R) @2 R2 t3 s' [* y6 n* I# [        return (hex - '0');
3 u5 u  l# c) j6 G  r  n    if (isalpha(hex)) & C" K" f1 }4 d" K
        return (hex - 'A' + 10);
0 x4 e# O- n  b0 W5 C    return 0; & }+ _+ o0 J- I6 O5 w2 x2 A9 f- I: g, z
} 3 p! j2 c) P% U4 p1 N7 ]& X0 ~
) g, k- i, ^' S3 \6 ~
string bytesToHexString(const byte *in, size_t size) { % r* D* Y/ T  z- @
    string str;   ]1 l4 P6 R0 y
    for (size_t i = 0; i < size; ++i) { : B5 U3 M, g* A( x  z2 x4 h  I
        int t = in[i];
3 p" h2 g% O5 P# n3 E1 {1 U        int a = t / 16; : j5 R$ S% K8 B* @& z8 D% n
        int b = t % 16; 9 i9 y. n* l$ m5 J2 u1 t
        str.append(1, intToHexChar(a)); 9 d7 Y0 t. H6 g7 O2 C2 F5 J
        str.append(1, intToHexChar(b)); ! c/ ]4 ?+ }" i: k; }& n; x7 x
        if (i != size - 1)
% d1 E+ q" P' {3 J& W9 _            str.append(1, ' ');
( {/ h, Z0 I3 o    }
4 F+ [# m: S+ }- s1 Q+ r    return str;
& a. b# Y; S+ `7 P* c}
1 Z4 t$ T3 R! e( s % A4 F# d+ x6 I; x3 Y
size_t hexStringToBytes(const string &str, byte *out) { $ l" Z; u* B1 a+ `" o9 G- f, g
2 K2 }4 |4 W# q6 p$ x' ?1 V# e
    vector<string> vec;
9 N7 g3 S' B+ ^/ `0 _9 {" g3 ]+ l    string::size_type currPos = 0, prevPos = 0;
- w- d# J) |' c5 D8 y& M    while ((currPos = str.find(' ', prevPos)) != string::npos) {
& O7 \6 d: N% ?$ V  z        string b(str.substr(prevPos, currPos - prevPos));
! l0 K  q$ t! z# X# r0 B' `        vec.push_back(b); , o6 d" B# H+ l
        prevPos = currPos + 1; . x9 f% f1 K% y' c3 l. O
    } * [  e3 j9 z" o( `
    if (prevPos < str.size()) {
$ W) j( L+ n  t+ X        string b(str.substr(prevPos)); ) x' T" p* J" `  G9 Z1 f3 Y# t8 I
        vec.push_back(b); # W% u: `3 w1 k2 E6 L
    }
7 `2 V) d2 e( x" z. }    typedef vector<string>::size_type sz_type; ( F# U4 X7 x$ l. {
    sz_type size = vec.size();
5 p' t7 j% D  d6 I    for (sz_type i = 0; i < size; ++i) {
& k( o2 ^' d) K' q        int a = hexCharToInt(vec[i][0]); & r+ J$ E9 c* @7 A
        int b = hexCharToInt(vec[i][1]); : Y) _* T2 k% d
        out[i] = a * 16 + b;
" x. T) n9 n& f8 U# A    } ) n- o! D8 I% o5 O- v' A4 F2 o
    return size;
, E) b( L4 b! }/ a$ l4 K0 B}
回复

使用道具 举报

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

tea.h

#ifndef TEA_H
- r0 O  r( ]8 V2 G: a9 P/ e9 g#define TEA_H
7 I) o: A  Y9 y ! M  q6 J6 n  M8 }. I
/* % q( Y4 o6 }! c9 i8 W
*for htonl,htonl & b' \7 u4 u' b$ O
*do remember link "ws2_32.lib" : M0 m% H$ I3 O5 |+ U1 @- z
*/ " l4 v7 L+ _  U- k' z5 U& |
#include <winsock2.h>
5 L. C6 x) ]; i* T+ m#include "util.h"
! ]! F) Q1 }4 A) n/ \7 Q2 J
- `$ k5 v6 p( m4 W3 Y$ n  xclass TEA {
+ C5 v5 }/ }- J" s! ?7 P- R9 Kpublic: : o4 k, g- j+ I4 n+ B( O
    TEA(const byte *key, int round = 32, bool isNetByte = false);
' ^/ X; @5 Q: D& A" e; E  L. u    TEA(const TEA &rhs);
9 y: v7 P4 C7 ?5 v: ?7 N    TEA& operator=(const TEA &rhs);
  f( T9 H% G/ n1 u    void encrypt(const byte *in, byte *out);
5 ~$ u% P( {; P1 H8 _( L$ y; l    void decrypt(const byte *in, byte *out); 7 k9 Q6 f* s; y5 f7 t. i( N6 R; k
private:
; s. q, z& m2 K    void encrypt(const ulong *in, ulong *out);
2 Z$ Z1 ?( B0 n7 `) J8 p7 G    void decrypt(const ulong *in, ulong *out); - U, i& I1 N) J, U5 o* L  R
    ulong ntoh(ulong netlong) { return _isNetByte ? ntohl(netlong) : netlong; }
2 j9 ?+ }3 E6 b/ s    ulong hton(ulong hostlong) { return _isNetByte ? htonl(hostlong) : hostlong; } 5 E" Z, ^3 @$ i6 q& E- U" K
private:
! S( E8 {5 j2 Y( y  C7 _    int _round; //iteration round to encrypt or decrypt
- b2 M' q8 E+ K8 k2 h* d, L9 U    bool _isNetByte; //whether input bytes come from network
5 X8 U# s" Y; o2 N  a; L    byte _key[16]; //encrypt or decrypt key
1 @; y* e4 E; ]1 @) H7 s# f" {1 E}; - h: r% a* Z6 S& v5 [  _3 S$ D: z

. U4 S- I1 N$ e#endif/*TEA_H*/
回复

使用道具 举报

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

tea.cpp

1 #include "tea.h" 1 F) \4 ]( r( A5 d3 l
2 #include <cstring> //for memcpy,memset
5 A. |8 g# B( c2 u2 C! ~2 d 3  
- x) R4 i2 C& e, d 4 using namespace std;
2 E6 z5 s( m  @" M3 {- p# t 5  0 s$ e4 z! ~* c, K" s0 T* C' n
6 TEA::TEA(const byte *key, int round /*= 32*/, bool isNetByte /*= false*/)
2 ~3 {8 z2 [( s6 r# M0 A. P- I 7 :_round(round)
$ H3 P( |( v: P& ^' a( K2 x" d 8 ,_isNetByte(isNetByte) { / A9 g4 @$ e9 u6 w' }$ |5 T8 k
9     if (key != 0)
2 ]" M2 W  z% u* u0 A( y10         memcpy(_key, key, 16);
2 C  Y# z: M0 `! |4 F( t$ Q/ b11     else : q& D9 e, l8 [( w/ T. N& @
12         memset(_key, 0, 16);
: ~6 r* v! c: z+ k13 } 2 y7 R9 v( d& L. d1 R
14  
% x  o, ^" o) t, k15 TEA::TEA(const TEA &rhs) 9 c( w( X# ?- W. j0 S& s' q: m0 E
16 :_round(rhs._round) $ G' o/ [. E3 q" @& [4 A
17 ,_isNetByte(rhs._isNetByte) {
* p: `/ p! u2 Z" P& n18     memcpy(_key, rhs._key, 16); % t! \8 [5 ~3 x+ N
19 } ! h( K# z3 g7 n2 w8 r. r7 a, p
20  ( s, O- f( s; q0 O) H
21 TEA& TEA::operator=(const TEA &rhs) {   Y# X6 {% q8 `3 D% W) u) _3 o9 M' S
22     if (&rhs != this) { + S$ H' L9 R( d3 A. ^
23         _round = rhs._round; 1 _' B- i' Q# t, d/ z( m
24         _isNetByte = rhs._isNetByte;
2 H  P8 E' p7 i25         memcpy(_key, rhs._key, 16); & c" e  X. w! n# R4 ~$ i. f& j
26     }
0 b" c9 ?& x& l5 y0 D- s0 i' \# D7 ?27     return *this; 0 ]4 c7 L8 g' m3 d0 ?, n( g8 J" y7 Z. Y
28 } ) ~2 u) w  d3 B. D1 _
29  
% y& ~( G$ ~) n30 void TEA::encrypt(const byte *in, byte *out) { & t" y* c% d; t# _! w
31     encrypt((const ulong*)in, (ulong*)out); 0 ]3 f: j9 [( H" p
32 }
' N& S# K, l+ `7 L33  
3 h5 M5 \6 L. D9 V* r! ~34 void TEA::decrypt(const byte *in, byte *out) {   b$ a. B; O1 K& e$ L9 e3 {! Z5 ]
35     decrypt((const ulong*)in, (ulong*)out); $ {! v- w* {" A
36 } - T/ O! `) ]/ S
37  ) @8 X& a7 R$ w0 M" f
38 void TEA::encrypt(const ulong *in, ulong *out) { % Q. x4 s( k$ O0 \& I
39  0 [+ ]6 O$ ]/ s
40     ulong *k = (ulong*)_key; 9 T: j- J  Y3 T" a% B3 |. c
41     register ulong y = ntoh(in[0]); ( e2 Z5 d/ |, o  H, V
42     register ulong z = ntoh(in[1]);
- T( a6 \- M0 ^- t2 |! _43     register ulong a = ntoh(k[0]);
1 b7 \" i7 O! @7 k: `5 ~$ _$ I44     register ulong b = ntoh(k[1]);
' K- J3 q( G) m' D* D6 |) u45     register ulong c = ntoh(k[2]);
# q, l1 t. l3 X! [- M8 G1 W* \& p46     register ulong d = ntoh(k[3]); 9 ^9 \+ E! ]3 b2 B
47     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */
* s" Q( ?6 Q; ]) ?4 e: A48     register int round = _round;
3 i0 B: J; w8 l$ C49     register ulong sum = 0; * y6 f4 W. f. ?, {- p. Z" `9 p0 S
50  ( P4 J, ?$ [; N& q' o
51     while (round--) {    /* basic cycle start */ * @" D2 R1 u) P5 o
52         sum += delta; 6 m+ J5 Q8 ^. ^
53         y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
2 `% D  Y. f1 G  Q% g0 C4 U) O8 n, _3 N54         z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); 1 o5 q( \" x' s8 E, K5 N
55     }    /* end cycle */
9 x+ e4 _0 U( q1 C56     out[0] = ntoh(y);
4 O4 r8 p. q7 P( |) \% N57     out[1] = ntoh(z); 4 A4 [* h: v6 }$ f) n/ s( i
58 } + G) ^6 `4 o. M
59  
5 R) F' {$ z" C60 void TEA::decrypt(const ulong *in, ulong *out) {
; }% J1 R) q0 X% o- I: l& y61  
, h' l/ A& Y1 r$ L62     ulong *k = (ulong*)_key; & A2 j" E7 Q( O7 a
63     register ulong y = ntoh(in[0]);
3 y4 y  M) X; f/ @; v. i64     register ulong z = ntoh(in[1]); + L% ]# R+ [7 G+ l' p3 R
65     register ulong a = ntoh(k[0]);
1 n1 Z" T" t6 R. g0 d8 u66     register ulong b = ntoh(k[1]); 3 \6 Q( |0 b7 R) |0 w& O
67     register ulong c = ntoh(k[2]);
7 e% k% @0 t/ w' e& i68     register ulong d = ntoh(k[3]);
! Q; I; w( t# l5 N1 s1 f69     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */
- P1 p; o+ h& ^/ m7 v5 L70     register int round = _round;
7 L, }" q6 M& W* \: d+ R71     register ulong sum = 0;
5 h0 M$ ~! q# O5 r( I; N" V  H" ?. S72  
) X5 j" k+ B4 c  e73     if (round == 32)   S; H' `, n5 j# L' o, p
74         sum = 0xC6EF3720; /* delta << 5*/
+ W, m: e5 f7 q/ D5 v) b& W75     else if (round == 16) 0 K3 `/ s* R( v; Y+ \, K5 X
76         sum = 0xE3779B90; /* delta << 4*/ 7 l) @  Z$ s6 y/ {; V
77     else
4 P- \$ h+ `: }. L78         sum = delta << static_cast<int>(logbase(2, round)); ! E1 d: j. s  I: p; ~( ^
79  * G. J2 J" @) V% Q0 L- _/ q
80     while (round--) {    /* basic cycle start */ , C! E9 Q0 c- ^
81         z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); ; x, X7 Q4 \) o3 l9 u
82         y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
. v' ?9 d" V/ Y  G83         sum -= delta;
# N) q! ^& e( I+ Z84     }    /* end cycle */
# v3 G. m7 U8 s- a85     out[0] = ntoh(y);
4 M# m8 n3 x; G& O4 a& D86     out[1] = ntoh(z);
" b) Y# c4 S7 f0 W4 |2 ~- x87 }
2 U+ y9 Y. F9 W, n. |; p% ^, U. d
需要说明的是TEA的构造函数: 1 a- P7 x( M- I2 h
TEA(const byte *key, int round = 32, bool isNetByte = false); / D2 r# _# z  n# M
1.key - 加密或解密用的128-bit(16byte)密钥。 - N; B' Q5 O# @) K! }
2.round - 加密或解密的轮数,常用的有64,32,16。
* k# l. {6 N6 l  s5 U3.isNetByte - 用来标记待处理的字节是不是来自网络,为true时在加密/解密前先要转换成本地字节,执行加密/解密,然后再转换回网络字节。偷偷告诉你,QQ就是这样做的! + ]& e; o% r* d" t, I

6 M. W& _: D# X最后当然少不了测试代码:
回复

使用道具 举报

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

test.cpp

1 #include "tea.h" * D7 G: F0 m. j: l) ]
2 #include "util.h"
4 \7 ?  @% Q' w) E% F- v8 m 3 #include <iostream>
& L, |9 |5 s' t 4  5 P. Y2 A* J0 v; z: M) ?/ \: k* c0 |
5 using namespace std;
/ t# ]7 q" r0 _* C7 ^- i( u 6  , T* j3 k# w# \1 l% r( Q
7 int main() {
$ N" `/ s& H" H! O4 C2 \ 8  
) w5 B" ~  W) Q$ |2 Q0 N/ A 9     const string plainStr("AD DE E2 DB B3 E2 DB B3");
, @- n. D' n' g; b: l10     const string keyStr("3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4");
3 e) r3 g3 Z% p7 B! d0 i) d4 W11     const int SIZE_IN = 8, SIZE_OUT = 8, SIZE_KEY = 16;
! ~( F/ E; y! x9 A! N  V12     byte plain[SIZE_IN], crypt[SIZE_OUT], key[SIZE_KEY];
) s+ N* t/ `. u) G13  
1 {" b2 t2 t- c' \14     size_t size_in = hexStringToBytes(plainStr, plain); : o) V+ F7 d. N5 B
15     size_t size_key = hexStringToBytes(keyStr, key);
% I+ z8 p+ y9 w6 |) d+ n/ O16  
* l3 s* a8 S, ?1 `/ I  W  m17     if (size_in != SIZE_IN || size_key != SIZE_KEY)
) M) j9 Y& b7 ?/ {1 e0 m18         return -1; 8 ~7 i9 l; n5 u4 Z! e2 ?( b: f
19  5 Y. n$ v* g% k9 w0 E5 A- P7 r) f
20     cout << "Plain: " << bytesToHexString(plain, size_in) << endl; / d8 v. u7 O7 W9 v6 J& ]
21     cout << "Key  : " << bytesToHexString(key, size_key) << endl; * [2 V$ L0 E0 q3 ^/ Q1 [
22  / @0 b. v7 _. k- O- Y8 O$ a5 o
23     TEA tea(key, 16, true); 9 K* i4 J9 C$ D8 u# K
24     tea.encrypt(plain, crypt); 7 t( i; H7 M' ~7 q3 `0 g
25     cout << "Crypt: " << bytesToHexString(crypt, SIZE_OUT) << endl; 5 }  f1 X: ^" l" g9 b
26  
& P, c5 d8 T$ {0 q27     tea.decrypt(crypt, plain);
& D% K. i. g5 r+ u8 g- t: d28     cout << "Plain: " << bytesToHexString(plain, SIZE_IN) << endl;
. @2 i9 @) n6 x# O+ i4 c29     return 0; - P+ ?# L6 r* @" U* v
30 }  _( K: H' Q6 k  h! j
' U" |" R+ u) `' Y7 W. g, h7 a
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sailing0123/archive/2008/04/28/2339231.aspx
# y0 J% t' C4 A! u( j运行结果: 8 x- q* I5 J  o3 |* _, j" k
Plain: AD DE E2 DB B3 E2 DB B3
( Y) k, X$ f- Z3 Q- O& ?' B: RKey  : 3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4 ) {, Y$ M$ @* u  {
Crypt: 3B 3B 4D 8C 24 3A FD F2
1 F7 X6 W- Z( |: [% d+ EPlain: AD DE E2 DB B3 E2 DB B3
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-17 04:34 , Processed in 0.021197 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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