大佬教程收集整理的这篇文章主要介绍了C++中四种加密算法之DES源代码,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
DES算法是一种最通用的对称密钥算法,因为算法本身是公开的,所以其安全性在于密钥的安全性。基于密钥的算法通常有两类:对称算法和公开密钥算法。对称算法的对称性体现在加密密钥能够从解密密钥推算出来,反之亦然。
在大多数对称算法中,加解密的密钥是相同的,DES就是这样。可见,对称密钥算法的加解密密钥都是保密的。而公开密钥算法的加密密钥是公开的,解密密钥是保密的。
DES具体算法如下:
////////////////////////////////////////////////////////////////////////// /* ProvIDed by 朱孟斌,National University Of Technology Email: zmbtsubasa@gmailcom This product is free for use */ ////////////////////////////////////////////////////////////////////////// #ifndef Des_H #define Des_H //! enum bool{false,true}; /*! if bool is not supported,use this or just replace with char and use 1 for true,0 for false; @see enum {ENCRYPT,DECRYPT}; */ enum {ENCRYPT,DECRYPT}; /*@brIEf 16圈子密钥*/ static bool SubKey[2][16][48];// 16圈子密钥 /*@brIEf 3次DES标志*/ static bool Is3DES;// 3次DES标志 static char Tmp[256]; static char deskey[16]; typedef bool (*PSubKey)[16][48]; class _declspec(dllexport) Des { public: Des(); ~Des(); //! Type―ENCRYPT:加密,DECRYPT:解密 /*! 输出缓冲区(Out)的长度 >= ((datalen+7)/8)*8,即比datalen大的且是8的倍数的最小正整数 In 可以= Out,此时加/解密后将覆盖输入缓冲区(In)的内容 当keylen>8时系统自动使用3次DES加/解密,否则使用标准DES加/解密超过16字节后只取前16字节 @see bool Des_Go(char *Out,char *In,long datalen,const char *Key,int keylen,bool Type = ENCRYPT); */ bool Des_Go(char *Out,bool Type = ENCRYPT); private: /*! @brIEf 标准DES加/解密 @see static voID DES(char Out[8],char In[8],const PSubKey pSubKey,bool Type); */ static voID DES(char Out[8],bool Type);//标准DES加/解密 /*! @brIEf 设置密钥 @see static voID SetKey(const char* Key,int len); */ static voID SetKey(const char* Key,int len);// 设置密钥 /*! @brIEf 设置子密钥 @see static voID SetSubKey(PSubKey pSubKey,const char Key[8]); */ static voID SetSubKey(PSubKey pSubKey,const char Key[8]);// 设置子密钥 /*! @brIEf f 函数 @see static voID F_func(bool In[32],const bool Ki[48]); */ static voID F_func(bool In[32],const bool Ki[48]);// f 函数 /*! @brIEf S 盒代替 @see static voID S_func(bool Out[32],const bool In[48]); */ static voID S_func(bool Out[32],const bool In[48]);// S 盒代替 /*! @brIEf 变换 @see static voID transform(bool *Out,bool *In,const char *table,int len); */ static voID transform(bool *Out,int len);// 变换 /*! @brIEf 异或 @see static voID Xor(bool *InA,const bool *InB,int len); */ static voID Xor(bool *InA,int len);// 异或 /*! @brIEf 循环左移 @see static voID RotateL(bool *In,int len,int loop); */ static voID RotateL(bool *In,int loop);// 循环左移 /*! @brIEf 字节组转换成位组 @see static voID BytetoBit(bool *Out,const char *In,int bits); */ static voID BytetoBit(bool *Out,int bits);// 字节组转换成位组 /*! @brIEf 位组转换成字节组 @see static voID BitToByte(char *Out,const bool *In,int bits); */ static voID BitToByte(char *Out,int bits);// 位组转换成字节组 }; ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// #endif
DES.cpp文件:
////////////////////////////////////////////////////////////////////////// /* ProvIDed by 朱孟斌,National University Of Technology Email: zmbtsubasa@gmailcom This product is free for use */ ////////////////////////////////////////////////////////////////////////// #include "memoryh" #include "Desh" ////////////////////////////////////////////////////////////////////////// // initial permutation IP const static char IP_table[64] = { 58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7 }; // final permutation IP^-1 const static char IPR_table[64] = { 40,7,58,25 }; // expansion operation matrix static const char E_table[48] = { 32,1 }; // 32-bit permutation function P used on the output of the S-Boxes const static char P_table[32] = { 16,25 }; // permuted choice table (key) const static char PC1_table[56] = { 57,4 }; // permuted choice key (table) const static char PC2_table[48] = { 14,32 }; // number left rotations of pc1 const static char LOOP_table[16] = { 1,1 }; // The (in)famous S-Boxes const static char S_Box[8][4][16] = { // S1 14,// S2 15,// S3 10,// S4 7,// S5 2,// S6 12,// S7 4,// S8 13,11 }; ////////////////////////////////////////////////////////////////////////// // Code starts: ////////////////////////////////////////////////////////////////////////// Des::Des() { } Des::~Des() { } bool Des::Des_Go(char *Out,bool Type) { if( !( Out && In && Key && (datalen=(datalen+7)&0xfffffff8) ) ) return false; SetKey(Key,keylen); if( !Is3DES ) { // 1次DES for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8) DES(Out,In,&SubKey[0],Type); } else{ // 3次DES 加密:加(key0)-解(key1)-加(key0) 解密::解(key0)-加(key1)-解(key0) for(long i=0,In+=8) { DES(Out,Type); DES(Out,Out,&SubKey[1],!Type); DES(Out,Type); } } return true; } voID Des::SetKey(const char* Key,int len) { memset(deskey,16); memcpy(deskey,Key,len>16?16:len); SetSubKey(&SubKey[0],&deskey[0]); Is3DES = len>8 ? (SetSubKey(&SubKey[1],&deskey[8]),true) : false; } voID Des::DES(char Out[8],bool Type) { static bool M[64],tmp[32],*li=&M[0],*Ri=&M[32]; BytetoBit(M,64); transform(M,M,IP_table,64); if( Type == ENCRYPT ){ for(int i=0; i<16; ++i) { memcpy(tmp,Ri,32); F_func(Ri,(*pSubKey)[i]); Xor(Ri,li,32); memcpy(li,tmp,32); } }else{ for(int i=15; i>=0; --i) { memcpy(tmp,32); F_func(li,(*pSubKey)[i]); Xor(li,32); memcpy(Ri,32); } } transform(M,IPR_table,64); BitToByte(Out,64); } voID Des::SetSubKey(PSubKey pSubKey,const char Key[8]) { static bool K[64],*KL=&K[0],*KR=&K[28]; BytetoBit(K,64); transform(K,K,PC1_table,56); for(int i=0; i<16; ++i) { RotateL(KL,LOOP_table[i]); RotateL(KR,LOOP_table[i]); transform((*pSubKey)[i],PC2_table,48); } } voID Des::F_func(bool In[32],const bool Ki[48]) { static bool MR[48]; transform(MR,E_table,48); Xor(MR,Ki,48); S_func(In,MR); transform(In,P_table,32); } voID Des::S_func(bool Out[32],const bool In[48]) { for(char i=0,j,k; i<8; ++i,In+=6,Out+=4) { j = (In[0]<<1) + In[5]; k = (In[1]<<3) + (In[2]<<2) + (In[3]<<1) + In[4]; BytetoBit(Out,&S_Box[i][j][k],4); } } voID Des::transform(bool *Out,int len) { for(int i=0; i<len; ++i) Tmp[i] = In[ table[i]-1 ]; memcpy(Out,Tmp,len); } voID Des::Xor(bool *InA,int len) { for(int i=0; i<len; ++i) InA[i] ^= InB[i]; } voID Des::RotateL(bool *In,int loop) { memcpy(Tmp,loop); memcpy(In,In+loop,len-loop); memcpy(In+len-loop,loop); } voID Des::BytetoBit(bool *Out,int bits) { for(int i=0; i<bits; ++i) Out[i] = (In[i>>3]>>(i&7)) & 1; } voID Des::BitToByte(char *Out,int bits) { memset(Out,bits>>3); for(int i=0; i<bits; ++i) Out[i>>3] |= In[i]<<(i&7); } ////////////////////////////////////////////////////////////////////////// // Code ends //////////////////////////////////////////////////////////////////////////
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是大佬教程为你收集整理的C++中四种加密算法之DES源代码全部内容,希望文章能够帮你解决C++中四种加密算法之DES源代码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。