当前位置:首页 » 密码管理 » rsa加密c

rsa加密c

发布时间: 2022-08-06 12:50:52

㈠ 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
*/

㈡ 求正确的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();
}
}

㈣ 如何用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;
}

㈤ windows c语言加密rsa公钥加密有哪些

RSA算法它是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。算法的名字以发明者的名字命名:Ron Rivest, Adi Shamir 和Leonard
Adleman。但RSA的安全性一直未能得到理论上的证明。它经历了各种攻击,至今未被完全攻破。

一、RSA算法 :

首先, 找出三个数, p, q, r,
其中 p, q 是两个相异的质数, r 是与 (p-1)(q-1) 互质的数
p, q, r 这三个数便是 private key

接着, 找出 m, 使得 rm == 1 mod (p-1)(q-1)
这个 m 一定存在, 因为 r 与 (p-1)(q-1) 互质, 用辗转相除法就可以得到了
再来, 计算 n = pq
m, n 这两个数便是 public key

编码过程是, 若资料为 a, 将其看成是一个大整数, 假设 a < n
如果 a >= n 的话, 就将 a 表成 s 进位 (s <= n, 通常取 s = 2^t),
则每一位数均小于 n, 然后分段编码
接下来, 计算 b == a^m mod n, (0 <= b < n),
b 就是编码后的资料

解码的过程是, 计算 c == b^r mod pq (0 <= c < pq),
于是乎, 解码完毕 等会会证明 c 和 a 其实是相等的 :)

如果第三者进行窃听时, 他会得到几个数: m, n(=pq), b
他如果要解码的话, 必须想办法得到 r
所以, 他必须先对 n 作质因数分解
要防止他分解, 最有效的方法是找两个非常的大质数 p, q,
使第三者作因数分解时发生困难

㈥ 求助c rsa 分段加密

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 <iostream>
using namespace std;

template <class HugeInt>
HugeInt Power( const HugeInt & x, const HugeInt & n, // 求x^n mod p
const HugeInt & p )
{
if( n == 0 )
return 1;

HugeInt tmp = Power( ( x * x ) % p, n / 2, p );

if( n % 2 != 0 )
tmp = ( tmp * x ) % p;

return tmp;
}

template <class HugeInt>
void fullGcd( const HugeInt & a, const HugeInt & b, //
HugeInt & x, HugeInt & y )
{
HugeInt x1, y1;

if( b == 0 )
{
x = 1;
y = 0;
}
else
{
fullGcd( b, a % b, x1, y1 );
x = y1;
y = x1 - ( a / b ) * y1;
}
}

template <class HugeInt>
HugeInt inverse( const HugeInt & p, const HugeInt & q, // 求d
const HugeInt & e )
{
int fyn = ( 1 - p ) * ( 1 - q );
HugeInt x, y;

fullGcd( fyn, e, x, y );
return x > 0 ? x : x + e;
}

int main( )
{
cout << "Please input the plaintext: " << endl;
int m;
cin >> m;
cout << "Please input p,q and e: " << endl;
int p, q, e;
cin >> p >> q >> e;
int n = p * q;
int d = inverse( p, q, e );
int C = Power( m, e, n );
cout << "The ciphertext is: " << C << endl;
cout << "\n\nPlease input the ciphertext: " << endl;
cin >> C;
cout << "\n\nPlease input p, q and d: " << endl;
cin >> p >> q >> d;
n = p * q;
m = Power( C, d, n );
cout <<"The plaintext is: " << m << endl << endl;

system( "pause" );
return 0;
}

㈧ 如何用C语言实现RSA算法

RSA算法它是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。算法的名字以发明者的名字
命名:Ron Rivest, Adi Shamir 和Leonard
Adleman。但RSA的安全性一直未能得到理论上的证明。它经历了各种攻击,至今未被完全攻破。

一、RSA算法 :

首先, 找出三个数, p, q, r,
其中 p, q 是两个相异的质数, r 是与 (p-1)(q-1) 互质的数
p, q, r 这三个数便是 private key

接着, 找出 m, 使得 rm == 1 mod (p-1)(q-1)
这个 m 一定存在, 因为 r 与 (p-1)(q-1) 互质, 用辗转相除法就可以得到了
再来, 计算 n = pq
m, n 这两个数便是 public key

