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

📄 des.h

📁 本程序的实现des加解密的功能
💻 H
📖 第 1 页 / 共 2 页
字号:
{
	
  char *c;
  int  i,j,l,h,*str;
  str=(int *)malloc(100*sizeof(int));
  if(!str){printf("malloc is wrong!(query)"); exit(0);} //////////////////

	      if(strcmp(n,"IP")==0)     {  h=64;  str=IP;     l=64; }
	 else if(strcmp(n,"IP_1")==0)   {  h=64;  str=IP_1;   l=64; } //64位到64位         
     else if(strcmp(n,"E")==0)      {  h=48;  str=E;      l=32; } //
     else if(strcmp(n,"P")==0)      {  h=32;  str=P;      l=32; } //32位到32位
     else if(strcmp(n,"PC2")==0)    {  h=48;  str=PC2;    l=56; }
  c=(char *)malloc(h*sizeof(char)); 
  if(!c){printf("malloc is wrong!(query)"); exit(0);} //////////////////
  for(i=0;i<h;i++)
  {
      j=(str[i]-1);
      c[i]=e[j];
  }
	 return c;
}

char * getk(char * c,char *d)
{
	char *e;
    e=(char *)malloc(56*sizeof(char));
    if(!e){printf("malloc is wrong!(getk)"); exit(0);} //////////////////
    e=together(c,d,28);
    return query(e,"PC2");
}

char ** getKeys(unsigned long *k)
{
    int  i=0,j=0;
    char *c,*d,**ks; 
    c=(char *)malloc(28*sizeof(char)); 
    if(!c){printf("malloc is wrong!(getKeys)"); exit(0);} //////////////////
    d=(char *)malloc(28*sizeof(char));
    if(!d){printf("malloc is wrong!(getKeys)"); exit(0);} //////////////////
    ks=(char **)malloc(16*sizeof(char *));  
    if(!ks){printf("malloc is wrong!(getKeys)"); exit(0);} ////////////////// 
    c=interger(k[0],28);                     //将以unsigned long形式存放形式的二进制转换为
    d=interger(k[1],28);                      //以字符数组形式存放
   for(i=0;i<16;i++)
   {
   c=whbit(c,28,LOOP[i]);
   d=whbit(d,28,LOOP[i]);
   ks[i]=getk(c,d);
   }
   return ks;
}

char ** getKeyMassage(char* str)
{  
    unsigned long *e;
    e=(unsigned long *)malloc(2*sizeof(unsigned long));
    if(!e){printf("malloc is wrong!(getKeyMassage)"); exit(0);} ////////////////// 
    if(strlen(str)!=8)    //判断密钥的长度是否是64位
	{
		printf("the length of the key is wrong!\n");
		exit(0);
	}
    get56bit(getCharbit(str),e);   //进行pc1置换得到56位
    return getKeys(delevkey(e));  
}

char*  getM(char* M)
{
    unsigned long *  e;
	e=(unsigned long *)malloc(2*sizeof(unsigned long));
    if(!e){printf("malloc is wrong!(getM)"); exit(0);} ////////////////// 
	e=getCharbit(M);
    return together(interger(e[0],32),interger(e[1],32),32);
}

char ** divide(char * w)
{
  char *e1,*e2,**e;
  int i,j;
  e1=(char *)malloc(32*sizeof(char));
  if(!e1){printf("malloc is wrong!(divide)"); exit(0);} ////////////////// 
  e2=(char *)malloc(32*sizeof(char));
  if(!e2){printf("malloc is wrong!(divide)"); exit(0);} ////////////////// 
  e=(char **)malloc(2*sizeof(char *));
  if(!e){printf("malloc is wrong!(divide)\n"); exit(0);} ////////////////// 
  strncpy(e1, w, 32);  
  for(i=32,j=0;i<2*32;i++,j++)
   e2[j]=w[i];
   e[0]=e1;
   e[1]=e2;
   return e;
}

char * XOR(char *e1,char *e2,int l)
{
   char *e;
   int   i;
   e=(char *)malloc(l*sizeof(char ));
   if(!e){printf("malloc is wrong!(XOR)\n"); exit(0);} ////////////////// 
   for(i=0;i<l;i++)
   e[i]=(e1[i]^e2[i])+'0';
   return e;
}

int getS(char *d,int l)
{
	char a[2],*b;
    int i,j,h,a1,a2;
    a1=a2=0;
	b=(char *)malloc(4*sizeof(char ));
    if(!b){printf("malloc is wrong!(getS)\n"); exit(0);} ////////////////// 
	h=0;	j=0;
    for(i=0;i<6;i++)
	{
		if(i==0 || i==5)
		{
			a[j]=d[i];
			j++;
		}
		if(i!=0 && i!=5)
		{
			b[h]=d[i];
			h++;
		}
	}
   a1=change(a,2);
   a2=change(b,4);
  return S[l][a1][a2];
}

char * dealS(char *e)
{
   char *d,*end;
   int i,j,k,l,z=0,x,c;
   d=(char *)malloc(6*sizeof(char ));
   if(!d){printf("malloc is wrong!(dealS)\n"); exit(0);} ////////////////// 
   end=(char *)malloc(32*sizeof(char ));
   if(!end){printf("malloc is wrong!(dealS)\n"); exit(0);} ////////////////// 
   for(i=0,l=0;i<8;i++)
  {
	  for(k=0,j=l;k<6;j++,k++,l++)
	  {
        d[k]=e[j];
	  }
	 for(c=z,x=0;c<32 && x<4;c++,x++)
	 {
		 end[c]=(interger(getS(d,i),4))[x];
		 z++;
	 }
  }
    return end;
}

