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

📄 rsa.c

📁 C语言实现的rsa加密算法
💻 C
字号:
#include "stdlib.h"                    //random函数有问题 
#include "stdio.h"
#include "math.h"


unsigned long  n;
int e,f;
unsigned p,q;
unsigned char text_8[100];
unsigned text_16[100],secu_16[100];
void public_key_product();
void person_key_product();
int gcd(unsigned long,unsigned long);
unsigned power(unsigned,unsigned,unsigned long);
void convert_16_to_8(unsigned a[100]);
void convert_8_to_16();
void coder();
void decoder();
void get();





main()
{
    printf("    *********************************************************\n");
    printf("    *       The RSA arithmetic:                             *\n");
    printf("    * FROM: TongJi University                               *\n");
    printf("    *       The Computer Sciense of the year of 02          *\n");
    printf("    *       方文鑫(020843)                                *\n");
    printf("    *       Thank you!                                      *\n");
    printf("    *********************************************************\n");
    printf("\n");
    printf("*       Please press any key to continue.(except the key e)\n"); 

 while(getch()!='e')
 {
  printf("Please enter your text to coded:\n");
  get();
  public_key_product();
  person_key_product();
  coder();
  printf("\nThe coded is:\n%s\n",text_8);
  printf("\n\n");
  printf("Press any key to look at the code text\n");
  getch();
  printf("\n(d=%d,n=%lu)\n",f,n);                     //why d==0 and f==9775;
  decoder();
  printf("\nThe code is\n%s\n",text_8);               //输出有问题 
 }
}





void get()
{
 printf("Enter the text('e' to exit):\n");
 scanf("%s",text_8);
}





int gcd(unsigned long a,unsigned long b)      /*The gcd(),you can get gcd((p-1),(q-1))*/
{
 unsigned long c;
 if(a>=b)
 {
  c=a%b;
  if(c==0)
       return 0;
    else if (c==1)
       return 1;
  else gcd(b,c);
  }
  else gcd(b,a);
}




 void public_key_product()         //The public_key
{
 e=63;
 p=2*rand()+1+30000;
 while(!is_number(p)&&p<65536);
 {
  p+=2;
 }
 q=2*rand()+1+30000;
 while(!is_number(q)&&q<65536);
 {
  q+=2;
 }
 n=(unsigned long )p*(unsigned long)q;
 while(!gcd((unsigned long )(p-1)*(unsigned long )(q-1),e))
 e++;
 printf("(e=%d,n=%lu)\n",e,n);
 printf("p=%d,q=%d\n",p,q);
}


 /*void public_key_product()         //The public_key
{
 e=63;
 p=47;
 q=59;
 n=(unsigned long )p*(unsigned long)q;
 while(!gcd((unsigned long )(p-1)*(unsigned long )(q-1),e))
 e++;
 printf("(e=%d,n=%lu)\n",e,n);
 printf("p=%d,q=%d\n",p,q);
}*/




void person_key_product()         //The person_key
{
 int i=1;
 while((((p-1)*(q-1)*i+1)%e)&&i>0)
 i++;
 //f=((p-1)*(q-1)*i+1)/e;
 f=(p-1)*(q-1)/e;
 printf("d=%d\n",f);
}







int is_number(unsigned n)                //The is_numer,you can get n
{
 unsigned m=1,b=0,a,j=n-1,i,z;            //判断是否是质数,是质数则返回1; 
  while(!j%2)
   {
    m*=2;
    b++;
    j/=2;
   }
   m=(n-1)/m;
   a=rand()*(n-1)/32767;
   //a=n-10;
   for(j=0;j<1000;j++)
   {
    a+=2;
    if(a>2)
    {
     i=0;
     z=power(a,m,n);
     if((z!=1)&&(z!=(p-1)))          //求p的公约数 
      return 0;

      while((i<b)&&(z!=(p-1)))
      {
       if((i>0)&&(z==1))
          return 0;
          z=power(z,2,p);
          i+=1;
         }
      if(z!=(p-1))return 0;
     }
    return 1;
   }

}






unsigned power(unsigned a,unsigned b,unsigned long c)   //The power,you can get a,b,c
{
 unsigned long z=1,t;
 for(t=a;b>0;b>>=1)
 {
  if(b%2==1)z=(z*t)%c;
  t=(t*t)%c;
  }
  return z;
 }






void convert_8_to_16()
{
  int i;
 for(i=0;i<100;i++)
  text_16[i]=text_8[2*i]*256+text_8[2*i+1];
}



void convert_16_to_8(unsigned a[100])
{
  int i=0,flag=0;
  unsigned temp;
  while(i<200&&flag<100)
  {
   temp=a[flag]/256;
   text_8[i]=temp;
   text_8[i+1]=a[flag]%256;
   i+=2;
   flag++;
  }
}









void coder()                        //编码 
{
 int i;
 i=0;
 convert_8_to_16();
 while(text_16[i]!=0)
 {
 secu_16[i]=power(text_16[i],e,n);
 i++;
 }
 convert_16_to_8(secu_16);
}





void decoder()                      //解码 
{
 int i;
 i=0;
 convert_8_to_16();
 while(secu_16[i]!=0)
 {
 text_16[i]=power(secu_16[i],f,n);
 i++;
 }
 convert_16_to_8(text_16);
}


⌨️ 快捷键说明

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