给出p、q、e、M,求公钥,私钥,并且利用RSA算法加密和解密?

[复制链接]
查看11 | 回复2 | 2010-4-15 13:41:16 | 显示全部楼层 |阅读模式
设p=17 q=11,这两个数明显是一个素数,根据这2个素数,选择一个e值,e=7,关于e怎么选,一会儿在下面的算法里你就知道。假设明文M=88,则开始如下算法:1. 计算n=pq=1872. 计算φ(n)=(p-1)(q-1)=1603. 选择e,使用它与160互素且小于160,因此选e=74. 确定d,使de除160的余数为1,取d=23。这样得到公钥PU={e,n}={7,187} 私钥={d,n}={23,187}加密:密文C=M^e mod n=88^7 mod 187=11解密:明文M=C^d mod n=88
回复

使用道具 举报

千问 | 2010-4-15 13:41:16 | 显示全部楼层
假定用户A要发送消息m给用户B,1)用户B要产生两个素数p和q;2)用户B计算n=pq和φ(n)=(p-1)(q-1);3)用户B选着一个数e(0#include typedef int Elemtype;Elemtype p,q,e;Elemtype fn;Elemtype m,c;int flag = 0;typedef void (*Msghandler) (void);struct MsgMap { char ch; Msghandler handler;};/* 公钥 */struct PU { Elemtype e; Elemtype n;} pu;/* 私钥 */struct PR { Elemtype d; Elemtype n;} pr;/* 判定一个数是否为素数 */bool test_prime(Elemtype m) { if (m0) {bin[n] = b % 2;n++;b /= 2; } }/* 候选菜单,主界面 */void Init() { cout in2 ? in1 : in2); Elemtype b = ( in1 =0; i--) {f = (f * f) % n;if(bin == 1) { f = (f * a) % n;} } return f;}/* 产生密钥 */void produce_key() { cout>p>>q; while (!(test_prime(p)&&test_prime(q))){cout>p>>q; }; pr.n = p * q; pu.n = p * q; fn = (p - 1) * (q - 1); cout>e; while((gcd(fn,e)!=1)) {cout>e; } pr.d = (extend_euclid(fn,e) + fn) % fn; pu.e = e; flag = 1; cout>m; c = modular_multiplication(m,pu.e,pu.n); cout>c; m = modular_multiplication(c,pr.d,pr.n); cout<<"m is:"<<m<<endl;}/* 版权信息 */void about() { cout<<"*********************************************"<<endl; cout<<"***
by Terry
***"<<endl; cout<<"*** copyright 2010,All rights reserved by ***"<<endl; cout<<"*** Terry,technology supported by weizuo !***"<<endl; cout<<"*** If you have any question, please mail ***"<<endl; cout<<"*** to [email protected] !
***"<<endl; cout<<"*** Computer of science and engineering ***"<<endl; cout<<"*** XiDian University
2010-4-29
***"<<endl; cout<<"*********************************************"<<endl; cout<<endl<<endl; Init();}/* 消息映射 */MsgMap Messagemap[] = { {'a',about}, {'s',produce_key}, {'d',decrypt}, {'e',encrypt}, {'q',NULL}};/* 主函数,提供循环 */void main() { Init(); char d; while((d = getchar())!='q') {int i = 0;while(Messagemap.ch) { if(Messagemap.ch == d) {
Messagemap.handler();
break; } i++;} }}
回复

使用道具 举报

千问 | 2010-4-15 13:41:16 | 显示全部楼层
ziji
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行