DES加密算法Word文件下载.docx
- 文档编号:18796929
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:16
- 大小:18.65KB
DES加密算法Word文件下载.docx
《DES加密算法Word文件下载.docx》由会员分享,可在线阅读,更多相关《DES加密算法Word文件下载.docx(16页珍藏版)》请在冰豆网上搜索。
failed.
intencrypt(charkey[8],char*data,intblocks=1);
intdecrypt(charkey[8],char*data,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:
voiddes(unsignedchar*in,unsignedchar*out,intblocks);
voiddes_block(unsignedchar*in,unsignedchar*out);
unsignedlongKnL[32];
enumMode{ENCRYPT,DECRYPT};
voiddeskey(unsignedcharkey[8],Modemd);
voidusekey(unsignedlong*);
voidcookey(unsignedlong*);
voidscrunch(unsignedchar*,unsignedlong*);
voidunscrun(unsignedlong*,unsignedchar*);
voiddesfunc(unsignedlong*,unsignedlong*);
staticunsignedcharDf_Key[24];
staticunsignedshortbytebit[8];
staticunsignedlongbigbyte[24];
staticunsignedcharpc1[56];
staticunsignedchartotrot[16];
staticunsignedcharpc2[48];
staticunsignedlongSP1[64];
staticunsignedlongSP2[64];
staticunsignedlongSP3[64];
staticunsignedlongSP4[64];
staticunsignedlongSP5[64];
staticunsignedlongSP6[64];
staticunsignedlongSP7[64];
staticunsignedlongSP8[64];
};
#endif
#include<
mem.h>
#include"
des.h"
intDES:
encrypt(charkey[8],char*data,intblocks)
{
if((!
data)||(blocks<
1))
return0;
deskey(key,ENCRYPT);
des(data,data,blocks);
return1;
decrypt(charkey[8],char*data,intblocks)
deskey(key,DECRYPT);
yencrypt(charkey[8],char*data,intsize)
data)||(size<
//Thelastcharofdataisbitwisecomplementedandfilledtherest
//buffer.Ifsizeis16,itwillextendto24,and17still24.
charlastChar=*(data+size-1);
intblocks=size/8+1;
memset(data+size,~lastChar,blocks*8-size);
returnencrypt(data,data,blocks);
ydecrypt(charkey[8],char*data,intblocks,int*size)
if((!
data)||(blocks<
1))
if(!
decrypt(data,data,blocks))
if(size!
=0)
{
intpos=blocks*8-1;
charendChar=data[pos];
while((pos>
0)&
&
(data[pos]==endChar))
pos--;
if(data[pos]!
=~endChar)
*size=pos+1;
}
//-----------------------------------------------------------------------
//des
//Encrpts/Decrypts(accordingtothekeycurrentlyloadedintthe
//internalkeyregister)SOMEblocksofeightbytesataddress'
in'
//intotheblockataddress'
out'
.Theycanbethesame.
//
//"
in"
out"
block"
Numberofblocks.
voidDES:
des(unsignedchar*in,unsignedchar*out,intblocks)
for(inti=0;
i<
blocks;
i++,in+=8,out+=8)
des_block(in,out);
//des_block
//internalkeyregister)oneblockofeightbytesataddress'
des_block(unsignedchar*in,unsignedchar*out)
unsignedlongwork[2];
scrunch(in,work);
desfunc(work,KnL);
unscrun(work,out);
}
//----------------------------------------------------------------------
//deskey
//Setstheinternalkeyregister(KnR)accordingtothehexadecimal
//keycontainedinthe8bytesofhexkey,accordingtotheDES,
//forencryptionordecrytionaccordingtoMODE
isthe64bitskey.
md"
meansencryptionordecryption.
deskey(unsignedcharkey[8],Modemd)/*ThankstoJamesGillogly
&
am
p;
PhilKarn!
*/
registerinti,j,l,m,n;
unsignedcharpc1m[56],pcr[56];
unsignedlongkn[32];
for(j=0;
j<
56;
j++){
l=pc1[j];
m=l&
07;
pc1m[j]=(key[l>
>
3]&
bytebit[m])?
1:
0;
for(i=0;
16;
i++){
if(md==DECRYPT)m=(15-i)<
<
1;
elsem=i<
n=m+1;
kn[m]=kn[n]=0L;
28;
l=j+totrot[i];
if(l<
28)pcr[j]=pc1m[l];
elsepcr[j]=pc1m[l-28];
for(j=28;
56)pcr[j]=pc1m[l];
24;
if(pcr[pc2[j]])kn[m]|=bigbyte[j];
if(pcr[pc2[j+24]])kn[n]|=bigbyte[j];
cookey(kn);
return;
//cookey
//Onlycalledbydeskey.
cookey(registerunsignedlong*raw1)
registerunsignedlong*cook,*raw0;
unsignedlongdough[32];
registerinti;
cook=dough;
i++,raw1++){
raw0=raw1++;
*cook=(*raw0&
0x00fc0000L)<
6;
*cook|=(*raw0&
0x00000fc0L)<
10;
*cook|=(*raw1&
0x00fc0000L)>
*cook++|=(*raw1&
0x00000fc0L)>
0x0003f000L)<
12;
0x0000003fL)<
0x0003f000L)>
4;
0x0000003fL);
usekey(dough);
//usekey
//Onlycalledbycookey.
//Loadstheinteralkeyregisterwiththedataincookedkey.
usekey(registerunsignedlong*from)
registerunsignedlong*to,*endp;
to=KnL,endp=&
KnL[32];
while(to<
endp)*to++=*from++;
scrunch(registerunsignedchar*outof,registerunsignedlong*int
o)
*into=(*outof++&
0xffL)<
*into|=(*outof++&
8;
*into++|=(*outof++&
0xffL);
*into|=(*outof&
unscrun(registerunsignedlong*outof,registerunsignedchar*int
o)
*into++=(*outof>
24)&
0xffL;
16)&
8)&
*into++=*outof++&
*into=*outof&
desfunc(registerunsignedlong*block,registerunsignedlong*keys
)
registerunsignedlongfval,work,right,leftt;
registerintround;
leftt=block[0];
right=block[1];
work=((leftt>
4)^right)&
0x0f0f0f0fL;
right^=work;
leftt^=(work<
4);
16)^right)&
0x0000ffffL;
16);
work=((right>
2)^leftt)&
0x33333333L;
leftt^=work;
right^=(work<
2);
8)^leftt)&
0x00ff00ffL;
8);
right=((right<
1)|((right>
31)&
1L))&
0xffffffffL;
work=(leftt^right)&
0xaaaaaaaaL;
leftt=((leftt<
1)|((leftt>
for(round=0;
round<
round++){
work=(right<
28)|(right>
work^=*keys++;
fval=SP7[work&
0x3fL];
fval|=SP5[(work>
fval|=SP3[(work>
fval|=SP1[(work>
work=right^*keys++;
fval|=SP8[work&
fval|=SP6[(work>
fval|=SP4[(work>
fval|=SP2[(work>
leftt^=fval;
work=(leftt<
28)|(leftt>
work=leftt^*keys++;
right^=fval;
right=(right<
31)|(right>
1);
leftt=(leftt<
31)|(leftt>
8)^right)&
2)^right)&
16)^leftt)&
4)^leftt)&
*block++=right;
*block=leftt;
//Initialofstaticdatamembers.Thesedatawillbeusedbyallthe
//in
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DES 加密算法