编码过程是, 若资料为 a, 将其看成是一个大整数, 假设 a < n
如果 a >= n 的话, 就将 a 表成 s 进位 (s <= n, 通常取 s = 2^t),
则每一位数均小于 n, 然后分段编码
接下来, 计算 b == a^m mod n, (0 <= b < n),
b 就是编码后的资料

解码的过程是, 计算 c == b^r mod pq (0 <= c < pq),
于是乎, 解码完毕 等会会证明 c 和 a 其实是相等的 :)

如果第三者进行窃听时, 他会得到几个数: m, n(=pq), b
他如果要解码的话, 必须想办法得到 r
所以, 他必须先对 n 作质因数分解
要防止他分解, 最有效的方法是找两个非常的大质数 p, q,
使第三者作因数分解时发生困难
<定理>
若 p, q 是相异质数, rm == 1 mod (p-1)(q-1),
a 是任意一个正整数, b == a^m mod pq, c == b^r mod pq,
则 c == a mod pq

证明的过程, 会用到费马小定理, 叙述如下:
m 是任一质数, n 是任一整数, 则 n^m == n mod m
(换另一句话说, 如果 n 和 m 互质, 则 n^(m-1) == 1 mod m)
运用一些基本的群论的知识, 就可以很容易地证出费马小定理的

<证明>
因为 rm == 1 mod (p-1)(q-1), 所以 rm = k(p-1)(q-1) + 1, 其中 k 是整数
因为在 molo 中是 preserve 乘法的
(x == y mod z and u == v mod z => xu == yv mod z),
所以, c == b^r == (a^m)^r == a^(rm) == a^(k(p-1)(q-1)+1) mod pq

1. 如果 a 不是 p 的倍数, 也不是 q 的倍数时,
则 a^(p-1) == 1 mod p (费马小定理) => a^(k(p-1)(q-1)) == 1 mod p
a^(q-1) == 1 mod q (费马小定理) => a^(k(p-1)(q-1)) == 1 mod q
所以 p, q 均能整除 a^(k(p-1)(q-1)) - 1 => pq | a^(k(p-1)(q-1)) - 1
即 a^(k(p-1)(q-1)) == 1 mod pq
=> c == a^(k(p-1)(q-1)+1) == a mod pq

2. 如果 a 是 p 的倍数, 但不是 q 的倍数时,
则 a^(q-1) == 1 mod q (费马小定理)
=> a^(k(p-1)(q-1)) == 1 mod q
=> c == a^(k(p-1)(q-1)+1) == a mod q
=> q | c - a
因 p | a
=> c == a^(k(p-1)(q-1)+1) == 0 mod p
=> p | c - a
所以, pq | c - a => c == a mod pq

3. 如果 a 是 q 的倍数, 但不是 p 的倍数时, 证明同上

4. 如果 a 同时是 p 和 q 的倍数时,
则 pq | a
=> c == a^(k(p-1)(q-1)+1) == 0 mod pq
=> pq | c - a
=> c == a mod pq
Q.E.D.

这个定理说明 a 经过编码为 b 再经过解码为 c 时, a == c mod n (n = pq)
但我们在做编码解码时, 限制 0 <= a < n, 0 <= c < n,
所以这就是说 a 等于 c, 所以这个过程确实能做到编码解码的功能

二、RSA 的安全性

RSA的安全性依赖于大数分解,但是否等同于大数分解一直未能得到理论上的证明,因为没有证明破解
RSA就一定需要作大数分解。假设存在一种无须分解大数的算法,那它肯定可以修改成为大数分解算法。目前, RSA
的一些变种算法已被证明等价于大数分解。不管怎样,分解n是最显然的攻击方法。现在,人们已能分解多个十进制位的大素数。因此,模数n
必须选大一些,因具体适用情况而定。

三、RSA的速度

由于进行的都是大数计算,使得RSA最快的情况也比DES慢上倍,无论是软件还是硬件实现。速度一直是RSA的缺陷。一般来说只用于少量数据加密。

四、RSA的选择密文攻击

RSA在选择密文攻击面前很脆弱。一般攻击者是将某一信息作一下伪装( Blind),让拥有私钥的实体签署。然后,经过计算就可得到它所想要的信息。实际上,攻击利用的都是同一个弱点,即存在这样一个事实:乘幂保留了输入的乘法结构:

