| 首页 | 学习 | 计算机 | 小说 | 动漫 | 论文 | 军事 | 科技 | 教育 | 哲学 | 历史 | 英语 | 笑话 | 
您现在的位置: 【书斋】 >> 计算机 >> 程序编程 >> 计算机正文 用户登录 新用户注册
RSA加密实例分析           ★★
RSA加密实例分析
现在网上有的大部分都是理论,这里将给出一个简单加密模块的全部源程序(源代码)

至于RSA的理论网上一大把,它是基于公钥加密体制的一种算法。这个实例主要是用来将某一重要文件绑定到一个IP地址上。把它拷贝下来,存为CODE.C,然后执行

GCC –O CODE CODE.C

然后用./ CODE E [IP ADDRESS] [FILENAME]对文件进行加密。或用

./CODE D [IP ADDRESS] 进行解密整个过程中产生了两个中间文件,至于什么文件,你去试一试就知道啦!

#include "stdio.h"

#include "string.h"

int e,d=1,n;

main(int argc,char *argv[])

{

int ip,fai,p=43,q=59,ch1,i=0,j=0,edch,dech=0;

char *num,*cha,ch,*file={"index.html"};

FILE *fp,*fp2;

if (argc<2)

{

printf("paramater error!");

exit(0);

}

if (argc>3)

{

file=argv[3];

}

//printf("%s,",file)

e=iptoint(argv[2]);

//printf("%s,%s",argv[1],argv[2]);

switch (*argv[1])

{

case 'e':

//printf("e=%d",e);

if ((fp2=fopen("encode.htm","w+"))==NULL)

{

printf("can't open file");

exit(0);

}

"code.c" 199L, 3571C

}

n=p*q;fai=(p-1)*(q-1);

if((fp=fopen(file,"r"))==NULL)

{

printf("can't open giving file");

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);

break;

case 'd':

// printf("d=%d",e);

if ((fp2=fopen("decode.html","w+"))==NULL)

{

if ((fp2=fopen("decode.html","w+"))==NULL)

{

printf("can't open file");

exit(0);

}

n=p*q;fai=(p-1)*(q-1);

while(((d*e)%fai)!=1)

d++;

if((fp=fopen("encode.htm","r"))==NULL)

{

printf("can't open file");

exit(0);

}

else

{ i=0;

// printf("e=%d,d=%d,n=%d,",e,d,n);

while((ch=fgetc(fp))!=EOF)

{

if (i==4)

{

//dech=((int)num[0])*1000+((int)num[1])*100+((int)num[2])*10+(int)num[3];

//printf("yuan=%d,",dech);

dech=fmod(dech,d,n);

//printf("new=%d,",dech);

fprintf(fp2,"%c",dech);

i=0;

dech=0;

//

}

switch (ch)

{

case '9':dech=dech*10+9;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 '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;

default:dech=dech*10;

}

// printf("dech=%d,",dech);

num[i]=ch;

i++;

}

dech=fmod(dech,d,n);

fprintf(fp2,"%c",dech);

}

close(fp);

close(fp2);

break;

default:

printf("Usage:code [option]...[Ip Address]...\n");

[1] [2] 下一页  


printf("List information about the option\n");

printf("e bind ip address to index.html\n");

printf("d decode from the encodeing file to index2.html");

}

}

int iptoint(ip)

char *ip;

{

int i,ei=0,len;

int prime=0;

len=strlen(ip);

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;

//printf("%d",ei);

//len=ei/2;

//while(prime==0)

//indent: Standard input:2: Error:Unexpected end of file

while (((ei % 2)== 0) || ((ei % 3) == 0))

{ ei=ei+1;

}

/* prime=1;

for (i=0;i<ei;i++)

{

if((ei%i)==0)

{

ei--;

prime=0;

break;

}

}

}*/

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;

上一页  [1] [2] 

[1]

在百度搜索:RSA加密实例分析
  • 上一个计算机:

  • 下一个计算机:
  • 相 关 文 章
  • 自解密的加密程序的制作

  • Creating Reusable Soft

  • Device Drivers(设备驱

  • Linux V2.2.X(i386体系

  • 第七篇:从程序员到系统

  • 第六篇:从程序员到系统

  • Linux动态函式库解析

  • 分析IDE硬盘驱动器读写过

  • Linux核心代码分析

  • 语法分析

  • 应用程序参数解析

  • References and arrays

  • 对BIND几个缺陷的分析

  • UNIX 萤幕导向程式的发展

  • Interrupts and Interru

  • 两中种简单排序算法分析

  • 文件加密技术一例

  • 老鼠走迷宫程序实例

  • C程序设计入门(实例讲解

  • C程序设计入门(实例讲解

  • 一个词法分析程序

  • 函数简介(无实例)

  • UNIX下C语言的图形编程c

  • PL/0语法分析器(递归子程

  • PL/0语言词法分析程序

  • 词法分析器

  • sanguo

  • Flash与Asp的结合应用实

  • flash遮罩特效实例精选-

  • flash遮罩特效实例精选-