rsa算法c语言
1. 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
*/
2. 如何用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();
}
}
3. 如何用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,fn,c=0,j=0,t=1,f1=1,l=2;
int a[10];
long N0,N1;
long PT,CT,N;
printf("请输入第一个数:");
scanf("%d",&P);
P=num(P,1);
printf("请输入比第一次大的数:");
scanf("%d",&Q);
Q=num(Q,1);
N=P*Q;
N1=(P-1)*(Q-1);
N0=N1;
while(N1>=3)
{
while(N1%l!=0)
{
l++;
}
a[j++]=l;
N1=N1/l;
}
printf("请输入一个奇数E,若E不合适,系统将会找一个比E大的合适值:");
scanf("%d",&E);
for(i=E;t>0;i=i+2)
{
for(k=0;k<j+1;k++)
{
(E%a[k-1]==0) break;
else if(k==j)
{
t--;
}
E=i;
}
}
for(k=1; ;k++)
{
if((N0*k+1)%E==0)
{
D=(N0*k+1)/E;
if((D*E)%N0==1)
break;
}
}
printf("请输入明文:");
scanf("%ld",&PT);
for(i=1;i<=E;i++)
{
fn=(f1*PT)%N;
f1=fn;
CT=fn;
}
f1=1;
for(i=1;i<=D;i++)
{
fn=(f1*CT)%N;
f1=fn;
PT=fn;
}
printf("P=%d,Q=%d\n",P,Q);
for(k=0;k<j;k++)
printf("%d ",a[k]);
printf("\n");
printf("E=%d,D=%d,N=%ld\n",E,D,N);
printf("密码是:%ld\n",CT);
printf("明文是:%ld\n",PT);
return 0;
}
4. 求用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');
}
}
5. 如何用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) //求平方根 系统的太慢,用了别人的
{
union
{
int intPart;
float floatPart;
}convertor;
union
{
int intPart;
float floatPart;
}convertor2;
convertor.floatPart = x;
convertor2.floatPart = x;
convertor.intPart = 0x1FBCF800 + (convertor.intPart >> 1);
convertor2.intPart = 0x5f3759df - (convertor2.intPart >> 1);
return 0.5f*(convertor.floatPart + (x * convertor2.floatPart));
}//可以不用,用sqrt()也可以
int isPrime(int n) //判断是否为素数
{
int i=0,k=2;
k=(int)CarmSqrt(n);
for(i=2;i<=k;i++)
{
if(n%i==0)
break;
}
if(i>k)
return 1;
else
return 0;
}
int rnd(int max) //生成随机数 2~max 用来生成e,
{ //取系统时间做随机数种子
int range,n;
int min=2,flag=0;
time_t t;
double j;
range=max-min;
t=time(NULL);
srand((unsigned)t);
n=rand();
j=((double)n/(double)RAND_MAX);
n=(int)(j*(double)range);
n+=min;
return n;
}
int co_prime(int a ,int b) // 求互质
{
int c;
do
{
if(b==1)
return 1;
c=a%b;
a=b;
b=c;
}while(c!=0);
return 0;
}
void get_d_e(int p,int q)
{
int r,t,e,d,i=2,k=0;
if(isPrime(p)!=1||isPrime(q)!=1)
{
printf("Invaild Parameters\nshould be PRIME!\n");
printf("Usage:RSA Prime1 Prime2\n");
exit(0);
}
r=p*q;
t=(p-1)*(q-1);
e=rnd(t)/10;
while(co_prime(t,e)!=1)
{
e=e+1;
}
for(d=2;d<t;d++)
{
if((e*d)%t==1)
break;
}
printf("d=%d e=%d r=%d\n",d,e,r);
}
void en(int n,int e,int r)
{
//加密
}
void de(int c,int d,int r)
{
//解密
}
void main(int argc,char* argv[])
{
int n1,n2;
if(argc!=3&&argc!=5)
{
printf("Invaild Parameters!\n");
printf("Usage: \nRSA -e Express e r\n");
printf("RSA -d Ciphertext d r\n");
printf("RSA Prime1 Prime2\n");//错误输出
exit(0);
}
else
if(argv[1][0]!='-')
{
n1=str2num(argv[1]);
n2=str2num(argv[2]);
get_d_e(n1,n2);
exit(0);
}
else
if(argv[1][1]=='e') //加密
{
n1=str2num(argv[3]);
n2=str2num(argv[4]);
en(str2num(argv[2]),n1,n2);
exit(0);
}
if(argv[1][1]=='d') //解密
{
n1=str2num(argv[3]);
n2=str2num(argv[4]);
en(str2num(argv[2]),n1,n2);
exit(0);
}
else{
printf("Invaild Parameters!\n");
printf("Usage: \nRSA -e Express e r\n");
printf("RSA -d Ciphertext d r\n");
printf("RSA Prime1 Prime2\n");
exit(0);
}
}
6. 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;
}
}
7. 求RSA算法的源代码(c语言)
这个是我帮个朋友写的,写的时候发现其实这个没那么复杂,不过,时间复杂度要高于那些成型了的,为人所熟知的rsa算法的其他语言实现.
#include
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");
scanf("%d\n",&p);
printf("input
the
q:\n");
scanf("%d%d\n",&p);
n=p*q;
printf("so,the
n
is
%3d\n",n);
t=(p-1)*(q-1);
printf("so,the
t
is
%3d\n",t);
printf("please
intput
the
e:\n");
scanf("%d",&e);
if(e<1||e>t)
{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
%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');
}
}
8. 有没有知道怎样用C语言来写RSA加密算法的代码的谢谢
#region RSA的加密函数 //##############################################################################
//RSA 方式加密
//说明KEY必须是XML的行式,返回的是字符串
//在有一点需要说明!!该加密方式有 长度 限制的!!
//##############################################################################
//RSA的加密函数 string public string RSAEncrypt(string xmlPublicKey, string m_strEncryptString)
{
byte[] PlainTextBArray; byte[] CypherTextBArray;
string Result;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
} //RSA的加密函数 byte[]
public string RSAEncrypt(string xmlPublicKey, byte[] EncryptString)
{
byte[] CypherTextBArray; string Result;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
CypherTextBArray = rsa.Encrypt(EncryptString, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
} #endregion
#region RSA的解密函数 //RSA的解密函数 string
public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)
{
byte[] PlainTextBArray;
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
PlainTextBArray = Convert.FromBase64String(m_strDecryptString);
DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
//RSA的解密函数 byte public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)
{
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
DypherTextBArray = rsa.Decrypt(DecryptString, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
} #endregion
#endregion
9. c语言程序分析 RSA算法
自己加cout<<输出来复看制赛
关键是理解他的转换过程
比如下面一个binamod函数的输出
N
3 5192 2604 4222
curr==5192 k==85
curr==5192 k==42
curr==5192 k==21
curr==5192 k==10
curr==5192 k==5
curr==5192 k==2
curr==5192 k==1
curr==5192 k==0
curr 1 k 1
else k%2!=0 curr=5192
curr 5192 k 2
if k%2==0 curr=6578
curr 6578 k 5
else k%2!=0 curr=5175
curr 5175 k 10
if k%2==0 curr=283
curr 283 k 21
else k%2!=0 curr=4525
curr 4525 k 42
if k%2==0 curr=4454
curr 4454 k 85
else k%2!=0 curr=1615