⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 des_code.txt

📁 运行参数为:程序名 希望加密的文件路径 保存加密后的文件路径 密钥 E/D(E为加密,D为解密)
💻 TXT
字号:
#include <stdio.h>
#include <string.h>
//此DES算法为CBC--密码分组链接模式
//此DES算法的加解密效率约为56K每秒。
int Crypt(char *sourcepath,char *destpath,char *inputkey,char *inputiv);
int Decrypt(char *sourcepath,char *destpath,char *inputkey,char *inputiv);
int is_InputKeyRight(char *inputkey);
int Check(int argc,char **argv);
int is_Sourcepathright(char *source_path);
int is_Destpathright(char *dest_path);
void IP(unsigned char *output,unsigned char *input);
void IP1(unsigned char *output,unsigned char *input);
unsigned char getbit(int n,unsigned char ch);
int EP(unsigned char *EPplaintext,unsigned char *plaintext);
int F(unsigned char *r,unsigned char *k,unsigned char *output);
void P(unsigned char *output,unsigned char *input);
void FK(unsigned char *l,unsigned char *r,unsigned char *sk,unsigned char *output);
int PC1(unsigned char *cd,unsigned char *input);
int PC2(unsigned char *output,unsigned char *input);
int LS1(unsigned char *input);
void GenerateSubkey(char sk[16][6],char *inputkey);
void des(unsigned char *output,unsigned char *plaintext,unsigned char sk[16][6],int flag);

int main(int argc,char **argv)
{
 if (Check(argc,argv)==0){
  return -1;
 }
 if (argv[5][0]=='e'||argv[5][0]=='E'){
  if(Crypt(argv[1],argv[2],argv[3],argv[4]))
   return 1;
  else
   return -1;
 }
 if (argv[5][0]=='d'||argv[5][0]=='D'){
  if(Decrypt(argv[1],argv[2],argv[3],argv[4]))
   return 1;
  else
   return -1;
 }
 return -1;
}

unsigned char getbit(int n,unsigned char ch)//0-8均可作为n的传递值
{
 if(n==0)
  n=8;
 if(((ch>>(8-n))&1)==1)
  return 1;
 else
  return 0;
}

void IP(unsigned char *output,unsigned char *input)//output[8]
{
 unsigned char IPmap[]={
  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
 };
 int i=0;
 int j=0;
 int k=7;
 for(i=0;i<8;i++){
  for(j=0;j<8;j++){
   output[i]=output[i]+getbit(IPmap[j+i*8]%8,input[(IPmap[j+i*8]-1)/8]);
   if(k>0)
    output[i]=output[i]<<1;
   k--;
  }
  k=7;
 }
}

void IP1(unsigned char *output,unsigned char *input)
{
 unsigned char IP1map[]={
  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
 };
 int i;
 int j;
 int k=7;
 for(i=0;i<8;i++){
  for(j=0;j<8;j++){
   output[i]=output[i]+getbit(IP1map[j+i*8]%8,input[(IP1map[j+i*8]-1)/8]);
   if(k>0)
    output[i]=output[i]<<1;
   k--;
  }
  k=7;
 }
}

int EP(unsigned char *EPplaintext,unsigned char *plaintext)
{
 unsigned char EPmap[]={
  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
 };
 int i;
 int j;
 int k=7;
 for(i=0;i<6;i++){
  for(j=0;j<8;j++){
   EPplaintext[i]=EPplaintext[i]+getbit(EPmap[j+i*8]%8,plaintext[(EPmap[j+i*8]-1)/8]);
   if(k>0)
    EPplaintext[i]=EPplaintext[i]<<1;
   k--;
  }
  k=7;
 }
 return 1;
}

int F(unsigned char *r,unsigned char *k,unsigned char *output)
{
 unsigned char s1[4][16]={
  {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}
 };
 unsigned char s2[4][16]={
  {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}
 };
 unsigned char s3[4][16]={
  {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}
 };
 unsigned char s4[4][16]={
  {7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
  {13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
  {10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
  {3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}
 };
 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 + -