TEA加密算法的C、C++实现.doc_第1页
TEA加密算法的C、C++实现.doc_第2页
TEA加密算法的C、C++实现.doc_第3页
TEA加密算法的C、C++实现.doc_第4页
TEA加密算法的C、C++实现.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

TEA加密算法的C/C+实现啥都不说,直接贴代码,这是转发的哦,来源:Linux联盟收集整理首先是C版:1voidencrypt(unsignedlong*v,unsignedlong*k)2unsignedlongy=v0,z=v1,sum=0,i;/*setup*/3unsignedlongdelta=0x9e3779b9;/*akeyscheduleconstant*/4unsignedlonga=k0,b=k1,c=k2,d=k3;/*cachekey*/5for(i=0;i32;i+)/*basiccyclestart*/6sum+=delta;7y+=(z5)+b);8z+=(y5)+d);/*endcycle*/910v0=y;11v1=z;121314voiddecrypt(unsignedlong*v,unsignedlong*k)15unsignedlongy=v0,z=v1,sum=0xC6EF3720,i;/*setup*/16unsignedlongdelta=0x9e3779b9;/*akeyscheduleconstant*/17unsignedlonga=k0,b=k1,c=k2,d=k3;/*cachekey*/18for(i=0;i32;i+)/*basiccyclestart*/19z-=(y5)+d);20y-=(z5)+b);21sum-=delta;/*endcycle*/2223v0=y;24v1=z;25C语言写的用起来当然不方便,没关系,用C+封装以下就OK了:util.h1#ifndefUTIL_H2#defineUTIL_H34#include5#include6#include78typedefunsignedcharbyte;9typedefunsignedlongulong;1011inlinedoublelogbase(doublebase,doublex)12returnlog(x)/log(base);131415/*16*convertinttohexchar.17*example:10-A,15-F18*/19charintToHexChar(intx);2021/*22*converthexchartoint.23*example:A-10,F-1524*/25inthexCharToInt(charhex);2627usingstd:string;28/*29*convertabytearraytohexstring.30*hexstringformatexample:AFB0807D31*/32stringbytesToHexString(constbyte*in,size_tsize);3334/*35*convertahexstringtoabytearray.36*hexstringformatexample:AFB0807D37*/38size_thexStringToBytes(conststring&str,byte*out);3940#endif/*UTIL_H*/util.cpp1#includeutil.h2#include34usingnamespacestd;56charintToHexChar(intx)7staticconstcharHEX16=80,1,2,3,94,5,6,7,108,9,A,B,11C,D,E,F12;13returnHEXx;141516inthexCharToInt(charhex)17hex=toupper(hex);18if(isdigit(hex)19return(hex-0);20if(isalpha(hex)21return(hex-A+10);22return0;232425stringbytesToHexString(constbyte*in,size_tsize)26stringstr;27for(size_ti=0;isize;+i)28intt=ini;29inta=t/16;30intb=t%16;31str.append(1,intToHexChar(a);32str.append(1,intToHexChar(b);33if(i!=size-1)34str.append(1,);3536returnstr;373839size_thexStringToBytes(conststring&str,byte*out)4041vectorvec;42string:size_typecurrPos=0,prevPos=0;43while(currPos=str.find(,prevPos)!=string:npos)44stringb(str.substr(prevPos,currPos-prevPos);45vec.push_back(b);46prevPos=currPos+1;4748if(prevPosstr.size()49stringb(str.substr(prevPos);50vec.push_back(b);5152typedefvector:size_typesz_type;53sz_typesize=vec.size();54for(sz_typei=0;isize;+i)55inta=hexCharToInt(veci0);56intb=hexCharToInt(veci1);57outi=a*16+b;5859returnsize;60tea.h1#ifndefTEA_H2#defineTEA_H34/*5*forhtonl,htonl6*dorememberlinkws2_32.lib7*/8#include9#includeutil.h1011classTEA12public:13TEA(constbyte*key,intround=32,boolisNetByte=false);14TEA(constTEA&rhs);15TEA&operator=(constTEA&rhs);16voidencrypt(constbyte*in,byte*out);17voiddecrypt(constbyte*in,byte*out);18private:19voidencrypt(constulong*in,ulong*out);20voiddecrypt(constulong*in,ulong*out);21ulongntoh(ulongnetlong)return_isNetByte?ntohl(netlong):netlong;22ulonghton(ulonghostlong)return_isNetByte?htonl(hostlong):hostlong;23private:24int_round;/iterationroundtoencryptordecrypt25bool_isNetByte;/whetherinputbytescomefromnetwork26byte_key16;/encryptordecryptkey27;2829#endif/*TEA_H*/tea.cpp1#includetea.h2#include/formemcpy,memset34usingnamespacestd;56TEA:TEA(constbyte*key,intround/*=32*/,boolisNetByte/*=false*/)7:_round(round)8,_isNetByte(isNetByte)9if(key!=0)10memcpy(_key,key,16);11else12memset(_key,0,16);131415TEA:TEA(constTEA&rhs)16:_round(rhs._round)17,_isNetByte(rhs._isNetByte)18memcpy(_key,rhs._key,16);192021TEA&TEA:operator=(constTEA&rhs)22if(&rhs!=this)23_round=rhs._round;24_isNetByte=rhs._isNetByte;25memcpy(_key,rhs._key,16);2627return*this;282930voidTEA:encrypt(constbyte*in,byte*out)31encrypt(constulong*)in,(ulong*)out);323334voidTEA:decrypt(constbyte*in,byte*out)35decrypt(constulong*)in,(ulong*)out);363738voidTEA:encrypt(constulong*in,ulong*out)3940ulong*k=(ulong*)_key;41registerulongy=ntoh(in0);42registerulongz=ntoh(in1);43registerulonga=ntoh(k0);44registerulongb=ntoh(k1);45registerulongc=ntoh(k2);46registerulongd=ntoh(k3);47registerulongdelta=0x9E3779B9;/*(sqrt(5)-1)/2*232*/48registerintround=_round;49registerulongsum=0;5051while(round-)/*basiccyclestart*/52sum+=delta;53y+=(z5)+b);54z+=(y5)+d);55/*endcycle*/56out0=ntoh(y);57out1=ntoh(z);585960voidTEA:decrypt(constulong*in,ulong*out)6162ulong*k=(ulong*)_key;63registerulongy=ntoh(in0);64registerulongz=ntoh(in1);65registerulonga=ntoh(k0);66registerulongb=ntoh(k1);67registerulongc=ntoh(k2);68registerulongd=ntoh(k3);69registerulongdelta=0x9E3779B9;/*(sqrt(5)-1)/2*232*/70registerintround=_round;71registerulongsum=0;7273if(round=32)74sum=0xC6EF3720;/*delta5*/75elseif(round=16)76sum=0xE3779B90;/*delta4*/77else78sum=deltastatic_cast(logbase(2,round);7980while(round-)/*basiccyclestart*/81z-=(y5)+d);82y-=(z5)+b);83sum-=delta;84/*endcycle*/85out0=ntoh(y);86out1=ntoh(z);87需要说明的是TEA的构造函数:TEA(const byte *key, int round = 32, bool isNetByte = false);1.key - 加密或解密用的128-bit(16byte)密钥。2.round - 加密或解密的轮数,常用的有64,32,16。3.isNetByte - 用来标记待处理的字节是不是来自网络,为true时在加密/解密前先要转换成本地字节,执行加密/解密,然后再转换回网络字节。偷偷告诉你,QQ就是这样做的!最后当然少不了测试代码:test.cpp1#includetea.h2#includeutil.h3#include45usingnamespacestd;67intmain()89conststringplainStr(ADDEE2DBB3E2DBB3);10conststringkeyStr(3ADA7521DBE2DBB311B44901A5C6EAD4);11constintSIZE_IN=8,SIZE_OUT=8,SIZE_KEY=16;12byteplainSIZE_IN,cryptSIZE_OUT,keySIZE_KEY;1314size_tsize_in=hexStringToBytes(plainStr,plain);15size_tsize_key=hexStringToBytes(keyStr,key);1617if(size_in!=SIZE_IN|size_key!=SIZE_KEY)18return-1;1920coutPlain:bytesToHexString(plain,size_in)endl;21coutKey:bytesToHexString(key,size_key)endl;2223TEAtea(key,16,true);24tea.encrypt(plain,crypt);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论