找回密码
 注册
搜索
查看: 38329|回复: 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轮):6 ]+ i0 o) M' p: S; j  l
微型加密算法(TEA)及其相关变种(XTEA,Block TEA,XXTEA) 都是分组加密算法,它们很容易被描述,实现也很简单(典型的几行代码)。
) e! g% p4 B" U: m# n- VTEA 算法最初是由剑桥计算机实验室的 David Wheeler 和 Roger Needham 在 1994 年设计的。该算法使用 128 位的密钥为 64 位的信息块进行加密,它需要进行 64 轮迭代,尽管作者认为 32 轮已经足够了。该算法使用了一个神秘常数δ作为倍数,它来源于黄金比率,以保证每一轮加密都不相同。但δ的精确值似乎并不重要,这里 TEA 把它定义为 δ=「(√5 - 1)231」(也就是程序中的 0×9E3779B9)。 1 X- v, S, C/ ?) _
之后 TEA 算法被发现存在缺陷,作为回应,设计者提出了一个 TEA 的升级版本——XTEA(有时也被称为“tean”)。XTEA 跟 TEA 使用了相同的简单运算,但它采用了截然不同的顺序,为了阻止密钥表攻击,四个子密钥(在加密过程中,原 128 位的密钥被拆分为 4 个 32 位的子密钥)采用了一种不太正规的方式进行混合,但速度更慢了。 - B. K) l  g6 Q7 D7 F- a# x
在跟描述 XTEA 算法的同一份报告中,还介绍了另外一种被称为 Block TEA 算法的变种,它可以对 32 位大小任意倍数的变量块进行操作。该算法将 XTEA 轮循函数依次应用于块中的每个字,并且将它附加于它的邻字。该操作重复多少轮依赖于块的大小,但至少需要 6 轮。该方法的优势在于它无需操作模式(CBC,OFB,CFB 等),密钥可直接用于信息。对于长的信息它可能比 XTEA 更有效率。
8 X6 N; U) G7 p在 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. . ~, v6 C+ D  f! l. V. ~
  2. void encrypt(unsigned long *v, unsigned long *k) {
    % Y2 [: Y  _1 N  }
  3.      unsigned long y=v[0], z=v[1], sum=0, i;         /* set up */
    7 `/ k( G* C7 W$ G# S* p" c
  4.      unsigned long delta=0x9e3779b9;                 /* a key schedule constant */
    $ G( j& v2 e1 l% q8 z% l; J! Y/ d
  5.      unsigned long a=k[0], b=k[1], c=k[2], d=k[3];   /* cache key */ - I7 R( z  `9 I# M0 n$ B) {
  6.      for (i=0; i < 32; i++) {                        /* basic cycle start */
    + _0 G- M! |- i' ~
  7.          sum += delta;
    * j% t: |' R3 N' f( ^+ B
  8.          y += ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);   T+ a& k* c5 l( t& _* E' m' _
  9.          z += ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);/* end cycle */ 2 v3 e1 f. |2 R$ q( E
  10.      } 4 N& b7 ]& _, A7 R" V+ N
  11.      v[0]=y;
    0 V* y. w7 y8 W+ E& r
  12.      v[1]=z; ( Y* x( f4 f) V9 ^
  13. }
    6 `! @+ ~# a. E9 I2 \
  14.   # R5 _" p7 A3 o4 q/ j
  15. void decrypt(unsigned long *v, unsigned long *k) {
    3 O. Y3 V0 d+ W9 M
  16.      unsigned long y=v[0], z=v[1], sum=0xC6EF3720, i; /* set up */ 7 u. y. c1 o1 U/ ?9 D
  17.      unsigned long delta=0x9e3779b9;                  /* a key schedule constant */ 6 V5 v7 H- g7 ?# i8 r
  18.      unsigned long a=k[0], b=k[1], c=k[2], d=k[3];    /* cache key */
    : H! l0 l+ g9 h$ j' E3 j
  19.      for(i=0; i<32; i++) {                            /* basic cycle start */ 8 \6 h; B( C- p6 K( e- [% F
  20.          z -= ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);
    : x1 a/ W* O+ @9 x, Y$ J
  21.          y -= ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b); 2 Y' K- x. _' ^8 o5 A$ e) Y+ w! y
  22.          sum -= delta;                                /* end cycle */
    - S2 `2 o5 Z3 R5 p( z8 q) X; ^3 s- g: L
  23.      } $ I$ ]0 w6 p, N7 N; F" K
  24.      v[0]=y; 7 v: j; D( E- g2 @4 a+ c# N
  25.      v[1]=z; ; s1 B' b1 Q# k* V
  26. }1 v9 L! L3 L; W/ q
