做一个RSA的算法,C语言实现的~!编程高手进啊~!救命啊~!

作者&投稿:章狗 (若有异议请与网页底部的电邮联系)
急啊,做RSA用C语言实现~

bu xing

上学期交的作业,已通过老师在运行时间上的测试
#include
#include

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:
");
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:
");
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:
");
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("*******RSA密码系统*******
");
printf("Please select what do you want to do:
");
printf("1.Encrpt.
");
printf("2.Decrpt.
");
printf("3.Exit.
");
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.
");break;
}
getchar();
}
}

看你催就仓促写了个,自我感觉写的不是很好,但是能用了。数据只能是大写字母组成的字符串。
加密的时候,输入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

程序修改如下:
(主要是你的循环写的不对,输入的字符应该-'0'才能与正常的数字对应)
#include
#include
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\n",r);
s=r%c;
printf("%d\n",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",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\n",d);
printf("the
cipher
please
input
1\n");
printf("the
plain
please
input
2\n");
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\n",c);break;
case
2:printf("input
the
c:");
scanf("%d",&c);
m=candp(c,d,n);
printf("the
cipher
is
%8d\n",m);
break;
}
}

看你催就仓促写了个,自我感觉写的不是很好,但是能用了。数据只能是大写字母组成的字符串。
加密的时候,输入Y,然后输入要加密的文本(大写字母)
解密的时候,输入N,然后输入一个整数n表示密文的个数,然后n个整数表示加密时候得到的密文。
/*RSA
algorithm
*/
#include
#include
#include
#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


密码学中的rsa算法是什么
密码学中的rsa算法是什么如下:算法原理:RSA公开密钥密码体制的原理是:根据数论,寻求两个大素数比较简单,而将它们的乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥 算法描述:RSA算法的具体描述如下:(1)任意选取两个不同的大素数p和q计算乘积 (2)任意选取一个大整数e,满足整数e用...

简述rsa签名算法
RSA签名算法的具体步骤如下:1. 密钥生成:选择一个公开的大素数p和q,计算它们的积n=pq,以及欧拉函数φ(n)=(p-1)(q-1)。然后选择一个整数e,使得1<e<φ(n),且e与φ(n)互质。计算e关于φ(n)的模反元素d,即满足ed≡1(mod φ(n))。此时,(e,n)为公钥,(d,n)为私钥。2. ...

rsa是什么意思
什么是RSARSA公开密钥密码体制。所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制。RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。RSA是被研究得最广泛的公钥算法,从提出到现今的三十多年里,经历了各种...

RSA加密算法的内容是怎样的?
1) 确定密钥的宽度。2) 随机选择两个不同的素数p处q,它们的宽度是密钥宽度的二分之一。3) 计算出p和q的乘积n 。4) 在2和Φ(n)之间随机选择一个数e , e 必须和Φ(n)互素,整数e用做加密密钥(其中Φ(n)=(p-1)*(q-1))。5) 从公式ed ≡ 1 mod Φ(n)中求出解密密钥d 。6)...

在密码学中rsa的算法是什么
rsa一般指rsa加密算法。RSA加密算法是一种非对称加密算法。在公开密钥加密和电子商业中RSA被广泛使用。1977年由罗纳德·李维斯特(Ron Rivest)、阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的。RSA就是他们三人姓氏开头字母拼在一起组成的。RSA公开密钥密码体制的原理是:根据数论,...

