資源描述:
《des加密算法27587》由會員上傳分享,免費在線閱讀,更多相關內容在教育資源-天天文庫。
1、DES加密算法這個類實現(xiàn)了DES算法,如果你的數(shù)據(jù)塊data的大小sizeof(data)是8的倍數(shù)的話,可以使用函數(shù)DES::encrypt(key,data,sizeof(data)/8)來進行加密,用DES::decrypt(key,data,sizeof(data)/8)來進行解密。如果數(shù)據(jù)塊的大小是隨意的,那么可以使用函數(shù)DES::yencrypt()和DES::ydecrypt()來進行加解密。這個模塊的面向對象的設計不是很好,與其說是一個類,不如說是一個模塊的函數(shù)集合。#ifndef__DES_H#define__DES_HclassDES{public://
2、Encrypt/decryptthedatain"data",accordingtothe"key".//Callerisresponsibleforconfirmingthebuffersizeof"data"http://pointstois8*"blocks"bytes.//Thedataencrypted/decryptedisstoredindata.//Thereturncodeis1:success,other:failed.intencrypt(charkey[8],char*data,intblocks=1);intdecrypt(charkey[8],char*d
3、ata,intblocks=1);//Encrypt/decryptanysizedata,accordingtoaspecialmethod.//Beforecallingyencrypt,copydatatoanewbufferwithsize//calculatedbyextend.intyencrypt(charkey[8],char*data,intsize);intydecrypt(charkey[8],char*in,intblocks,int*size=0);intextend(intsize){return(size/8+1)*8;};private:vo
4、iddes(unsignedchar*in,unsignedchar*out,intblocks);voiddes_block(unsignedchar*in,unsignedchar*out);private:unsignedlongKnL[32];enumMode{ENCRYPT,DECRYPT};voiddeskey(unsignedcharkey[8],Modemd);voidusekey(unsignedlong*);voidcookey(unsignedlong*);private:voidscrunch(unsignedchar*,unsignedlong*)
5、;voidunscrun(unsignedlong*,unsignedchar*);voiddesfunc(unsignedlong*,unsignedlong*);private:staticunsignedcharDf_Key[24];staticunsignedshortbytebit[8];staticunsignedlongbigbyte[24];staticunsignedcharpc1[56];staticunsignedchartotrot[16];staticunsignedcharpc2[48];staticunsignedlongSP1[64];sta
6、ticunsignedlongSP2[64];staticunsignedlongSP3[64];staticunsignedlongSP4[64];staticunsignedlongSP5[64];staticunsignedlongSP6[64];staticunsignedlongSP7[64];staticunsignedlongSP8[64];};#endif#include#include"des.h"intDES::encrypt(charkey[8],char*data,intblocks){if((!data)
7、
8、(blocks<1))re
9、turn0;deskey(key,ENCRYPT);des(data,data,blocks);return1;};intDES::decrypt(charkey[8],char*data,intblocks){if((!data)
10、
11、(blocks<1))return0;deskey(key,DECRYPT);des(data,data,blocks);return1;};intDES::yencrypt(charkey[8],char*data,intsize){if((!data)
12、
13、(size<1))ret