复制代码
C语言写的用起来当然不方便,没关系,用C++封装以下就OK了:
回复

使用道具 举报

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

unit.h

#ifndef UTIL_H 8 p! x9 d/ K" A( Q0 t; ]8 g+ I
#define UTIL_H
' l6 B# e3 o2 e" K0 U9 I0 @' l( g  w1 c
#include <string>
+ |; a5 H1 V. X#include <cmath> 8 x" u+ s' x: v2 i3 t
#include <cstdlib>
" P* A+ ]" l( ~& K
" |/ Q0 y$ e. o4 ^6 d( Btypedef unsigned char byte; ! j9 N: M9 y& a; d! V6 Y
typedef unsigned long ulong; & `! |+ K1 |3 Z8 W3 B
5 F1 h9 s* |2 d) S  q) v7 q
inline double logbase(double base, double x) {
) ~# P. _/ A2 X3 L9 O! ~    return log(x)/log(base); * J2 q" s9 i$ P7 y+ k! Q
}
- j0 P/ `& N- g* Z; l) B3 L
' ^9 O  K  J) \4 E/* ) Z7 G0 d/ \" ]& v5 h, i3 K, t
*convert int to hex char. - ^, n( I& g- w1 m8 w& ^( Y4 Q: X, v
*example:10 -> 'A',15 -> 'F'
* B2 A, Z! F2 r/ m$ t$ N, ~*/ 0 `, u8 w" ]# Q$ E3 C. ]
char intToHexChar(int x); ( `8 U6 `5 z3 z0 s* s/ M8 v: P+ @2 w
2 {: w# q* U, L7 {9 M( _
/*
& z# [: Q; T9 t/ W1 T8 X*convert hex char to int.
6 ~. h# S- ]: R  G2 Q4 E*example:'A' -> 10,'F' -> 15
; X1 p; t' l, `2 l1 ?: J2 y*/ - q0 {3 q& E) F5 v* E
int hexCharToInt(char hex);   c. E$ b5 `5 K+ b0 O1 J
. e) X2 T$ P* W$ C& L. @; U
using std::string;
0 w4 P& N0 W3 W7 e% B/* 5 b2 L# |. r$ i6 ^2 k2 u- w5 B3 h2 P  x- C
*convert a byte array to hex string. ' d: q9 T& U! [' y
*hex string format example:"AF B0 80 7D" 6 P6 N+ R( V) C8 }$ c# N9 C
*/ ! k/ P) c$ W6 d5 S' d$ X
string bytesToHexString(const byte *in, size_t size);
" ]* [- ^  I% z2 X  ?9 u4 k % Q0 h0 m, H; u0 w
/*
: b7 i% L$ m9 j4 {, r9 X*convert a hex string to a byte array. 0 B5 @0 N! y6 ]) ^! K4 G
*hex string format example:"AF B0 80 7D" 8 `/ M1 t$ Z4 s& `0 r! W  {
*/
4 t( v% x4 ^- B6 s. ]7 Isize_t hexStringToBytes(const string &str, byte *out);
9 Y/ |/ H* n/ ^( k " f9 l! |. P( a, v- o) W+ }+ b
#endif/*UTIL_H*/
回复

使用道具 举报

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

util.cpp

#include "util.h"
2 O1 P# K2 }" B8 q#include <vector>
% a  o' {9 A. F* B% g7 {
/ l7 X% a3 g, A" R$ `8 I" f) Busing namespace std; * L" }8 a0 l" i, c: U5 {& M

; ?& S5 R" R( S: m4 G7 I  q! bchar intToHexChar(int x) { + \* t* c- j  g! M
    static const char HEX[16] = { 5 R, B* [8 F/ P! `
        '0', '1', '2', '3', 4 I' M: w& S. D7 R4 F& [* B. ?/ a
        '4', '5', '6', '7', 9 R7 s: u* Q& d! T9 @) c$ s2 H" _
        '8', '9', 'A', 'B',
' A. A: A% R9 P* X  L; S5 v        'C', 'D', 'E', 'F' 1 a, W. O3 O# ^" h1 [$ }
    }; , ?" v# h8 i4 h% E" `
    return HEX[x]; 4 R9 T2 L+ T; D5 z
}
7 A3 o7 F$ ^4 q/ d- [- D( r3 n% ^
# q( O" B9 B4 S9 X. Sint hexCharToInt(char hex) { ; ]* A8 B$ ^  `0 K* }6 }4 ^. f
    hex = toupper(hex); . G+ C6 g, f  l; y7 V
    if (isdigit(hex)) & L- s1 ?; `0 S8 f% y# \0 x) E- |
        return (hex - '0');
  d: ^8 ]1 ]$ j4 l0 ~, R  u    if (isalpha(hex)) 1 i! W3 b. u- q( I1 K8 B1 ?: {
        return (hex - 'A' + 10);
# n0 h- i: ~1 c- }- H/ o+ c! K3 E    return 0; ' K* R8 r7 U3 x5 Z, `
} 7 O0 `0 r$ Q& M; R1 o# f
) O3 P. ~# @* t  Q
string bytesToHexString(const byte *in, size_t size) { ( U6 o9 J; ]1 H% E% V" R( I6 a
    string str;
* X; J" N( P$ P3 [    for (size_t i = 0; i < size; ++i) { 4 `7 M9 _  }, v: c% S" i* J1 C* U
        int t = in[i]; 1 ^! H( W) A0 B/ N+ t7 P; ?8 T
        int a = t / 16; " ~( X0 F' B2 J/ h5 g; z
        int b = t % 16; 8 X: m: q2 G8 f! S
        str.append(1, intToHexChar(a));
7 k; I% G$ [! J+ q* ?9 P, U        str.append(1, intToHexChar(b));
7 k4 H% I8 J. {9 h. v! I1 L        if (i != size - 1)
; z* v3 C  R( \' g4 A& d6 i            str.append(1, ' ');
' m6 f- O$ K: X! _- f- u+ X9 @    }
2 U( K6 s  o# [$ i$ q: h. V) q. Q4 E    return str;
8 B$ d7 H! |2 _$ B: f% B; L6 h}
! A6 [: @2 S4 T& M# i' D, [4 A
9 F; \/ T( A" g* k* f- Y9 Usize_t hexStringToBytes(const string &str, byte *out) {
( O4 I/ N/ L9 y' e7 b
+ O1 ~9 |4 }. w* L. W8 ~! _  Z/ y    vector<string> vec; : a6 g* X  y# _$ N: F4 U4 A* ?  ^
    string::size_type currPos = 0, prevPos = 0; / g! R, H1 m( r* U1 y
    while ((currPos = str.find(' ', prevPos)) != string::npos) {
$ w5 P. A$ g! \; i& U- o" w: Y        string b(str.substr(prevPos, currPos - prevPos));
# F8 h9 Y; `0 A$ ~        vec.push_back(b); 0 I7 d9 a2 A* y0 [, K5 K1 Q) z
        prevPos = currPos + 1;
; m" m6 `. M7 h& ]7 }- ~& r    } 9 A# @# p- {) N
    if (prevPos < str.size()) {
3 s) b4 Y% c( g" r( M$ o' U        string b(str.substr(prevPos));
* g6 T9 `! @3 T3 D        vec.push_back(b); 2 j( g# B  w  q3 a
    }
1 d- m4 t1 n1 [0 R    typedef vector<string>::size_type sz_type; 5 Y/ S5 C2 v: O8 Q; q0 \/ ~
    sz_type size = vec.size(); 5 a5 B+ g0 h8 d7 P& m0 a
    for (sz_type i = 0; i < size; ++i) {
: |% W) Z% o. R/ q1 r& P/ p# r        int a = hexCharToInt(vec[i][0]);
8 c8 d/ N. X2 _! t! |        int b = hexCharToInt(vec[i][1]);
/ O5 e8 d1 W9 b7 _        out[i] = a * 16 + b;
; v% b. X- T5 Z* S1 g; b* [, u6 }* Y; Z    }
4 Q6 n0 O- u' O) Y' a& N    return size; . a: ]5 p' z$ N/ p8 F1 g) s) a9 J
}
回复

使用道具 举报

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

tea.h

#ifndef TEA_H ) t& L6 J$ |" ~! d) T& f  M( \
#define TEA_H 6 H- n, \4 Y. @

9 y$ Z' A+ \. u% }/ \% H8 r$ R; s/*
8 J( i9 e. Z6 i*for htonl,htonl 3 T7 I0 D) t7 y) O4 i- a# r
*do remember link "ws2_32.lib"
0 p# f; M! y) O8 A% X+ r9 B& c! z*/ 0 `# V1 d. Y* l- F1 u( u
#include <winsock2.h>
% F" @; n# L: u#include "util.h" 6 Y& j/ F& U$ l0 E" Q) _
0 t3 s  w# W1 E$ R
class TEA {
, H- B8 O* T. wpublic: 8 j1 z" o! {$ d# U
    TEA(const byte *key, int round = 32, bool isNetByte = false); . m, q4 x8 C7 F6 a6 X/ s
    TEA(const TEA &rhs); 8 P# x5 P7 m4 p3 {3 d+ A
    TEA& operator=(const TEA &rhs); $ d( Q# Q4 e4 B" |% D3 X% @
    void encrypt(const byte *in, byte *out); : x. b: C, ~2 `# Z
    void decrypt(const byte *in, byte *out); $ X9 S% i9 T/ H# y' A7 i
private:
8 N. C* M4 v  q8 e! y    void encrypt(const ulong *in, ulong *out); 4 b6 N; q" _5 e3 G
    void decrypt(const ulong *in, ulong *out);
6 L6 |: j/ u9 ^& x7 S+ W5 K$ w    ulong ntoh(ulong netlong) { return _isNetByte ? ntohl(netlong) : netlong; }
* F8 [+ Q1 R- a! d) a$ m    ulong hton(ulong hostlong) { return _isNetByte ? htonl(hostlong) : hostlong; } & `) Y2 W  e6 d4 v3 r3 s1 |
private:
6 e0 F  F4 o* G. e- N/ f, Q    int _round; //iteration round to encrypt or decrypt
% N6 ]3 n" z2 t4 V' I2 C# Z) y8 w: m    bool _isNetByte; //whether input bytes come from network " V4 t+ C/ S) p; h! i
    byte _key[16]; //encrypt or decrypt key
3 l- a. s& Q3 ^2 |1 y};
! p8 v$ y! X/ X " d- u( R+ r& c8 P) q
#endif/*TEA_H*/
回复

使用道具 举报

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

tea.cpp

1 #include "tea.h" $ I: z& c( M+ Z& d. z# ?
2 #include <cstring> //for memcpy,memset
8 R9 c. @: ^/ W( ?; J0 j9 j( g' Z 3  
, m8 v4 y$ |6 N 4 using namespace std; 0 g. H2 d/ y4 [' E* T) R% v: x
5  
& H- y+ H. ^2 t# G% E# q 6 TEA::TEA(const byte *key, int round /*= 32*/, bool isNetByte /*= false*/)
5 p! h8 ^3 a6 G  H! { 7 :_round(round)
9 x( ?$ R: C& a3 o* q# s9 u 8 ,_isNetByte(isNetByte) { " a$ J3 U2 e, C; d4 V$ ?2 [9 E
9     if (key != 0)
' v& y8 |. p' n+ |% ?! c% z10         memcpy(_key, key, 16); + I5 w0 T' N; R/ v% s: V
11     else " d) x/ E; o2 F& _: K
12         memset(_key, 0, 16);
3 g7 p' R" ?% r5 _5 J13 } : ~6 P; Z; V' U: f, {3 Z
14  
2 d6 D5 Z' H! Q15 TEA::TEA(const TEA &rhs)
' |: S1 q9 E0 J) F. c16 :_round(rhs._round)
0 _, y; V6 S3 Y* J% y17 ,_isNetByte(rhs._isNetByte) { $ [: C/ h, P3 U0 ?  Z1 k
18     memcpy(_key, rhs._key, 16); + \( E% F* N9 y2 H2 q7 w0 z
19 }
* B5 P5 ]; u; O4 v2 W5 S20  
2 _5 [* J& b5 g% N# ^' U7 X21 TEA& TEA::operator=(const TEA &rhs) {
& [' `6 ]0 S; z4 s& v22     if (&rhs != this) {
( y& P# r' K$ F1 q; v23         _round = rhs._round; 2 Z9 _) ?$ Z& M
24         _isNetByte = rhs._isNetByte;
, P5 Y, T1 z  n6 j4 u25         memcpy(_key, rhs._key, 16); " M; n! U# a: g
26     }
* x9 V2 V: x, v, R, H9 F, U, W27     return *this; ( `9 H, o8 u  Y. _( Q
28 } . s$ X$ G' y( J" V+ M* V' G
29  
' u% I8 X1 q3 `30 void TEA::encrypt(const byte *in, byte *out) {
% }2 S7 D& y5 r4 g4 @3 n31     encrypt((const ulong*)in, (ulong*)out); + X  {4 [( J: N8 z+ A
32 }
% [$ {4 m) d4 N, p33  
' i8 l8 {; T+ Z! X34 void TEA::decrypt(const byte *in, byte *out) { : Y" S" w7 _* v; ^$ y! ^* }4 A
35     decrypt((const ulong*)in, (ulong*)out);
( |; M: f+ F5 i: L36 }
2 r7 _% z; y/ A1 G5 [5 {37  ! `8 o, _/ D$ M0 J
38 void TEA::encrypt(const ulong *in, ulong *out) { : B- m. c5 N6 g2 k$ w5 `
39  9 x" ~: D) y2 e
40     ulong *k = (ulong*)_key;
$ ^" W. }4 o3 v6 N$ l# V3 m9 _+ z" D8 E41     register ulong y = ntoh(in[0]);
8 d5 F2 b' A1 m$ W' X42     register ulong z = ntoh(in[1]);
% _( i+ @0 Q9 c2 Y43     register ulong a = ntoh(k[0]); & j, q$ v' a) q/ |- I
44     register ulong b = ntoh(k[1]);
9 c7 P& t; D6 I' S8 C45     register ulong c = ntoh(k[2]); ! C/ n: F+ r$ Q8 ]5 j4 K8 m- F
46     register ulong d = ntoh(k[3]); " Q9 k- v0 {2 m& g
47     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */ 1 K$ r. l- `' ^- n/ i& R) {% t5 p1 {
48     register int round = _round; 7 Z5 d! b' ^5 S# S
49     register ulong sum = 0;
* P+ C0 l% ?) q) U6 e. Z50  
- V5 T4 v" M4 ?51     while (round--) {    /* basic cycle start */
5 P' w! ?% F* _7 l1 [5 j( {52         sum += delta; , `/ i/ A7 A9 j( C0 M% k, k% Y
53         y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); ) m& @. H% W9 b* S# s" w0 w
54         z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); 2 S; J. Z& m4 y9 b7 \! I7 N
55     }    /* end cycle */
0 M' m5 f; z/ L! S56     out[0] = ntoh(y);
9 \. `) x2 ?7 Z57     out[1] = ntoh(z); 0 G& [+ j% a8 a  U; G; j: v
58 } 1 d, D* j/ X- l' {+ u" L
59  0 `' p, j' g+ O
60 void TEA::decrypt(const ulong *in, ulong *out) {
: Y& l$ f) F5 Q3 g5 U0 c61  % o' h9 y& c& ^3 y! c$ g" ~
62     ulong *k = (ulong*)_key;
4 N& v! m- Q, m. ]  f7 [+ r63     register ulong y = ntoh(in[0]); ; A; o5 |1 O; V
64     register ulong z = ntoh(in[1]);
: G3 s9 q/ x8 n* Q; O: Z65     register ulong a = ntoh(k[0]);
$ D7 I; s& }2 P; j& E& p; ^66     register ulong b = ntoh(k[1]);
; J. B' J5 a% d( R& }67     register ulong c = ntoh(k[2]); 9 r8 d4 h1 t; Y9 H9 p5 B
68     register ulong d = ntoh(k[3]); $ Q3 M5 N0 ~9 ]+ ?
69     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */ / m$ ~" @4 P" O3 u$ A. i
70     register int round = _round; : u1 [2 m) j; q: S; b
71     register ulong sum = 0;
0 ?! ^" D0 q. ?% _: L( s  ~4 b72  
2 Z1 W/ D1 c; ~2 a- v8 Y$ g73     if (round == 32)
1 d$ u! C6 m/ P4 U74         sum = 0xC6EF3720; /* delta << 5*/ ! l5 A5 T$ t1 a0 q5 u1 g
75     else if (round == 16) 1 _+ ]* v6 ^' x  i4 t
76         sum = 0xE3779B90; /* delta << 4*/
! \: N$ p- k. Q77     else ; {. B3 Y+ U9 R: e* |: n
78         sum = delta << static_cast<int>(logbase(2, round)); & y/ D- G- N; K8 o4 Y- H
79  ( g: e2 o. z, X/ b% N
80     while (round--) {    /* basic cycle start */ ' [1 i' ?( w4 U% S+ V) ]* h' D- M
81         z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); & Q4 z2 C6 Q( P  K5 ^7 L2 X
82         y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
/ k+ t. ]% z- k& w83         sum -= delta; ) m1 s: R' J9 P* W+ ?& r+ |% r
84     }    /* end cycle */   j: ~8 n  P' X( j% P
85     out[0] = ntoh(y);
2 H3 Y0 \5 J  ]5 U* T86     out[1] = ntoh(z); - m; c2 J/ |  g& G2 E5 }# c
87 }  M% `4 X& J% d4 K
; o/ \% [0 Q( r7 E
需要说明的是TEA的构造函数: 8 n% j4 p  _* T5 `, T" Q6 {
TEA(const byte *key, int round = 32, bool isNetByte = false); 7 V9 D1 P8 _/ o: R& O3 ~
1.key - 加密或解密用的128-bit(16byte)密钥。
, I; ]' G- A; V4 I5 k5 \/ G: c( L6 H2.round - 加密或解密的轮数,常用的有64,32,16。
* g/ r, y! H) f3.isNetByte - 用来标记待处理的字节是不是来自网络,为true时在加密/解密前先要转换成本地字节,执行加密/解密,然后再转换回网络字节。偷偷告诉你,QQ就是这样做的!
- c$ B5 G, u" f9 d/ M$ ^6 w
' z/ W; N( p- }* B: X$ A0 n最后当然少不了测试代码:
回复

使用道具 举报

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

test.cpp

1 #include "tea.h"
  f( p+ q8 [( `& x1 S$ d 2 #include "util.h" $ C& y: ^. r: E4 N
3 #include <iostream>
6 J) }9 O* J* C# s/ \0 W+ @$ c1 } 4  
) N0 K" c2 O2 j; H) }1 k$ ]; f 5 using namespace std; - s" V+ `& b' J- U4 j; E
6  
( u: T9 \$ |3 q6 m7 Q$ e$ @ 7 int main() {
. k( t+ Y+ z% c# Q5 n3 ?3 _; C 8  
1 `) Z+ i( o3 K4 D* O: n& C 9     const string plainStr("AD DE E2 DB B3 E2 DB B3");
3 t  x$ j3 E9 ^& ~* y10     const string keyStr("3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4"); 3 h4 D& d7 U8 A* \# g
11     const int SIZE_IN = 8, SIZE_OUT = 8, SIZE_KEY = 16;
- ]- H$ O) |+ s% s- v& b) k" F12     byte plain[SIZE_IN], crypt[SIZE_OUT], key[SIZE_KEY];
) ?' C3 ?4 e0 p' |: ^13  
" c/ r+ W3 ]/ G0 q! f2 n% S14     size_t size_in = hexStringToBytes(plainStr, plain); : ]- ]$ h& x( G
15     size_t size_key = hexStringToBytes(keyStr, key);
( {% D; ^: l' L: s' y7 {  z16  # p! T. F2 W# i3 F
17     if (size_in != SIZE_IN || size_key != SIZE_KEY)
( |; R5 R0 `  i' v. O18         return -1;
& [8 V4 E% ~9 l( n. }19  
) e/ R8 s% t9 t! k- e20     cout << "Plain: " << bytesToHexString(plain, size_in) << endl; ) S8 i$ z7 o. i- Q
21     cout << "Key  : " << bytesToHexString(key, size_key) << endl; $ @* W: ?/ _9 t  c
22  
" T2 f# M2 J9 p: y23     TEA tea(key, 16, true); 3 ~5 l, q7 Q5 F/ Q) ^0 {, K; E4 B
24     tea.encrypt(plain, crypt); $ Q1 U+ K; a( K, J6 d
25     cout << "Crypt: " << bytesToHexString(crypt, SIZE_OUT) << endl; ) t; k1 E* s5 e; u9 T6 s
26  
8 g& L. q# R$ D6 l6 H0 K) i. G: A27     tea.decrypt(crypt, plain);
+ c- R3 w8 [& P! K28     cout << "Plain: " << bytesToHexString(plain, SIZE_IN) << endl;
/ ^5 t% a% D& P29     return 0;
9 |3 s7 G2 N- R$ c# B; u3 v. o$ B  D30 }8 X! N' C1 t" ]
  n: Y* R: ]* A+ S. Z1 M' [
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sailing0123/archive/2008/04/28/2339231.aspx
  C- r  H8 {/ p9 Q& W( {运行结果: ) o' H* {  D# t8 ^' \
Plain: AD DE E2 DB B3 E2 DB B3 9 Z' R2 \  t5 _% c% @8 C
Key  : 3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4 6 f  R, M! B+ n; G& N
Crypt: 3B 3B 4D 8C 24 3A FD F2
, A, P) m# g. ^- _/ @" f, XPlain: AD DE E2 DB B3 E2 DB B3
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-1 23:40 , Processed in 0.020270 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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