1. c语言关于RSA加密

密码学嘛,最后一个实验,我还没开始写呢。本来算法就不简单
而且你连算法都不提供,估计是不太可能有人给你写了,今天下午上机,如果不幸我写出来了,就发你一份吧,怎么样,要是写不出来,就爱莫能助了

2. 求用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');

}
}

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算法

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

5. 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

6. 有没有知道怎样用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

7. 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;
}
}

8. 求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');
}
}

9. 如何用C语言来使用openssl rsa进行公钥加密,已有公钥和明文

1. 本程序使用2048位密钥对,每次加密时,原始数据的最大长度为245字节,加密后的密文长度为256字节.(采用打PADDING 的加密方式)

2. 如果所加密数据长度大于245字节,请分多次加密,后将密文按顺序存储;解密时,每次读取256字节,进行解密,将解密后的数据依次按顺序存储,即可还原原始数据.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#define OPENSSLKEY "test.key"
#define PUBLICKEY "test_pub.key"
#define BUFFSIZE 1024
char *my_encrypt(char *str, char *path_key); //加密
char *my_decrypt(char *str, char *path_key); //解密
int main(void)
{
char *source = "i like dancing !!!";
char *ptf_en, *ptf_de;
printf("source is :%s\n", source);
//1.加密
ptf_en = my_encrypt(source, PUBLICKEY);
if (ptf_en == NULL){
return 0;
}else{
printf("ptf_en is :%s\n", ptf_en);
}
//2.解密
ptf_de = my_decrypt(ptf_en, OPENSSLKEY);
if (ptf_de == NULL){
return 0;
}else{
printf("ptf_de is :%s\n", ptf_de);
}
if(ptf_en) free(ptf_en);
if(ptf_de) free(ptf_de);
return 0;
}
//加密
char *my_encrypt(char *str, char *path_key)
{
char *p_en = NULL;
RSA *p_rsa = NULL;
FILE *file = NULL;
int lenth = 0; //flen为源文件长度, rsa_len为秘钥长度
//1.打开秘钥文件
if((file = fopen(path_key, "rb")) == NULL)
{
perror("fopen() error 111111111 ");
goto End;
}
//2.从公钥中获取 加密的秘钥
if((p_rsa = PEM_read_RSA_PUBKEY(file, NULL,NULL,NULL )) == NULL)
{
ERR_print_errors_fp(stdout);
goto End;
}
lenth = strlen(str);
p_en = (char *)malloc(256);
if(!p_en)
{
perror("malloc() error 2222222222");
goto End;
}
memset(p_en, 0, 256);
//5.对内容进行加密
if(RSA_public_encrypt(lenth, (unsigned char*)str, (unsigned char*)p_en, p_rsa, RSA_PKCS1_PADDING) < 0)
{
perror("RSA_public_encrypt() error 2222222222");
goto End;
}
End:
//6.释放秘钥空间, 关闭文件
if(p_rsa) RSA_free(p_rsa);
if(file) fclose(file);
return p_en;
}
//解密
char *my_decrypt(char *str, char *path_key)
{
char *p_de = NULL;
RSA *p_rsa = NULL;
FILE *file = NULL;
//1.打开秘钥文件
file = fopen(path_key, "rb");
if(!file)
{
perror("fopen() error 22222222222");
goto End;
}
//2.从私钥中获取 解密的秘钥
if((p_rsa = PEM_read_RSAPrivateKey(file, NULL,NULL,NULL )) == NULL)
{
ERR_print_errors_fp(stdout);
goto End;
}
p_de = (char *)malloc(245);
if(!p_de)
{
perror("malloc() error ");
goto End;
}
memset(p_de, 0, 245);
//5.对内容进行加密
if(RSA_private_decrypt(256, (unsigned char*)str, (unsigned char*)p_de, p_rsa, RSA_PKCS1_PADDING) < 0)
{
perror("RSA_public_encrypt() error ");
goto End;
}
End:
//6.释放秘钥空间, 关闭文件
if(p_rsa) RSA_free(p_rsa);
if(file) fclose(file);
return p_de;
}

10. 如何用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();
}
}