DES加密算法C语言 实验报告Word格式.docx
- 文档编号:17290777
- 上传时间:2022-11-30
- 格式:DOCX
- 页数:15
- 大小:19.03KB
DES加密算法C语言 实验报告Word格式.docx
《DES加密算法C语言 实验报告Word格式.docx》由会员分享,可在线阅读,更多相关《DES加密算法C语言 实验报告Word格式.docx(15页珍藏版)》请在冰豆网上搜索。
FILE*fin,*fout;
/*Fordebug*/
inlinevoidprintBite(bit64num)
{
while(num)
{
printf("
%d"
num%2);
num>
>
=1;
}
printf("
\n"
);
}
/*TransferfromchartobitinEncrtption*/
inlinebit64ToBit(
char*in//sourcestring
);
/*TransferfromchartobitinDeciphtering*/
inlinebit64DeToBit(
/*Transferfrombittochar*/
inlinevoidToBite(
char*out,//outstring
bit64num//sourcebits
/*Permutation*/
inlinebit64substitute(
bit64num,//sourcebits
constint*table,//Permutationtable
size_tlen//bitslength
/*Bitrecyclelooptoleft*/
inlinebit28MoveLeft(
bit28key,//sourcebits
intlen//bitslength
/*Bitrecyclelooptoright*/
inlinebit28MoveRight(
bit28key,//sourcebits
/*Dividebitsintotwoparts*/
inlinevoiddivide(
intlen,//lengthofeachbits
bit32*L,//leftoutbits
bit32*R//rightoutbits
/*Sbox*/
inlinebit32SChange(
bit48num//sourcebits
/*Fbox*/
inlinebit32FChange(
bit32num,//sourcebits
bit48key//secretkey
/*Keyinitialization*/
inlinevoidSetKey(
char*in//stringofkey
/*Enryption*/
inlinevoidDES(
char*message//messagestobeencrypted
/*Deciphering*/
inlinevoidDecipher(
char*message//messagestobedeciphered
/*Initialization*/
inlinevoidinit();
intmain()
init();
system("
pause"
return0;
inlinevoidinit()
/*Setsecretkey*/
Pleaseinputyoursecretkey(8digits):
charkey[10000];
scanf("
%s"
key);
if(strlen(key)!
=8)
ERRORKey\n"
return;
SetKey(key);
/*SetmodeEncryptionorDeciphering*/
Pleaseinputthemode(\"
E\"
forEncrypt,\"
D\"
forDecipher):
void(*p)(char*);
intdelta=8;
switch(getch())
case'
E'
:
p=DES;
delta=8;
break;
D'
p=Decipher;
delta=16;
default:
ERROR!
return;
/*Loadfile*/
Pleaseinputthepathoftheinfile:
charmessage[10000],in[100],out[100];
in);
Pleaseinputthepathoftheoutfile:
out);
fin=freopen(in,"
r"
stdin);
fout=freopen(out,"
w"
stdout);
/*Ifsuccess*/
if(!
fin||!
fout)
Erroropenfile!
/*Readfile*/
while(gets_s(message))
for(inti=0;
i<
strlen(message);
i+=delta)
{
p(message+i);
}
/*Closestream*/
fclose(stdin);
fclose(stdout);
fclose(fin);
fclose(fout);
inlinebit64ToBit(char*in)
/*Ifvalid*/
in)
return0;
/*Copychar**/
chartemp[8];
memset(temp,'
'
8*sizeof(char));
for(inti=0;
strlen(in)&
&
ENCRYPT_LENGTH;
i++)
temp[i]=in[i];
/*Transfertobit*/
bit64key=0x0;
key|=((bit64)temp[i]<
<
(ENCRYPT_LENGTH*i));
returnkey;
inlinebit64DeToBit(char*in)
chartemp[64/DECIPHER_LENGTH];
64/DECIPHER_LENGTH;
if(in[i]>
='
A'
)
temp[i]=in[i]-'
7'
;
else
if(in[i]>
0'
{
temp[i]=in[i]-'
}
}
(DECIPHER_LENGTH*i));
inlinevoidToBite(char*out,bit64num)
if(strlen(out)<
=ENCRYPT_LENGTH)
out=(char*)malloc(sizeof(char)*(ENCRYPT_LENGTH+1));
memset(out,0,sizeof(char)*(ENCRYPT_LENGTH+1));
out[i]=num&
0xFF;
=8;
inlinebit64substitute(bit64num,constint*table,size_tlen)
bit64out=0;
/*Calculation*/
len;
out|=((bit64)((num>
(table[i]-1))&
1)<
i);
returnout;
inlinebit28MoveLeft(bit28key,intlen)
bit28temp=0;
temp=key<
(28-len);
//rightbits
key=key>
//leftbits
key|=temp;
//compare
key&
=0x0FFFFFFF;
//deletehighestfourbits
inlinebit28MoveRight(bit28key,intlen)
temp=key>
key=key<
inlinevoiddivide(bit64num,intlen,bit32*L,bit32*R)
*L=*R=0;
*L=num&
MAX32;
num>
=len;
*R=num&
inlinebit32SChange(bit48num)
bit32key=0;
8;
bit32x,y;
x=(num>
1)&
0x0F;
//themiddlefourbits
y=(((num>
5)&
1)|(num&
1);
//thefirstandthelastbits
key|=(S[i][y][x]<
(i*4));
//permutate
=6;
//changetonext
inlinebit32FChange(bit32num,bit48key)
bit48temp=substitute(num,E,sizeof(E)/sizeof(E[0]));
temp^=key;
num=SChange(temp);
returnsubstitute(num,P,sizeof(P)/sizeof(P[0]));
inlinevoidSetKey(char*in)
bit64key=ToBit(in);
bit28C,D;
key=substitute(key,PC1,sizeof(PC1)/sizeof(PC1[0]));
divide(key,28,&
C,&
D);
16;
C=MoveLeft(C,Move[i]);
D=MoveLeft(D,Move[i]);
key=(bit64)C|((bit64)D<
28);
SubKey[i]=substitute(key,PC2,48);
inlinevoidDES(char*message)
bit64BitMes=substitute(ToBit(message),IP,sizeof(IP)/sizeof(IP[0]));
bit32L,R,temp;
divide(BitMes,32,&
L,&
R);
/*16rounds*/
temp=R;
R=FChange(R,SubKey[i]);
R^=L;
L=temp;
BitMes=(bit64)L|((bit64)R<
32);
BitMes=substitute(BitMes,IPR,sizeof(IPR)/sizeof(IPR[0]));
/*printencryptedmessage*/
chartemp=(0xF&
(BitMes>
(i*4)));
temp+=(temp>
9?
:
%c"
temp);
inlinevoidDecipher(char*message)
bit64BitMes=substitute(DeToBit(message),IP,sizeof(IP)/sizeof(IP[0]));
for(inti=15;
i>
=0;
i--)
temp=L;
L=FChange(L,SubKey[i]);
L^=R;
R=temp;
/*printdecipheredmessages*/
(0xFF&
(i*8))));
table.h文件
#pragmaonce
/*IPpermutationforplaintext*/
constintIP[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
};
/*IPRpermutationtoprint*/
constintIPR[64]={
40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25
/*---------------------------premutation----------------------------*/
/*theexpansionpermutation*/
staticintE[48]={
32,1,2,3,4,5,4,5,6,7,8,9,
8,9,10,11,12,13,12,13,14,15,16,17,
16,17,18,19,20,21,20,21,22,23,24,25,
24,25,26,27,28,29,28,29,30,31,32,1
/*Compressionpermutation*/
staticintPC1[56]={
57,49,41,33,25,17,9,1,58,50,42,34,26,18,
10,2,59,51,43,35,27,19,11,3,60,52,44,36,
63,55,47,39,31,23,15,7,62,54,46,38,30,22,
14,6,61,53,45,37,29,21,13,5,28,20,12,4
/*Numberofkeybitsshiftedperround*/
staticintMove[16]={
1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
staticintPC2[48]={
14,17,11,24,1,5,3,28,15,6,21,10,
23,19,12,4,26,8,16,7,27,20,13,2,
41,52,31,37,47,55,30,40,51,34,33,48,
44,49,39,56,34,53,46,42,50,36,29,32
/*-------------Ffunction---------------*/
/*Sboxespermutation*/
staticintS[8][4][16]={
//S1
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13,
//S2
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9,
//S3
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12,
//S4
7,13,14,3,0,6,9,1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DES加密算法C语言 实验报告 DES 加密算法 语言 实验 报告