在密码学中,对rsa算法的描述正确的是?
RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密。RSA的算法涉及三个参数,n、e1、e2。其中,n是两个大质数p、q的积,n的二进制表示时所占用的位数,就是所谓的密钥长度。e1和e2是一对相关的值,e1可以任意取,但要求e1与(p-1...

在密码学中的rsa算法是什么
加密算法E和解密算法D也都是公开的。虽然解密密钥SK是由公开密钥PK决定的,但却不能根据PK计算出SK。正是基于这种理论,1978年出现了著名的RSA算法,它通常是先生成一对RSA密钥,其中之一是保密密钥,由用户保存;另一个为公开密钥,可对外公开,甚至可在网络服务器中注册。为提高保密强度,RSA密钥至少...

什么是RSA算法,求简单解释。
RSA公钥加密算法是1977年由Ron Rivest、Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的。RSA取名来自开发他们三者的名字。RSA是目前最有影响力的公钥加密算法,它能够 抵抗到目前为止已知的所有密码攻击,已被ISO推荐为公钥数据加密标准。RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易...

rsa算法是第一个数字签名算法
RSA算法是一种非对称加密算法,由Ron Rivest、Adi Shamir和Leonard Adleman在1977年共同提出。该算法是第一个数字签名算法,其安全性基于大数分解难题。RSA算法广泛应用于电子商务、电子邮件、数字证书等领域。该算法通过生成一对密钥,即公钥和私钥,使得发送方可以使用接收方的公钥加密信息,接收方则使用自己...

一个RSA算法的加密运算,需要完整的演算过程。
rsa的安全性在于对于一个大数n,没有有效的方法能够将其分解 从而在已知n d的情况下无法获得e;同样在已知n e的情况下无法 求得d。rsa简洁幽雅,但计算速度比较慢,通常加密中并不是直接使用rsa 来对所有的信息进行加密,最常见的情况是随机产生一个对称加密的密钥,然后使用对称加密算法对信息加密,...

师宗县15388219890: 如何用C语言程序实现RSA算法? -
畅婷头孢: #include "stdafx.h" #include<math.h> #include<stdio.h> int isP(int m) { int i; for(i=2;i<m;i++) if(m % i==0)return 0; return 1; } int num(int m,int k) { int i=0; for(m=m;k>0;m++) if(isP(m)) {k--;return m; } } int main(int argc, char* argv[]) {int P,Q,E,D,i,k,...

师宗县15388219890: 求用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(...

师宗县15388219890: 如何用C语言实现RSA算法 -
畅婷头孢: #include <stdio.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <math.h> int str2num(char *str) //字符转数字 { int i=0,num=0; for(i=0;i<(int)strlen(str);i++) num=num*10+str[i]-'0'; return num; } float CarmSqrt(float x) //求平方根 系统...

师宗县15388219890: 求一段优质的C语言写的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\n",r); return r; } int fun(int x,int y) //公钥e与t的互素判断 { int=t; while(y) { t=x; x=y; y=t%y; } if(x==1) return 0; ...

师宗县15388219890: 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\n",r); s=r%c; printf("%d\n",s); return...

师宗县15388219890: 如何用c语言实现RSA加密算法
畅婷头孢: 首先你要了解rsa算法和会用c语言然后到网上搜索大数运算库因为rsa得大数运算分解要用到它

师宗县15388219890: 编一个简单的C语言小程序.....关于RSA算法的
畅婷头孢: #include &lt;math.h&gt; #include &lt;iostream&gt; using namespace std; void JiaM(double e,double n); void JieM(double d,double n); int main() { double n,e,d; cout&lt;&lt;"请输入n:"&lt;&lt;endl; cin&gt;&gt;n; cout&lt;&lt;"请输入e:"&lt;&lt;endl; ...

师宗县15388219890: 怎么编写一个C语言,实现RSA算法?
畅婷头孢: * RSA.H - header file for RSA.C */ /* Copyright (C) RSA Laboratories, a division of RSA Data Security, Inc., created 1991. All rights reserved. */ int RSAPublicEncrypt PROTO_LIST ((unsigned char *, unsigned int *, unsigned char *, unsigned int, R_...

师宗县15388219890: 求 rsa简单加密算法 C代码,急! -
畅婷头孢: #include<iostream>#include<cmath> using namespace std; void main() { int p,q; cout<<"请输入两个较大的素数:"<<endl; cin>>p>>q; cout<<"p="<<p<<",q="<<q<<endl; int n,o; n=p*q; o=(p-1)*(q-1); cout<<"n="<<n<<",o="<<o<<endl; ...

师宗县15388219890: 本人急需C语言实现RSA算法的源程序 -
畅婷头孢: **************************** 第一问输出d,e,r,我写出来了. 后面的加密,解密不难,但是我不知道怎么求那么大的幂次然后再求余,所以没写,函数原型我写出来了,你可以自己填上,如果你有什么好想法的话,请给我在百度上发消息,谢谢.输...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网