( XM )^d = X^d *M^d mod n

前面已经提到,这个固有的问题来自于公钥密码系统的最有用的特征--每个人都能使用公钥。但从算法上无法解决这一问题,主要措施有两条:一条是采用好的公
钥协议,保证工作过程中实体不对其他实体任意产生的信息解密,不对自己一无所知的信息签名;另一条是决不对陌生人送来的随机文档签名,签名时首先使用
One-Way HashFunction 对文档作HASH处理,或同时使用不同的签名算法。在中提到了几种不同类型的攻击方法。

五、RSA的公共模数攻击

若系统中共有一个模数,只是不同的人拥有不同的e和d,系统将是危险的。最普遍的情况是同一信息用不同的公钥加密,这些公钥共模而且互质,那末该信息无需私钥就可得到恢复。设P为信息明文,两个加密密钥为e1和e2,公共模数是n,则:

C1 = P^e1 mod n

C2 = P^e2 mod n

密码分析者知道n、e1、e2、C1和C2,就能得到P。

因为e1和e2互质,故用Euclidean算法能找到r和s,满足:

r * e1 + s * e2 = 1

假设r为负数,需再用Euclidean算法计算C1^(-1),则

( C1^(-1) )^(-r) * C2^s = P mod n

另外,还有其它几种利用公共模数攻击的方法。总之,如果知道给定模数的一对e和d,一是有利于攻击者分解模数,一是有利于攻击者计算出其它成对的e’和d’,而无需分解模数。解决办法只有一个,那就是不要共享模数n。

RSA的小指数攻击。 有一种提高 RSA速度的建议是使公钥e取较小的值,这样会使加密变得易于实现,速度有
所提高。但这样作是不安全的,对付办法就是e和d都取较大的值。

RSA算法是
第一个能同时用于加密和数字签名的算法,也易于理解和操作。RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人
们接受,普遍认为是目前最优秀的公钥方案之一。RSA的安全性依赖于大数的因子分解,但并没有从理论上证明破译RSA的难度与大数分解难度等价。即RSA
的重大缺陷是无法从理论上把握它的保密性能
如何,而且密码学界多数人士倾向于因子分解不是NPC问题。
RSA的缺点主要有:A)产生密钥很麻烦,受到素数产生技术的限制,因而难以做到一次一密。B)分组长度太大,为保证安全性,n 至少也要 600
bits
以上,使运算代价很高,尤其是速度较慢,较对称密码算法慢几个数量级;且随着大数分解技术的发展,这个长度还在增加,不利于数据格式的标准化。目
前,SET( Secure Electronic Transaction )协议中要求CA采用比特长的密钥,其他实体使用比特的密钥。

C语言实现

#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;
}
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;
printf("the n is %3d\n",n);
t=(p-1)*(q-1);
printf("the t is %3d\n",t);
printf("please input the e: ");
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 %d\n",d);
printf("the cipher please input 1\n");
printf("the plain please input 2\n");
scanf("%d",&r);
switch(r)
{
case 1: printf("input the m: "); /*输入要加密的明文数字*/
scanf("%d",&m);
c=candp(m,e,n);
printf("the cipher is %d\n",c);break;
case 2: printf("input the c: "); /*输入要解密的密文数字*/
scanf("%d",&c);
m=candp(c,d,n);
printf("the cipher is %d\n",m);break;
}
getch();
}

热点内容
手机程序缓存文件怎么找 发布:2024-05-15 16:48:16 浏览:256
linux系统debian 发布:2024-05-15 16:24:40 浏览:617
安卓手机怎么设置音效好 发布:2024-05-15 16:20:22 浏览:100
我的世界服务器tbtp 发布:2024-05-15 16:19:18 浏览:34
服务器如何改成工作站 发布:2024-05-15 16:19:09 浏览:385
史丰收速算法有意义吗 发布:2024-05-15 16:13:38 浏览:877
借贷宝连接不到服务器是怎么回事 发布:2024-05-15 16:08:21 浏览:328
解压即玩的游戏可以分批解压吗 发布:2024-05-15 16:08:05 浏览:83
手机存储芯片改优盘 发布:2024-05-15 16:03:21 浏览:882
可编程门禁锁 发布:2024-05-15 16:00:12 浏览:37