① 做一个RSA的算法,c语言实现的~!编程高手进啊~!救命啊~!
看你催就仓促写了个,自我感觉写的不是很好,但是能用了。数据只能是大写字母组成的字符串。
加密的时候,输入Y,然后输入要加密的文本(大写字母)
解密的时候,输入N,然后输入一个整数n表示密文的个数,然后n个整数表示加密时候得到的密文。
/*RSA algorithm */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MM 7081
#define KK 1789
#define PHIM 6912
#define PP 85
typedef char strtype[10000];
int len;
long nume[10000];
int change[126];
char antichange[37];
void initialize()
 { int i;
   char c;
   for (i = 11, c = 'A'; c <= 'Z'; c ++, i ++)
    { change[c] = i;
      antichange[i] = c;
    }
 }
void changetonum(strtype str)
 { int l = strlen(str), i;
   len = 0;
   memset(nume, 0, sizeof(nume));
   for (i = 0; i < l; i ++)
    { nume[len] = nume[len] * 100 + change[str[i]];
      if (i % 2 == 1) len ++;
    }
   if (i % 2 != 0) len ++;
 }
long binamod(long numb, long k)
 { if (k == 0) return 1;
   long curr = binamod (numb, k / 2);
   if (k % 2 == 0)
    return curr * curr % MM;
   else return (curr * curr) % MM * numb % MM;
 }
long encode(long numb)
 { return binamod(numb, KK);
 }
long decode(long numb)
 { return binamod(numb, PP);
 }
main()
 { strtype str;
   int i, a1, a2;
   long curr;
   initialize();
   puts("Input 'Y' if encoding, otherwise input 'N':");
   gets(str);
   if (str[0] == 'Y')
    { gets(str);
      changetonum(str);
      printf("encoded: ");
      for (i = 0; i < len; i ++)
       { if (i) putchar('-');
  printf(" %ld ", encode(nume[i]));
       }
      putchar('\n');
    }
   else
    { scanf("%d", &len);
      for (i = 0; i < len; i ++)
       { scanf("%ld", &curr);
  curr = decode(curr);
  a1 = curr / 100;
  a2 = curr % 100;
  printf("decoded: ");
  if (a1 != 0) putchar(antichange[a1]);
  if (a2 != 0) putchar(antichange[a2]);
       }
      putchar('\n');
    }
   putchar('\n');
   system("PAUSE");
   return 0;
 }
