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

📄 test.c

📁 使用idea加密技术
💻 C
字号:
/* idea.c */
#include "idea.h"
#include "stdio.h"
#include "string.h"
#define false 0
#define true 1
int m=0,n=0;
static word16 inv(word16 x)
{
	word16 d0,d1;
	word16 q,y;
	if(x<=1)
		return x;                                             //1的乘法逆元是1,2(16)+1的乘法逆元是2(16)+1
	d1=(word16)(0x10001/x);                                   //利用大淤求一术中的dk求乘法逆元
	y=(word16)(0x10001%x);
	if(y==1)
		return low16(1-d1);
	d0=1;
	do{
		q=x/y;
		x=x%y;
		d0+=q*d1;
		if(x==1)
			return d0;
		q=y/x;
		y=y%x;
		d1+=q*d0;
	}while(y!=1);
	return low16(1-d1);
}

void en_key_idea(word16 *userkey,word16 *Z)
{
	int i,j;
	/*shifts*/
	for(j=0;j<8;j++)
		Z[j]=*userkey++;
	for(i=0;j<KEYLEN;j++){
		i++;
		Z[i+7]=((Z[i&7]<<9)|(Z[(i+1)&7]>>7));
		Z+=i&8;
		i&=7;
	}
}

void de_key_idea(word16 *Z,word16 *DK)
{
	int j;
	word16 t1,t2,t3;
	DK+=52;
	t1=inv(*Z++);
	t2=-*Z++;
	t3=-*Z++;
	*--DK=inv(*Z++);
	*--DK=t3;
	*--DK=t2;
	*--DK=t1;
	for(j=1;j<ROUNDS;j++){
		t1=*Z++; 
		*--DK=*Z++;
		*--DK=t1;
		t1=inv(*Z++);
        t2=-*Z++;
	    t3=-*Z++;
		*--DK=inv(*Z++);
        *--DK=t2;
	    *--DK=t3;
	    *--DK=t1;
	}
	t1=*Z++;
	*--DK=*Z++;
	*--DK=t1;
	t1=inv(*Z++);
	t2=-*Z++;
	t3=-*Z++;
	*--DK=inv(*Z++);
	*--DK=t3;
	*--DK=t2;
	*--DK=t1;
}

word16 mul(word16 a,word16 b)
{
	word32 p;
	if(a){
		if(b){
			p=(word32)a*b;
			b=(word16)(low16(p));                                    //低16位
			a=(word16)(p>>16);                                       //高16位
			return b-a+(b<a);
		}
		else
			return 1-a;
	}
	else
		return 1-b;
}

#define MUL(x,y) (x=mul(x,y))

static void cipher_idea(word16 in[4],word16 out[4],IDEAkey Z)
{
	word16 x1,x2,x3,x4,s2,s3,r=ROUNDS;
	x1=*in++;
	x2=*in++;
	x3=*in++;
	x4=*in;
	do{
		MUL(x1,*Z++);
		x2+=*Z++;
		x3+=*Z++;
		MUL(x4,*Z++);
		s3=x3;
		x3^=x1;
		MUL(x3,*Z++);
		s2=x2;
		x2^=x4;
		x2+=x3;
		MUL(x2,*Z++);
		x3+=x2;
		x1^=x2;
		x4^=x3;
		x2^=s3;
		x3^=s2;
	}while(--r);                                           //8轮变换
	MUL(x1,*Z++);                                          //输出变换
	*out++=x1;
	*out++=(x3+*Z++);
	*out++=(x2+*Z++);
	MUL(x4,*Z);
	*out=x4;
}
word16 exch(char ch)
{
	int i=0,j,k=0,ch1;
	int asc[4];
	word16 asc1;
    ch1=ch%16;ch=ch/16;
	while(ch!=0)
	{
		asc[i++]=ch1;
	    
		ch1=ch%16;ch=ch/16;
	}
	asc[i]=ch1;
	for(j=i;j>=0;j--)
		asc1=asc1*10+asc[j];
	return asc1;

}

/* test.c */
void main()
{
	int i,j=0,choose,conti=true;
	FILE *fp;
	char ch,filename[10],password[10];
	word16 in[4],out[4],c[100], ch1,get,ch2,p[100],IV[4];
	word16 userkey[8]={0x7363,0x686F,0x6F6C,0x6F66,0x636F,0x6D70,0x7574,0x6572};
	IDEAkey Z,DK;
	en_key_idea(userkey,Z);
	printf("\n****************************************************************************\n\n");
	printf("*            choose from 0(Encrypt) or 1(Decrypt) or 2(exit)                *\n");
	printf("\n****************************************************************************\n");
	scanf("%d",&choose);
	while(conti==true)
	{
	if(choose==0)
	{
		printf("please input the filename you want to Encrypt:\n");
	    scanf("%s",filename);
        printf("please input the password you want to Encrypt:(please remember your password!)\n");
        scanf("%s",password);
        for(i=0;i<4;i++)
		{
            get=password[j++];	
		    ch1=get<<8&0xff00;
            get=password[j++];
		    ch2=get&0x00ff;
		    ch1=ch1+ch2;
			IV[i]=ch1;
		}
        if((fp=fopen(filename,"r"))==NULL)
		{
		  printf("cannot open the file!\n");
		  exit(0);
		}
        for(i=0;i<4;i++)
		{
		   ch=fgetc(fp);	
		   if(ch==EOF)
		   {
			  ch1=' ';
              ch1=ch1<<8&0xff00;
		   }
		   else
		    ch1=ch<<8&0xff00;
		   if(ch==EOF)
			  ch2=' ';
		   else
		   {
			  ch=fgetc(fp);
			  if(ch==EOF)
			  {
				  ch2=' ';
				  ch2=ch2&0x00ff;
			  }
		      else
				  ch2=ch&0x00ff;
		   }
		   ch1=ch1+ch2;
		   in[i]=ch1^IV[i];
           c[m++]=in[i];
		   IV[i]=in[i];
		}
	    ch=fgetc(fp);
	    while(ch!=EOF)
		{    
		     for(i=0;i<4;i++)
			 {
		         if(ch==EOF)
				 {
			        ch1=' ';
                    ch1=ch1<<8&0xff00;
				 }
		         else
                    ch1=ch<<8&0xff00;
		         if(ch==EOF)
			        ch2=' ';
		         else
				 {
			        ch=fgetc(fp);
			        if(ch==EOF)
					{
				       ch2=' ';
				       ch2=ch2&0x00ff;
					}
		            else
				      ch2=ch&0x00ff;
				 }
		         ch1=ch1+ch2;		  
		         in[i]=ch1^IV[i];
		         ch=fgetc(fp);
			 }
             cipher_idea(in,out,Z);
		     for(i=0;i<4;i++)
			 {
                c[m++]=out[i];
		        IV[i]=out[i];
			 }
		}
	    fclose(fp);
	    printf("ciphertext:\n");
	    for(i=0;i<m;i++)
		{
		   printf("\n0x%04x\n",c[i]);
		}
       if((fp=fopen(filename,"w"))==NULL)
	   {
		  printf("cannot open the file!\n");
		  exit(0);
	   }
  	   for(i=0;i<m;i++)
	   {
	      ch1=c[i]>>8&0x00ff;
	      ch2=c[i]&0x00ff;
		  fputc(ch1,fp);
	      fputc(ch2,fp);
	   }
	   printf("Encrypt succeed!");
	   fclose(fp);
	}
	else if(choose==1)
	{
        printf("please input the filename you want to Decrypt:\n");
	    scanf("%s",filename);
        printf("please input the password you Encrypt the file:\n");
        scanf("%s",password);
		j=0;
        for(i=0;i<4;i++)
		{
            get=password[j++];	
		    ch1=get<<8&0xff00;
            get=password[j++];
		    ch2=get&0x00ff;
		    ch1=ch1+ch2;
			IV[i]=ch1;
		}
		
        if((fp=fopen(filename,"r"))==NULL)
		{
		   printf("cannot open the file!\n");
		   exit(0);
		}

	    de_key_idea(Z,DK);
	    for(i=0;i<4;i++)
		{
		   p[n++]=c[i]^IV[i];
		   IV[i]=c[i];
		   out[i]=c[n+3];
		}
	    while(n<m)
		{   
		   cipher_idea(out,in,DK);
	       for(i=0;i<4;i++)
		   {
		      p[n++]=in[i]^IV[i];
              IV[i]=c[n-1];
		      out[i]=c[n+3];
		   }
		}
	    printf("plaintext:\n");
	    for(i=0;i<n;i++)
		    printf("\n0x%04x\n",p[i]);
	    if((fp=fopen(filename,"w"))==NULL)
		{
		    printf("cannot open the file!\n");
		    exit(0);
		}
	    for(i=0;i<n;i++)
		{
	        ch1=p[i]>>8&0x00ff;
	        ch2=p[i]&0x00ff;
		    fputc(ch1,fp);
	        fputc(ch2,fp);
		}
	    fclose(fp);
	}
	else if(choose==2)
		exit(0);
	else
		printf("Illegal input!");
    printf("\n*            choose from 0(Encrypt) or 1(Decrypt) or 2(exit)   *\n\n");
	scanf("%d",&choose);
	if(choose==0||choose==1)
		conti=true;
	else
		conti=false;
	}
	
	
}

⌨️ 快捷键说明

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