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

使用道具 举报

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

unit.h

#ifndef UTIL_H
: b9 K1 i0 L+ J& j#define UTIL_H 9 A( o% V" o0 G) j4 a" M
- v$ D7 l( D1 p  [- j+ ]6 D
#include <string> & P. O" q. Y% q7 L5 ]
#include <cmath> 7 J3 }" C, r8 I. c9 I: ]( }
#include <cstdlib>
/ V' {, ^# _' l. R) d6 |
" c- H6 F& w, U1 Ltypedef unsigned char byte;
& n4 d4 z3 \7 p6 M& ?% ntypedef unsigned long ulong;
6 T' J& y/ q; ~7 K: m
8 L5 V- W+ J- }6 H% minline double logbase(double base, double x) { * d7 H1 E  C7 [7 ?- e% `( d
    return log(x)/log(base); . j9 A( ]" A# H6 x  F* y6 ~* e6 w7 R
} 0 c8 a3 c- E2 J( {! U1 `' S
! b- n: b. D: h% h
/*
% c: y3 R1 d$ Q9 t9 @% o*convert int to hex char. " T0 u6 ?! T  W3 e% L4 V2 F
*example:10 -> 'A',15 -> 'F'
" S+ a' y8 m8 \*/
) `4 W& {  Y# x9 j# n3 cchar intToHexChar(int x);
( c% f0 t8 E0 I; q8 h" G2 a " D  p& f! H% Z, X8 L
/* - k7 X! |4 d7 @+ m$ P- C  s) v: X9 ?
*convert hex char to int.
+ t( G" w. J/ G8 R: j*example:'A' -> 10,'F' -> 15
; j0 Z/ x  h) [6 ~' ]9 G; l6 d*/ 2 _( c# J# @2 B& y1 ]( }( r
int hexCharToInt(char hex);
8 W4 b- n& P( E, {4 A% h8 X+ e # {/ P& N* G; ]7 N2 o
using std::string;
  G6 b# q1 d; s. X$ [$ m0 d/* : y1 ], r5 }1 D8 b4 X. D
*convert a byte array to hex string.
; ?' M+ ^/ U9 d6 i2 o*hex string format example:"AF B0 80 7D" 2 G1 F3 ?# i& }: r" F9 g
*/
& x1 P0 g. q2 X& D7 F; c: Nstring bytesToHexString(const byte *in, size_t size); ! S2 G8 P) h* s* I

8 ^. Y2 h5 k% D: i) U' M+ M/* 6 ~  l8 x/ D( S6 L
*convert a hex string to a byte array.
# K) l+ n) z  B# w7 p5 u- v*hex string format example:"AF B0 80 7D" 8 K# z% k9 e$ \1 ]- H( I/ ^9 k
*/
5 q5 l. e! w& H/ u2 `' g2 H8 lsize_t hexStringToBytes(const string &str, byte *out); 5 w) \' k6 r; f4 Q% |7 w0 r* ]

$ ^7 F/ K. @4 b, N9 A7 k#endif/*UTIL_H*/
回复

使用道具 举报

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

util.cpp

#include "util.h"
1 D0 ?; Z9 z& f4 b#include <vector> & R; s" r0 O; }
# Y' C6 ?& P/ J+ Y$ b
using namespace std; 6 f/ Q, e. j6 \4 @: `+ w

; V) d  o% ~9 }# s7 L9 Pchar intToHexChar(int x) {
- E5 r3 }9 {3 k    static const char HEX[16] = {
. A9 v6 T# C* J. `, m" P        '0', '1', '2', '3', % S0 K$ u8 X" {7 a+ X$ a
        '4', '5', '6', '7',
' W+ t7 H) b; p4 C: d% V        '8', '9', 'A', 'B',
7 |- i9 J4 B- q7 N4 Y        'C', 'D', 'E', 'F' 0 x7 E7 n2 o$ o9 e
    };
* P4 r; M! {7 Y3 E( S" ^    return HEX[x]; / i6 g0 T  r/ e! l8 h' J) Z- }$ s
} + t+ P$ d4 K& C( [
! v! m: H7 G& t' \9 A
int hexCharToInt(char hex) { 2 T8 B, @! i$ S
    hex = toupper(hex);
+ y. K' s8 G+ I9 y    if (isdigit(hex))
) B1 I+ D! i. l. q, p/ _, K        return (hex - '0');
6 `1 Q$ `8 T- j    if (isalpha(hex))
+ I9 d0 j( B2 B        return (hex - 'A' + 10); ) D9 R! S1 U% Q4 b1 q9 N, s
    return 0;
( }5 D5 r0 D) l; {} ) s% U/ b6 v/ ^

0 u% v' V" S8 Cstring bytesToHexString(const byte *in, size_t size) {
* _0 s+ b. h# y+ B4 W1 `    string str; " _4 N; i" V9 B. A8 f, A- G8 s
    for (size_t i = 0; i < size; ++i) {
3 ~# X$ f, c( G: n        int t = in[i];
' @- O& Q" `: p2 x        int a = t / 16; ! z1 F  X, V% m. f6 B* @
        int b = t % 16; 4 f" l: ^7 {( f( g& b, T% w" {) S
        str.append(1, intToHexChar(a)); 5 _. t/ ]% V& A% i
        str.append(1, intToHexChar(b));
* |$ \' G! C- K6 y        if (i != size - 1)
  r* e) L9 z* c- N! F: ~" O            str.append(1, ' ');
2 a# A+ a1 k  q8 {    }
: x5 R2 p9 Z- a' }) f    return str;
- X% R' r; o: v# V+ Q' M}
, B# [3 f2 f2 Q: f+ E- z0 z , \# h* S& A2 U' t1 U
size_t hexStringToBytes(const string &str, byte *out) {
6 p3 W% n, u9 W% W" ]
/ Z3 Q8 Y1 e( s6 \( J6 t1 a    vector<string> vec; 0 d! j2 v3 I' T3 f6 U$ v
    string::size_type currPos = 0, prevPos = 0; % A% ~& K# H, H) q9 F1 J: k
    while ((currPos = str.find(' ', prevPos)) != string::npos) {
4 L& z, t0 L+ \& |4 _3 ~9 G        string b(str.substr(prevPos, currPos - prevPos));
6 m9 {8 G1 W; F2 O+ ]        vec.push_back(b); 1 W. M. L: o8 P7 J
        prevPos = currPos + 1;
% U3 t  L# I# Y) \# v& v( o# _    }
$ p+ S0 Y. Y) U6 Y* V, ?0 w, h$ M    if (prevPos < str.size()) { & w9 Y" r/ e5 Q8 h  K, \
        string b(str.substr(prevPos));
: G; o0 _& T3 J1 A, q! P0 d        vec.push_back(b);
, [% q. H% I' B- _# ~0 \    }   V; C( y  k% D; A  a8 L) q
    typedef vector<string>::size_type sz_type; 2 B7 D" k0 Q1 J' s0 s: Q
    sz_type size = vec.size();
* E, R) ~/ e3 Y0 v" e9 I7 a    for (sz_type i = 0; i < size; ++i) {
- |8 D/ K9 o  q4 R3 A        int a = hexCharToInt(vec[i][0]);
1 ?4 t8 K7 A$ f+ f2 N        int b = hexCharToInt(vec[i][1]);
9 V+ f9 q4 j' W8 v! x2 [        out[i] = a * 16 + b; / x$ m8 e1 F8 \/ P# v" j2 L
    } # Q7 @3 D, p* H5 ?* v, C# m: N4 _
    return size; 1 G, K) w& N8 q% s
}
回复

使用道具 举报

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

tea.h

#ifndef TEA_H 8 n2 w$ T! Y8 o
#define TEA_H
' ^( m) }7 a, j" B3 A' k . Z: W5 i8 |. [6 g3 y! Y8 ~4 D
/* ; Y- k6 e1 Y; C+ w
*for htonl,htonl   y% g. L$ p9 E5 M- G) j/ m- ~
*do remember link "ws2_32.lib" / S4 ?1 a% C+ U# D9 h7 F
*/ ! I& B- a- g+ l! H
#include <winsock2.h>
7 X; ?5 g- R4 l+ k8 `1 V8 Y#include "util.h" . U# P8 V( t8 {% y% k; r4 F

! b9 I; H6 v& c. q# J) Z, z8 u: @: Qclass TEA {
) {/ f- g  C- Cpublic: 3 E2 @" U0 Y9 O+ c/ Q
    TEA(const byte *key, int round = 32, bool isNetByte = false); 0 B6 e7 O0 B6 S6 E8 }
    TEA(const TEA &rhs);
# Z: D+ Y  F6 `8 D# c+ y6 R, ^    TEA& operator=(const TEA &rhs);
" E$ h. @5 p0 i& {+ Z, B9 ]    void encrypt(const byte *in, byte *out); # g! w0 b* E8 w: z' q6 c. ]$ ?
    void decrypt(const byte *in, byte *out);
: n# |6 K6 U$ f+ xprivate: . ~" J! [5 ]0 H5 W- s
    void encrypt(const ulong *in, ulong *out);
' ?( Z- [: r' I7 T; l" H/ ^    void decrypt(const ulong *in, ulong *out);
, x0 _  l# M( p; @4 D' Q    ulong ntoh(ulong netlong) { return _isNetByte ? ntohl(netlong) : netlong; } , k# z7 C/ p7 l/ R% J) v+ H, x* e
    ulong hton(ulong hostlong) { return _isNetByte ? htonl(hostlong) : hostlong; } / J+ O/ D' k# X0 s3 }% v' `  e
private:
4 s; w; s+ _6 m7 e' v    int _round; //iteration round to encrypt or decrypt
8 }: [: B( p8 ?6 ?) Q" j    bool _isNetByte; //whether input bytes come from network + n# T' Y" |7 d6 m: l4 R+ |5 o  U
    byte _key[16]; //encrypt or decrypt key 3 n2 ]: n9 n9 t+ N3 d0 u; @8 ?
}; # Y6 `" H% `7 |2 c+ x1 H
* K1 u/ a% S1 }% l: Q. J
#endif/*TEA_H*/
回复

使用道具 举报

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

tea.cpp

1 #include "tea.h"
2 U: d0 X1 X; E- g- U1 r. G 2 #include <cstring> //for memcpy,memset ' c6 _: H2 {; m. D
3  $ U' n: W: B: y! ]* W
4 using namespace std;
6 G: ?0 Q7 i8 o$ Y, w; c5 c 5  
! q, U+ k( U* o9 m 6 TEA::TEA(const byte *key, int round /*= 32*/, bool isNetByte /*= false*/) 5 c( {' X5 b: i8 S% [6 I3 g- z
7 :_round(round)
) |# L1 W# H% `# s! J2 U 8 ,_isNetByte(isNetByte) {
9 R; ?/ Y9 ?4 X& z9 l' U0 m 9     if (key != 0)
5 U$ m9 i. M8 w# w$ A6 s3 c& |' x10         memcpy(_key, key, 16);
, @* i' s/ B2 \11     else ( n* ?% }& S% @  t
12         memset(_key, 0, 16);   r/ F& B: |8 @2 W
13 } * y5 r8 C9 O* ?. |4 ]2 W8 a( g
14  
% O' C1 j& H) r. e# w! n; C15 TEA::TEA(const TEA &rhs) 6 n5 l# [$ o9 A1 j$ w- A
16 :_round(rhs._round)
% r2 _4 u1 E8 Y6 j9 q% u17 ,_isNetByte(rhs._isNetByte) {
2 G9 g5 T& Y4 o% `: L( b' a18     memcpy(_key, rhs._key, 16);
0 L/ S9 a+ L" B8 i" q19 }
: q: @( C' d5 l/ L& l7 T# }4 Z* Z' E20  
. \- d) ^9 n+ V$ v7 f# x21 TEA& TEA::operator=(const TEA &rhs) { $ {; f# l3 A' v0 a0 j& w8 y
22     if (&rhs != this) { / G* G; ^% P2 K5 k. `
23         _round = rhs._round;
, v6 g( K4 u( N# N& F4 _9 L6 |24         _isNetByte = rhs._isNetByte;
" ^8 K5 X$ @9 I6 l* A3 Q* {2 _25         memcpy(_key, rhs._key, 16); ( Q1 F3 e; }# c, P. L9 Y- o3 f1 Q5 F
26     }
& x' A  u* T) @6 g& b27     return *this;
8 e9 w" o1 w5 q9 Z, ?0 p6 Z( Y: a28 } $ Y2 q9 g. u; f8 U0 o* S+ S/ @
29  
7 j. A- K4 `3 z1 l7 L: \30 void TEA::encrypt(const byte *in, byte *out) { / }$ v3 ^( G1 D( e: j+ ^
31     encrypt((const ulong*)in, (ulong*)out);
7 f: @; @6 g! B# b% A& B) r7 Y32 }
: |0 y( `; w: D9 y$ A33  
0 e" K# U, E+ Z34 void TEA::decrypt(const byte *in, byte *out) {
2 U9 B7 L/ |) ]8 `35     decrypt((const ulong*)in, (ulong*)out); : z9 k3 ?0 \& l( Q0 e4 q
36 }
1 e' Y2 z1 H/ b+ `  b37  
. M+ G. ~2 j# f, N* r3 Q: G38 void TEA::encrypt(const ulong *in, ulong *out) {
4 f% m( S5 l# M$ M! U39  
" q- ]8 L+ q" u, k40     ulong *k = (ulong*)_key;
! l6 k/ l4 b- D. ~' T( d- Z$ ^0 ?41     register ulong y = ntoh(in[0]);
* M. {* H9 _& d42     register ulong z = ntoh(in[1]);
4 \: R# `: ]; F" u43     register ulong a = ntoh(k[0]); 6 O1 E0 e5 T7 H( Z! {5 v
44     register ulong b = ntoh(k[1]);
  ]6 f' n- Q& Q  Q5 a1 k- w45     register ulong c = ntoh(k[2]);
. T' m3 l: c# ]5 |9 v6 ^8 Y! J% v46     register ulong d = ntoh(k[3]); 9 ^# Y! N+ C% W& Y5 J( T' B- g- s
47     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */ ( J0 r' H: T9 _& ~5 U& m5 u5 j+ R
48     register int round = _round;
0 d( c$ I' N+ y, R& l% b( F4 e3 q$ o49     register ulong sum = 0;
8 s& n0 k9 {8 y9 k- R& C50  , h+ d7 B, V; T5 Y* I% T0 H/ v
51     while (round--) {    /* basic cycle start */
$ E# q/ V9 |4 p( Y& E2 s52         sum += delta;
4 p8 f5 x7 j  H  H4 M0 e53         y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
3 ]4 e& o, F4 v7 r+ U54         z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
& Z- k% w9 O6 k7 i& L( w3 \55     }    /* end cycle */ * J* P4 [0 J$ X/ [: c4 C5 {5 X
56     out[0] = ntoh(y);
7 U" J* X. o3 w- K: N5 N57     out[1] = ntoh(z);
& m# L1 L8 g4 {9 d# l  f# d: u. e$ y58 }
. y3 ?. \4 V" n3 x/ N% m; O59  $ X3 B  C" E' R; N" M) o$ h
60 void TEA::decrypt(const ulong *in, ulong *out) { - o$ ^1 ?3 I/ F( Z, f. P' u. _  R
61  . K$ {0 _- i7 s  i, y/ K
62     ulong *k = (ulong*)_key;
( l+ [/ w: {( h1 {* z63     register ulong y = ntoh(in[0]); 9 q  y' m& X4 F; q  A4 c4 d
64     register ulong z = ntoh(in[1]); 9 ~' z, B5 A$ ^% x" S
65     register ulong a = ntoh(k[0]);
, l- x' |) Y# ?+ I9 {! ^3 t66     register ulong b = ntoh(k[1]); 0 g7 V0 x5 y( w$ R. Z5 h
67     register ulong c = ntoh(k[2]); 7 o* A2 Z1 g: J4 {& V/ z
68     register ulong d = ntoh(k[3]); & y# O! ~. `# f/ W
69     register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */ 4 y" V' ]! R- E( L6 y' N. b
70     register int round = _round;
6 k' J) H; M* G  a: e71     register ulong sum = 0;
6 x6 |: T" N2 l2 ?% z72  
# l4 I* }! Z+ N! g73     if (round == 32) - l4 H0 w+ F) H; T: V
74         sum = 0xC6EF3720; /* delta << 5*/ 8 w: m+ c, b! w! N6 X0 C  j: |+ I
75     else if (round == 16)
3 ^0 [& F" P4 k, y76         sum = 0xE3779B90; /* delta << 4*/
! z1 M* g" _7 Z2 K3 t1 j77     else
( H$ N$ g- L6 U4 @, e' t3 i78         sum = delta << static_cast<int>(logbase(2, round)); / l3 W: y5 A# U" A2 i8 S! W; s
79  % h; X# x9 ~; l8 s: M! H, _3 L
80     while (round--) {    /* basic cycle start */ ! H1 x9 D4 V/ C7 _, ^1 u+ K
81         z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
9 [0 Q) l. v. A& E" r82         y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); & Y* D! V) w3 W" D8 H2 Q
83         sum -= delta; 6 O/ S9 ^4 s+ T; i; W) p
84     }    /* end cycle */
$ G& L0 T0 B( M4 G85     out[0] = ntoh(y);
$ t1 C2 J7 j# n& D+ f/ q$ P86     out[1] = ntoh(z);
: E. N2 d; {8 X. j6 y$ k( |87 }  Y4 R8 O% K5 M/ q# h

- b4 n" P; i, f% q需要说明的是TEA的构造函数: 3 }- @8 Z1 u* r- r- A/ n7 F1 s
TEA(const byte *key, int round = 32, bool isNetByte = false);
  z8 P: A- U, l: I" }0 V8 W! G( u" ~( ^1.key - 加密或解密用的128-bit(16byte)密钥。
  J4 V. y* @3 P) d* g2.round - 加密或解密的轮数,常用的有64,32,16。
. Y/ `6 |5 O5 N( y' y3.isNetByte - 用来标记待处理的字节是不是来自网络,为true时在加密/解密前先要转换成本地字节,执行加密/解密,然后再转换回网络字节。偷偷告诉你,QQ就是这样做的! ! n8 F# n$ i' s( A+ k

! q, i, h' S) J- K9 n4 `4 d最后当然少不了测试代码:
回复

使用道具 举报

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

test.cpp

1 #include "tea.h"
0 k$ G: e4 D! ~: k: Y 2 #include "util.h" 9 O; a; C$ H0 J9 S( V
3 #include <iostream>
9 `& S) S; k% R$ `/ |$ T 4  & D, u9 k) X( z8 K
5 using namespace std; 0 j4 `) g  Q9 n
6  7 J* h! x1 j* D5 d
7 int main() {   b* r: c, T; H! ?
8  
( N$ E* J- ^/ ]5 r6 R" t 9     const string plainStr("AD DE E2 DB B3 E2 DB B3");
! H% H3 d& Z- p4 Z$ R& B10     const string keyStr("3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4");
8 c; b( i/ T, Z5 u, m11     const int SIZE_IN = 8, SIZE_OUT = 8, SIZE_KEY = 16;
7 `* Y7 h& f, F  C6 f6 g$ ~+ P! K2 `8 ~12     byte plain[SIZE_IN], crypt[SIZE_OUT], key[SIZE_KEY];
& [6 D  z' [/ X$ Y13  
0 G% v2 ?% P4 X) q14     size_t size_in = hexStringToBytes(plainStr, plain); " W% S. L# j8 F
15     size_t size_key = hexStringToBytes(keyStr, key); # d$ Z, @! Q) u$ K4 i4 v" N7 K
16  
7 E8 x: w/ R1 ]  G% a17     if (size_in != SIZE_IN || size_key != SIZE_KEY)
3 x  s2 Q) J8 I  T+ {3 W18         return -1;
2 I; T4 W# w0 {( J19  
- n" J1 E, h. G4 _4 n20     cout << "Plain: " << bytesToHexString(plain, size_in) << endl; # g2 P3 }  @0 l& i; ]
21     cout << "Key  : " << bytesToHexString(key, size_key) << endl; 4 n  p) X# R) {8 A/ ?
22  " p0 {: ^# @: E0 M1 g4 b
23     TEA tea(key, 16, true); 8 R4 o! L+ K, @
24     tea.encrypt(plain, crypt); / n+ H3 z# r+ U% Y6 w* \% j' y" v
25     cout << "Crypt: " << bytesToHexString(crypt, SIZE_OUT) << endl;
8 E" I, r; N0 v8 r% L- i26  
8 v& c% e6 x9 a4 {/ Y27     tea.decrypt(crypt, plain);
$ A9 W& O# V  j7 ~28     cout << "Plain: " << bytesToHexString(plain, SIZE_IN) << endl;
( B5 d& n2 S4 M5 b/ k, q29     return 0; ) ^4 q- u. U. K9 f: d
30 }
( I6 a& ~/ p3 `1 c2 B2 B5 j# w. C
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sailing0123/archive/2008/04/28/2339231.aspx
0 Z9 `6 J) N& J1 {  D: _运行结果:
+ C- z. ^" A1 g4 z5 i/ \Plain: AD DE E2 DB B3 E2 DB B3
* H4 q* L% p# T9 i+ Z1 Q8 ~Key  : 3A DA 75 21 DB E2 DB B3 11 B4 49 01 A5 C6 EA D4
( J, L# C- f7 ]5 T* pCrypt: 3B 3B 4D 8C 24 3A FD F2 + L8 X+ I( f7 x
Plain: AD DE E2 DB B3 E2 DB B3
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-30 10:42 , Processed in 0.035903 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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