📄 hsj.c
字号:
}; unsigned char s5[4][16]={ {2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9}, {14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6}, {4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14}, {11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3} }; unsigned char s6[4][16]={ {12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11}, {10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8}, {9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6}, {4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13} }; unsigned char s7[4][16]={ {4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1}, {13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6}, {1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2}, {6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12} }; unsigned char s8[4][16]={ {13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7}, {1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2}, {7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8}, {2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11} }; unsigned char temp[6]; unsigned char temp1[8]={0}; int i; unsigned char x[8]={0}; unsigned char y[8]={0}; unsigned char EPplaintext[6]; unsigned char output1[4]={0}; EP(EPplaintext,r); for(i=0;i<6;i++) temp[i]^=k[i]; temp1[0]=(temp[0]>>2); temp1[1]=(temp[0]&3); temp1[1]<<=2; temp1[1]=temp1[1]+(temp[1]>>4); temp1[2]=(temp[1]&15); temp1[2]<<=4; temp1[2]=temp1[2]+(temp[2]>>6); temp1[3]=(temp[2]&63); temp1[4]=(temp[3]>>2); temp1[5]=(temp[3]&3); temp1[5]<<=2; temp1[5]=temp1[5]+(temp[4]>>4); temp1[6]=(temp[4]&15); temp1[6]<<=4; temp1[6]=temp1[6]+(temp[5]>>6); temp1[7]=(temp[5]&63); for(i=0;i<8;i++){ x[i]+=getbit(3,temp1[i]); x[i]<<=1; x[i]+=getbit(8,temp1[i]); y[i]+=getbit(4,temp1[i]); y[i]<<=1; y[i]+=getbit(5,temp1[i]); y[i]<<=1; y[i]+=getbit(6,temp1[i]); y[i]<<=1; y[i]+=getbit(7,temp1[i]); } output1[0]+=s1[x[0]][y[0]]; output1[0]<<=4; output1[0]+=s2[x[1]][y[1]]; output1[1]+=s3[x[2]][y[2]]; output1[1]<<=4; output1[1]+=s4[x[3]][y[3]]; output1[2]+=s5[x[4]][y[4]]; output1[2]<<=4; output1[2]+=s6[x[5]][y[5]]; output1[3]+=s7[x[6]][y[6]]; output1[3]<<=4; output1[3]+=s8[x[7]][y[7]]; P(output,output1); return 1; } void P(unsigned char *output,unsigned char *input) { unsigned char map1[]={ 16,7,20,21,29,12,28,17, 1,15,23,26,5,18,31,10, 2,8,24,14,32,27,3,9, 19,13,30,6,22,11,4,25 }; int i; int j; int k=7; for(i=0;i<4;i++){ for(j=0;j<8;j++){ output[i]=output[i]+getbit(map1[j+i*8]%8,input[(map1[j+i*8]-1)/8]); if(k>0) output[i]=output[i]<<1; k--; } k=7; } } void FK(unsigned char *l,unsigned char *r,unsigned char *sk,unsigned char *output) { int i; unsigned char result[4]={0}; F(r,sk,result); for(i=0;i<4;i++) result[i]^=l[i]; for(i=0;i<4;i++) output[i]=r[i]; for(i=4;i<8;i++) output[i]=result[i-4]; } int PC1(unsigned char *cd,unsigned char *input) { unsigned char map1[]={ 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 }; int i; int j; int k=7; for(i=0;i<7;i++){ for(j=0;j<8;j++){ cd[i]=cd[i]+getbit(map1[j+i*8]%8,input[(map1[j+i*8]-1)/8]); if(k>0) cd[i]=cd[i]<<1; k--; } k=7; } return 1; } int PC2(unsigned char *output,unsigned char *input) { unsigned char map1[]={ 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,45,33,48,44,49,39,56, 34,53,46,42,50,36,29,32 }; int i; int j; int k=7; for(i=0;i<6;i++){ for(j=0;j<8;j++){ output[i]=output[i]+getbit(map1[j+i*8]%8,input[(map1[j+i*8]-1)/8]); if(k>0) output[i]=output[i]<<1; k--; } k=7; } return 1; } int LS1(unsigned char *input) { int i[4]={0}; int j[4]={0}; unsigned char temp=0; unsigned char temp1=0; if(getbit(1,input[0])==1)i[0]=1; if(getbit(1,input[1])==1)i[1]=1; if(getbit(1,input[2])==1)i[2]=1; if(getbit(1,input[3])==1)i[3]=1; input[0]<<=1; input[1]<<=1; input[2]<<=1; input[0]+=i[1]; input[1]+=i[2]; input[2]+=i[3]; if(getbit(5,input[3])==1)j[0]=1; if(getbit(1,input[4])==1)j[1]=1; if(getbit(1,input[5])==1)j[2]=1; if(getbit(1,input[6])==1)j[3]=1; input[4]<<=1; input[5]<<=1; input[6]<<=1; input[4]+=j[2]; input[5]+=j[3]; input[6]+=j[0]; temp=input[3]; temp1=input[3]; temp<<=1; temp>>=5; temp<<=1; if(i[0]==1) temp+=1; temp<<=4; temp1<<=1; if(j[1]==1) temp1=temp1+1; temp1&=15; input[3]=temp+temp1; return 1; } int is_InputKeyRight(char *inputkey)//输入的密钥只能为8位的字符串 { if(strlen(inputkey)!=8) return 0; return 1; } void GenerateSubkey(char sk[16][6],char *inputkey) { int i; unsigned char destkey[7]={0}; unsigned char lstime[]={ 1,1,2,2,2,2,2,2, 1,2,2,2,2,2,2,1 }; PC1(destkey,inputkey); for(i=0;i<16;i++){ LS1(destkey); if(lstime[i]==2) LS1(destkey); PC2(sk[i],destkey); } } void des(unsigned char *output,unsigned char *plaintext,unsigned char sk[16][6],int flag)//flag==0crypt;flag==1decrypt { unsigned char step1[8]={0}; unsigned char step2[8]={0}; unsigned char stepr[4]={0}; unsigned char stepl[4]={0}; unsigned char temp[4]; int i; int j; IP(step1,plaintext); for(i=0;i<4;i++) stepl[i]=step1[i]; for(i=0;i<4;i++) stepr[i]=step1[i+4]; if (flag==0){ for(j=0;j<16;j++){ FK(stepl,stepr,sk[j],step2); for(i=0;i<4;i++) stepl[i]=step2[i]; for(i=0;i<4;i++) stepr[i]=step2[i+4]; } } else{ for(j=0;j<16;j++){ FK(stepl,stepr,sk[15-j],step2); for(i=0;i<4;i++) stepl[i]=step2[i]; for(i=0;i<4;i++) stepr[i]=step2[i+4]; } } for(i=0;i<4;i++) temp[i]=step2[i]; for(i=0;i<4;i++) step2[i]=step2[i+4]; for(i=0;i<4;i++) step2[i+4]=temp[i]; IP1(output,step2); } void arrayxor(unsigned char *dest,unsigned char *input) { int i; for(i=0;i<8;i++) dest[i]=input[i]^dest[i]; } int Crypt(char *sourcepath,char *destpath,char *inputkey,char *inputiv) { unsigned char plaintext[8]={0}; unsigned char sk[16][6]={0}; unsigned char output[8]={0}; unsigned char temp[8]={0}; int i; int j; int filesize; int crypttimes; FILE *fsource; FILE *fdest; for(i=0;i<8;i++) temp[i]=inputiv[i]; GenerateSubkey(sk,inputkey); fsource=fopen(sourcepath,"rb"); fdest=fopen(destpath,"wb"); fseek(fsource,0,2); filesize=ftell(fsource); rewind(fsource); crypttimes=filesize/8; while(crypttimes>0){ fread(plaintext,8,1,fsource); arrayxor(plaintext,temp); des(output,plaintext,sk,0); fwrite(output,8,1,fdest); crypttimes--; for(i=0;i<8;i++){ temp[i]=output[i]; output[i]=0; } } if(filesize%8!=0){ fread(plaintext,filesize%8,1,fsource); for(i=0;i<8-(filesize%8);i++){ plaintext[filesize%8+i]=255; } arrayxor(plaintext,temp); des(output,plaintext,sk,0); fwrite(output,8,1,fdest); } i=filesize%8; fputc(i,fdest); fclose(fsource); fclose(fdest); return 1; } int Decrypt(char *sourcepath,char *destpath,char *inputkey,char *inputiv) { unsigned char plaintext[8]={0}; unsigned char sk[16][6]={0}; unsigned char output[8]={0}; unsigned char temp[8]={0}; int i; int filesize; int crypttimes; FILE *fsource; FILE *fdest; int flag; for(i=0;i<8;i++) temp[i]=inputiv[i]; GenerateSubkey(sk,inputkey); fsource=fopen(sourcepath,"rb"); fdest=fopen(destpath,"wb"); fseek(fsource,0,2); filesize=ftell(fsource); rewind(fsource); crypttimes=filesize/8; fseek(fsource,filesize-1,0); flag=fgetc(fsource); rewind(fsource); while(crypttimes>1){ fread(plaintext,8,1,fsource); des(output,plaintext,sk,1); arrayxor(output,temp); fwrite(output,8,1,fdest); crypttimes--; for(i=0;i<8;i++){ temp[i]=plaintext[i]; output[i]=0; } } if(flag!=0){ fread(plaintext,8,1,fsource); des(output,plaintext,sk,1); arrayxor(output,temp); fwrite(output,flag,1,fdest); } fclose(fsource); fclose(fdest); return 1; } int Check(int argc,char **argv) { if(argc!=6){ printf("命令长度有错误。\n"); return 0; } if(is_Sourcepathright(argv[1])==0){ return 0; } if(is_Destpathright(argv[2])==0){ return 0; } if(is_InputKeyRight(argv[3])==0){ printf("输入的密钥不正确。\n"); return 0; } if(is_InputKeyRight(argv[4])==0){ printf("输入的矢量不正确。\n"); return 0; } if(strlen(argv[5])>1){ printf("错误的加解密类型。\n"); return 0; } if(argv[5][0]!='e'&&argv[5][0]!='E'&&argv[5][0]!='d'&&argv[5][0]!='D'){ printf("错误的加解密类型。\n"); return 0; } return 1; } int is_Sourcepathright(char *source_path) { FILE *fp; if ((fp=fopen(source_path,"rb"))==NULL){ printf("您所输入的文件不存在."); fclose(fp); return 0; } else{ fclose(fp); return 1; } } int is_Destpathright(char *dest_path) { FILE *fp; if ((fp=fopen(dest_path,"wb"))==NULL){ printf("输入的路径不正确."); fclose(fp); return 0; } else{ fclose(fp); return 1; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -