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

📄 rsa.c

📁 加密算法集锦
💻 C
字号:
#include "rsa.h"

int decode_rsa(ip,file)
	char *ip;
	char *file;
{
	int	fai,p=43,q=59,ch1,i=0,j=0,edch,dech=0;
		char num[1024];//char *num;
		memset (num , 0x00 , sizeof(num));
		char *cha,ch;//*file="index.html";
		FILE *fp,*fp2;
		e=iptoint(ip);
		if ((fp2=fopen("..//files//encode.htm","w+"))==NULL){
				printf("Fail To open file : ..//files//encode.htm\n");
				exit(0);
		}
		n=p*q;
		fai=(p-1)*(q-1);
		if((fp=fopen(file,"r"))==NULL){
			printf("Fail To open giving file\n");
			exit(0);
		}
		else{
			while((ch1=fgetc(fp))!=EOF){
				edch=fmod(ch1,e,n);
				if (edch>0 && edch<10){
					fprintf(fp2,"%s","000");
					fprintf(fp2,"%d",edch);
				}
				else if (edch<100 && edch>10){
					fprintf(fp2,"%s","00");
					fprintf(fp2,"%d",edch);
				}
				else if (edch<1000 && edch>100){
					fprintf(fp2,"%s","0");
					fprintf(fp2,"%d",edch);
				}
				else if (edch<10000 && edch>1000){
					fprintf(fp2,"%d",edch);
				}
				else
					fputc('\n',fp2);
				}
			}
			close(fp);
			close(fp2);
}
int encode_rsa(ip)
	char *ip;
{
		int fai,p=43,q=59,ch1,i=0,j=0,edch,dech=0;
		char num[1024];//char *num;
		memset (num , 0x00 , sizeof(num));
		char *cha,ch,*file="..//files//index.html";
		FILE *fp,*fp2;
		e=iptoint(ip);
	if ((fp2=fopen("..//files//decode.html","w+"))==NULL){
		printf("Fail To open file : decode.html\n");
		exit(0);
	}
	n=p*q;
	fai=(p-1)*(q-1);
	while(((d*e)%fai)!=1)
		d++;
	if((fp=fopen("..//files//encode.htm","r"))==NULL){
		printf("Fail To open file : ..//files//encode.htm\n");
		exit(0);
	}
	else{
		i=0;
		while((ch=fgetc(fp))!= EOF){
			if (i==4){
				dech=fmod(dech,d,n);
				fprintf(fp2,"%c",dech);
				i=0;
				dech=0;
			}
			switch (ch){
			case '0':
				dech=dech*10+0;
				break;
			case '1':
				dech=dech*10+1;
				break;
			case '2':
				dech=dech*10+2;
				break;
			case '3':
				dech=dech*10+3;
				break;
			case '4':
				dech=dech*10+4;
				break;
			case '5':
				dech=dech*10+5;
				break;
			case '6':
				dech=dech*10+6;
				break;
			case '7':
				dech=dech*10+7;
				break;
			case '8':
				dech=dech*10+8;
				break;
			case '9':
				dech=dech*10+9;
				break;
			default:
				dech=dech*10;
			}
			num[i]=ch;
			i++;
		}
		dech=fmod(dech,d,n);
		fprintf(fp2,"%c",dech);
		close(fp);
		close(fp2);
	}//
}
void err_info()
{
	printf("Usage:code [option]...[Ip Address]...\n");
	printf("List information about the option\n");
	printf("e   bind ip address to ..//files//index.html\n");
	printf("d   decode from the encodeing file to ..//files//index2.html");
}

int iptoint(ip)
		char *ip;
{
		int i,ei=0,len;
		int prime=0;
		len=strlen(ip);
		for(i=0;i<len;i++){
				if(ip[i]>'0' && ip[i]<'9')
						ei=ei+i*((int)ip[i]);
		}
		ei=ei/1000+(ei/100)%10+(ei/10)%10+ei%10;
		while (((ei % 2)== 0) || ((ei % 3) == 0)){
				ei=ei+1;
		}
		return(ei);
}

int fmod(x, r, p)
		int x,r,p;
{
		int a,b,c=1;
		a=x;
		b=r;
		while (b!=0){
				while ((b%2)==0){
						b=b/2;
						a=(a*a)%p;
				}
				b=b-1;
				c=(a*c)%p;
		}
		return c;
}

main(int argc, char **argv)
{

		if (argc<2){
			printf("\tParamater Error!\n\t\tUsage: \n\tcommand e IP (for get code)\n\tcommand d IP (for exc code)\n");
			exit(0);
		}
		switch(*argv[1]){
		case 'e':
			decode_rsa(argv[2],argv[3]);
			break;
		case 'd':
			encode_rsa(argv[2]);
			break;
		default:
			err_info();
			break;
		}

}

⌨️ 快捷键说明

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