测试:
输入:
Y
FERMAT
输出:
encoded: 5192 - 2604 - 4222
输入
N
3 5192 2604 4222
输出
decoded: FERMAT
② 求用C语言编写程序RSA算法
这个是我帮个朋友写的,写的时候发现其实这个没那么复杂,不过,时间复杂度要高于那些成型了的,为人所熟知的RSA算法的其他语言实现.
#include <stdio.h>
int candp(int a,int b,int c)
{ int r=1;
 b=b+1;
 while(b!=1)
{
 r=r*a;
 r=r%c;
 b--;
}
printf("%d",r);
return r;
}
void main()
{
 int p,q,e,d,m,n,t,c,r;
 char s;
{printf("input the p:\n");<br/> scanf("%d\n",&p);<br/> printf("input the q:\n");<br/> scanf("%d%d\n",&p); <br/> n=p*q;<br/> printf("so,the n is %3d\n",n);<br/> t=(p-1)*(q-1);<br/> printf("so,the t is %3d\n",t);<br/> printf("please intput the e:\n");<br/> scanf("%d",&e);<br/> if(e<1||e>t)<br/> {printf("e is error,please input again;");<br/>  scanf("%d",&e);}
  d=1;
  while  (((e*d)%t)!=1)   d++;
  printf("then caculate out that the d is %5d",d);
  printf("if you want to konw the cipher please input 1;\n if you want to konw the plain  please input 2;\n");
  scanf("%d",&r);
  if(r==1)
{
 printf("input the m :" );/*输入要加密的明文数字*/
 scanf("%d\n",&m);
  c=candp(m,e,n);
 printf("so ,the cipher is %4d",c);}
if(r==2)
{
printf("input the c :" );/*输入要解密的密文数字*/
 scanf("%d\n",&c);
  m=candp(c,d,n);
 printf("so ,the cipher is %4d\n",m); 
 printf("do you want to use this programe:Yes or No");
 scanf("%s",&s);
}while(s=='Y');
}
}
③ 求正确的RSA加密解密算法C语言的,多谢。
RSA算法它是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。算法的名字以发明者的名字命名:RonRivest,AdiShamir和LeonardAdleman。但RSA的安全性一直未能得到理论上的证明。它经历了各种攻击,至今未被完全攻破。一、RSA算法:首先,找出三个数,p,q,r,其中p,q是两个相异的质数,r是与(p-1)(q-1)互质的数p,q,r这三个数便是privatekey接着,找出m,使得rm==1mod(p-1)(q-1)这个m一定存在,因为r与(p-1)(q-1)互质,用辗转相除法就可以得到了再来,计算n=pqm,n这两个数便是publickey编码过程是,若资料为a,将其看成是一个大整数,假设a=n的话,就将a表成s进位(s因为rm==1mod(p-1)(q-1),所以rm=k(p-1)(q-1)+1,其中k是整数因为在molo中是preserve乘法的(x==ymodzan==vmodz=>xu==yvmodz),所以,c==b^r==(a^m)^r==a^(rm)==a^(k(p-1)(q-1)+1)modpq1.如果a不是p的倍数,也不是q的倍数时,则a^(p-1)==1modp(费马小定理)=>a^(k(p-1)(q-1))==1modpa^(q-1)==1modq(费马小定理)=>a^(k(p-1)(q-1))==1modq所以p,q均能整除a^(k(p-1)(q-1))-1=>pq|a^(k(p-1)(q-1))-1即a^(k(p-1)(q-1))==1modpq=>c==a^(k(p-1)(q-1)+1)==amodpq2.如果a是p的倍数,但不是q的倍数时,则a^(q-1)==1modq(费马小定理)=>a^(k(p-1)(q-1))==1modq=>c==a^(k(p-1)(q-1)+1)==amodq=>q|c-a因p|a=>c==a^(k(p-1)(q-1)+1)==0modp=>p|c-a所以,pq|c-a=>c==amodpq3.如果a是q的倍数,但不是p的倍数时,证明同上4.如果a同时是p和q的倍数时,则pq|a=>c==a^(k(p-1)(q-1)+1)==0modpq=>pq|c-a=>c==amodpqQ.E.D.这个定理说明a经过编码为b再经过解码为c时,a==cmodn(n=pq)但我们在做编码解码时,限制0intcandp(inta,intb,intc){intr=1;b=b+1;while(b!=1){r=r*a;r=r%c;b--;}printf("%d\n",r);returnr;}voidmain(){intp,q,e,d,m,n,t,c,r;chars;printf("pleaseinputthep,q:");scanf("%d%d",&p,&q);n=p*q;printf("thenis%3d\n",n);t=(p-1)*(q-1);printf("thetis%3d\n",t);printf("pleaseinputthee:");scanf("%d",&e);if(et){printf("eiserror,pleaseinputagain:");scanf("%d",&e);}d=1;while(((e*d)%t)!=1)d++;printf("thencaculateoutthatthedis%d\n",d);printf("thecipherpleaseinput1\n");printf("theplainpleaseinput2\n");scanf("%d",&r);switch(r){case1:printf("inputthem:");/*输入要加密的明文数字*/scanf("%d",&m);c=candp(m,e,n);printf("thecipheris%d\n",c);break;case2:printf("inputthec:");/*输入要解密的密文数字*/scanf("%d",&c);m=candp(c,d,n);printf("thecipheris%d\n",m);break;}getch();}
④ 如何用C语言实现RSA算法
上学期交的作业,已通过老师在运行时间上的测试
#include <stdio.h>
#include <stdlib.h>
unsigned long prime1,prime2,ee;
unsigned long *kzojld(unsigned long p,unsigned long q) //扩展欧几里得算法求模逆
{
	unsigned long i=0,a=1,b=0,c=0,d=1,temp,mid,ni[2];
	mid=p;
	while(mid!=1)
	{
		while(p>q)
		{p=p-q;	mid=p;i++;}
        a=c*(-1)*i+a;b=d*(-1)*i+b;
		temp=a;a=c;c=temp;
		temp=b;b=d;d=temp;
		temp=p;p=q;q=temp;
		i=0;
	}
	ni[0]=c;ni[1]=d;
	return(ni);
}
unsigned long momi(unsigned long a,unsigned long b,unsigned long p)     //模幂算法
{	
	unsigned long c;
	c=1;
	if(a>p) a=a%p;
	if(b>p) b=b%(p-1);
	while(b!=0)
	{
		while(b%2==0)
			{
				b=b/2;
				a=(a*a)%p;
			}
		b=b-1;
		c=(a*c)%p;
	}
	return(c);
}
void RSAjiami()   //RSA加密函数
{
	unsigned long c1,c2;
	unsigned long m,n,c;
	n=prime1*prime2;
	system("cls");
	printf("Please input the message:\n");
	scanf("%lu",&m);getchar();
	c=momi(m,ee,n);
	printf("The cipher is:%lu",c);
	return;
}
void RSAjiemi()   //RSA解密函数
{	
	unsigned long m1,m2,e,d,*ni;
	unsigned long c,n,m,o;
	o=(prime1-1)*(prime2-1);
	n=prime1*prime2;
	system("cls");
	printf("Please input the cipher:\n");
		scanf("%lu",&c);getchar();
	ni=kzojld(ee,o);
	d=ni[0];
	m=momi(c,d,n);
	printf("The original message is:%lu",m);
	return;
}
void main()
{	unsigned long m;
	char cho;
	printf("Please input the two prime you want to use:\n");
	printf("P=");scanf("%lu",&prime1);getchar();
	printf("Q=");scanf("%lu",&prime2);getchar();
	printf("E=");scanf("%lu",&ee);getchar();
	if(prime1<prime2)
	{m=prime1;prime1=prime2;prime2=m;}
	while(1)
	{
	system("cls");
	printf("\t*******RSA密码系统*******\n");
	printf("Please select what do you want to do:\n");
	printf("1.Encrpt.\n");
	printf("2.Decrpt.\n");
	printf("3.Exit.\n");
	printf("Your choice:");
	scanf("%c",&cho);getchar();
	switch(cho)
	{	case '1':RSAjiami();break;
		case '2':RSAjiemi();break;
		case '3':exit(0);
		default:printf("Error input.\n");break;
	}
	getchar();
	}
}
⑤ RSA加密解密算法示例(C语言)
#include <stdlib.h>
  
 #include <stdio.h>
  
 #include <string.h>
  
 #include <math.h>
  
 #include <time.h>
  
 #define PRIME_MAX 200     // 生成素数范围
  
 #define EXPONENT_MAX 200  // 生成指数e范围
  
 #define Element_Max 127     // 加密单元的最大值,这里为一个char, 即1Byte
  
 char str_read[100]="hello world !";  // 待加密的原文
  
 int str_encrypt[100];                // 加密后的内容
  
 char str_decrypt[100];               // 解密出来的内容
  
 int str_read_len;                    // str_read 的长度
  
 int prime1, prime2;                  // 随机生成的两个质数
  
 int mod, eular;                      // 模数和欧拉数
  
 int pubKey, priKey;                  // 公钥指数和私钥指数
  
 // 生成随机素数,实际应用中,这两个质数越大,就越难破解。
  
 int randPrime()
  
 {
  
  int prime, prime2, i;
  
 next:
  
  prime = rand() % PRIME_MAX;     // 随机产生数
  
  if (prime <= 1) goto next;       // 不是质数,生成下一个随机数
  
  if (prime == 2 || prime == 3) return prime;
  
  prime2 = prime / 2;              // prime>=4, prime2 的平方必定大于 prime , 因此只检查小于等于prime2的数
  
  for (i = 2; i <= prime2; i++)    // 判断是否为素数
  
  {
  
   if (i * i > prime) return prime;
  
   if (prime % i == 0) goto next;  // 不是质数,生成下一个随机数
  
  }
  
 }
  
 // 欧几里德算法,判断a,b互质
  
 int gcd(int a, int b)
  
 {
  
  int temp;
  
  while (b != 0) {
  
   temp = b;
  
   b = a % b;
  
   a = temp;
  
  }
  
  return a;
  
 }
  
 //生成公钥指数,条件是 1< e < 欧拉数,且与欧拉数互质。
  
 int randExponent()
  
 {
  
  int e;
  
  while (1)
  
  {
  
   e = rand() % eular; if (e < EXPONENT_MAX) break;
  
  }
  
  while (1)
  
  {
  
   if (gcd(e, eular) == 1) return e; e = (e + 1) % eular; if (e == 0 || e > EXPONENT_MAX) e = 2;
  
  }
  
 }
  
 //生成私钥指数
  
 int inverse()
  
 {
  
  int d, x;
  
  while (1)
  
  {
  
   d = rand() % eular;
  
   x = pubKey * d % eular;
  
   if (x == 1)
  
   {
  
    return d;
  
   }
  
  }
  
 }
  
 //加密函数
  
 void jiami()           
  
 {
  
  str_read_len = strlen(str_read);      //从参数表示的地址往后找,找到第一个'\0',即串尾。计算'\0'至首地址的“距离”,即隔了几个字符,从而得出长度。
  
  printf("密文是:");
  
  for (int i = 0; i < str_read_len; i++)
  
  {
  
   int C = 1; int a = str_read[i], b = a % mod;
  
   for (int j = 0; j < pubKey; j++) //实现加密
  
   {
  
    C = (C*b) % mod;
  
   }
  
   str_encrypt[i] = C;
  
   printf("%d ", str_encrypt[i]);
  
  }
  
  printf("\n");
  
 }
  
 //解密函数
  
 void jiemi()         
  
 {
  
  int i=0;  for (i = 0; i < str_read_len; i++)
  
  {
  
   int C = 1; int a = str_encrypt[i], b=a%mod;
  
   for (int j = 0; j < priKey; j++)
  
   {
  
    C = (C * b) % mod;
  
   }
  
   str_decrypt[i] = C;
  
  }
  
  str_decrypt[i] = '\0'; printf("解密文是:%s \n", str_decrypt);
  
 }
  
 int main()
  
 {
  
  srand(time(NULL));
  
  while (1)
  
  {
  
   prime1 = randPrime(); prime2 = randPrime(); printf("随机产生两个素数:prime1 = %d , prime2 = %d ", prime1, prime2);
  
   mod = prime1 * prime2; printf("模数:mod = prime1 * prime2 = %d \n", mod); if (mod > Element_Max) break; // 模数要大于每个加密单元的值
  
  }
  
  eular = (prime1 - 1) * (prime2 - 1);  printf("欧拉数:eular=(prime1-1)*(prime2-1) = %d \n", eular);
  
  pubKey = randExponent(); printf("公钥指数:pubKey = %d\n", pubKey);
  
  priKey = inverse(); printf("私钥指数:priKey = %d\n私钥为 (%d, %d)\n", priKey, priKey, mod);
  
  jiami(); jiemi();
  
  return 0;
  
 }
⑥ 如何用C++实现RSA算法
基础
RSA算法非常简单,概述如下:
找两素数p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一个数e,要求满足eperl -Mbigint -e "print 465**63%2773"
244
即用e对c解密后获得m=244 , 该值和原始信息M相等.
字符串加密
把上面的过程集成一下我们就能实现一个对字符串加密解密的示例了.
每次取字符串中的一个字符的ascii值作为M进行计算,其输出为加密后16进制
的数的字符串形式,按3字节表示,如01F
代码如下:
#!/usr/bin/perl -w
#RSA 计算过程学习程序编写的测试程序
#watercloud 2003-8-12
#
use strict;
use Math::BigInt;
my %RSA_CORE = (n=>2773,e=>63,d=>847); #p=47,q=59
my $N=new Math::BigInt($RSA_CORE{n});
⑦ rsa算法c语言实现
程序修改如下:
(主要是你的循环写的不对,输入的字符应该-'0'才能与正常的数字对应)
#include<stdio.h>
#include<math.h>
int
candp(int
a,int
b,int
c)
{int
r=1;
int
s;
int
i=1;
for(i=1;i<=b;i++)r=r*a;
printf("%d\
",r);
s=r%c;
printf("%d\
",s);
return
s;}
void
main()
{
int
p,q,e,d,m,n,t,c,r
;
char
s;
printf("please
input
the
p,q:");
scanf("%d%d",&p,&q);
n=p*q;
t=(p-1)*(q-1);
printf("the
n
is
%12d\
",n);
printf("please
input
the
e:");
scanf("%d",&e);
while(e<1||e>n)
//此处修改为while循环
{
printf("e
is
error,please
input
again:");
scanf("%d",&e);
}
d=1;
while(((e*d)%t)!=1)
d++;
printf("then
caculate
out
that
the
d
is
%d\
",d);
printf("the
cipher
please
input
1\
");
printf("the
plain
please
input
2\
");
scanf("%c",&s);
while((s-'0')!=1&&(s-'0')!=2)
//消除后面的getchar()
此处增加while循环注意括号内的字符
{scanf("%c",&s);}
switch(s-'0')
{
case
1:printf("intput
the
m:");
scanf("%d",&m);
c=candp(m,e,n);
printf("the
plain
is
%d\
",c);break;
case
2:printf("input
the
c:");
scanf("%d",&c);
m=candp(c,d,n);
printf("the
cipher
is
%8d\
",m);
break;
}
}