char *  F(char *w,char *k)
{
  w=dealS(XOR(query(w,"E"),k,48));//与密钥进行异或,并查询s盒,得到32位的值 //  
  w=query(w,"P");       //进行p置换
  return w;
}

char * encode(char *M,char *ky)
{
    
  char **e,*R,*L,*key,*z;
  char **k;
  int i;
  e=(char **)malloc(2*sizeof(char *));
  if(!e){printf("malloc is wrong!(encode)\n"); exit(0);} ////////////////// 
  R=(char *)malloc(32*sizeof(char ));
  if(!R){printf("malloc is wrong!(encode)\n"); exit(0);} ////////////////// 
  L=(char *)malloc(32*sizeof(char ));
  if(!L){printf("malloc is wrong!(encode)\n"); exit(0);} //////////////////
  key=(char *)malloc(48*sizeof(char));
  if(!key){printf("malloc is wrong!(encode)\n"); exit(0);} //////////////////
  k=(char **)malloc(16*sizeof(char *));
  if(!k){printf("malloc is wrong!(encode)\n"); exit(0);} //////////////////
  z=(char *)malloc(32*sizeof(char ));
  if(!z){printf("malloc is wrong!(encode)\n"); exit(0);} //////////////////
  k=getKeyMassage(ky);
  e=divide(query(getM(M),"IP")); //进行ip置换和将m分成位数相同的两部分
  L=e[0];
  R=e[1];
   for(i=0;i<16;i++)
   {
     key=k[i];
	 z=R;
     R=XOR(L,F(R,key),32);
	 L=z;
   }    
 return query(together(L,R,32),"IP_1");
  
}

char * Dencode(char *M,char *ky)
{
   
	char  *r,*e,*end;
	int i,j,l,k,h;
    r=(char *)malloc(64*sizeof(char ));
	if(!r){printf("malloc is wrong!(Dencode)\n"); exit(0);} //////////////////
    e=(char *)malloc(8 *sizeof(char ));
	if(!e){printf("malloc is wrong!(Dencode)\n"); exit(0);} //////////////////
    end=(char *)malloc(8 *sizeof(char ));
  	if(!end){printf("malloc is wrong!(Dencode)\n"); exit(0);} //////////////////
    r=encode(M,ky);
   for(i=0,l=0; i<8;i++)
   {
    for(j=l,h=0; h<8; h++,j++,l++)
	{
      e[h]=r[j];
	}
    k=change(e,8);    ///   
	end[i]=(char)k;
  }
   end[i]='\0';
   return end;

}

char * decode(char *M,char *ky)
{
  char **e,*R,*L,*key,*z;
  char **k;
  int i,j;

  e=(char **)malloc(2*sizeof(char *));
  if(!e){printf("malloc is wrong!(decode)\n"); exit(0);} //////////////////
  R=(char *)malloc(32*sizeof(char ));
  if(!R){printf("malloc is wrong!(decode)\n"); exit(0);} //////////////////
  L=(char *)malloc(32*sizeof(char ));
  if(!L){printf("malloc is wrong!(decode)\n"); exit(0);} //////////////////
  key=(char *)malloc(48*sizeof(char));
  if(!key){printf("malloc is wrong!(decode)\n"); exit(0);} //////////////////
  k=(char **)malloc(16*sizeof(char *));
  if(!k){printf("malloc is wrong!(decode)\n"); exit(0);} //////////////////
  z=(char *)malloc(32*sizeof(char ));
  if(!z){printf("malloc is wrong!(decode)\n"); exit(0);} //////////////////
  k=getKeyMassage(ky);
  e=divide(query(getM(M),"IP")); //进行ip置换和将m分成位数相同的两部分
  L=e[0];
  R=e[1];
   for(i=0,j=15;i<16 && j>=0;i++,j--)
   {
     key=k[j];
	 z=L;
     L=XOR(R,F(L,key),32);
	 R=z;	 
   }
  return query(together(L,R,32),"IP_1");
}

char * Ddecode(char *M,char *ky)
{
    char  *r,*e,*end;
	int i,j,l,k,h;
    r=(char *)malloc(64*sizeof(char ));
	if(!r){printf("malloc is wrong!(Ddecode)\n"); exit(0);} //////////////////
    e=(char *)malloc(8 *sizeof(char ));
	if(!e){printf("malloc is wrong!(Ddecode)\n"); exit(0);} //////////////////
    end=(char *)malloc(8 *sizeof(char ));
    if(!end){printf("malloc is wrong!(Ddecode)\n"); exit(0);} //////////////////
    r=decode(M,ky);
    for(i=0,l=0; i<8;i++)
   {
    for(j=l,h=0; h<8; h++,j++,l++)
	{
      e[h]=r[j];
	}
    k=change(e,8);
	end[i]=(char)k;
   }
    end[i]='\0';
    return end;
}

long int getSizefile(char* filename )
{
    FILE *fp;
    if( (fp=fopen(filename,"rb"))==NULL)
	{
	   printf("can not open file\n");
	   exit(0);
	} 
    fseek(fp,0,SEEK_END);
    return ftell(fp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -