当前位置:首页 > 代码 > 正文

mfcrsa源代码(mfc编写)

admin 发布:2022-12-19 19:39 150


今天给各位分享mfcrsa源代码的知识,其中也会对mfc编写进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求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(e1||et)

{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');

}

}

用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;

}

求RSA加密解密算法,c++源代码

//下面程序由520huiqin编写,已在VC++ 6.0下编译通过

#include iostream.h

#include math.h

#include stdio.h

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 (m = 1) {

return false;

}

else if (m == 2) {

return true;

}

else {

for(int i=2; i=sqrt(m); i++) {

if((m % i) == 0) {

return false;

break;

}

}

return true;

}

}

/* 将十进制数据转化为二进制数组 */

void switch_to_bit(Elemtype b, Elemtype bin[32]) {

int n = 0;

while( b 0) {

bin[n] = b % 2;

n++;

b /= 2;

}

}

/* 候选菜单,主界面 */

void Init() {

cout"*********************************************"endl;

cout"*** Welcome to use RSA encoder ***"endl;

cout"*** a.about ***"endl;

cout"*** e.encrypt ***"endl;

cout"*** d.decrypt ***"endl;

cout"*** s.setkey ***"endl;

cout"*** q.quit ***"endl;

cout"**********************************by*Terry***"endl;

cout"press a key:"endl;

}

/* 将两个数排序,大的在前面*/

void order(Elemtype in1, Elemtype in2) {

Elemtype a = ( in1 in2 ? in1 : in2);

Elemtype b = ( in1 in2 ? in1 : in2);

in1 = a;

in2 = b;

}

/* 求最大公约数 */

Elemtype gcd(Elemtype a, Elemtype b) {

order(a,b);

int r;

if(b == 0) {

return a;

}

else {

while(true) {

r = a % b;

a = b;

b = r;

if (b == 0) {

return a;

break;

}

}

}

}

/* 用扩展的欧几里得算法求乘法逆元 */

Elemtype extend_euclid(Elemtype m, Elemtype bin) {

order(m,bin);

Elemtype a[3],b[3],t[3];

a[0] = 1, a[1] = 0, a[2] = m;

b[0] = 0, b[1] = 1, b[2] = bin;

if (b[2] == 0) {

return a[2] = gcd(m, bin);

}

if (b[2] ==1) {

return b[2] = gcd(m, bin);

}

while(true) {

if (b[2] ==1) {

return b[1];

break;

}

int q = a[2] / b[2];

for(int i=0; i3; i++) {

t[i] = a[i] - q * b[i];

a[i] = b[i];

b[i] = t[i];

}

}

}

/* 快速模幂算法 */

Elemtype modular_multiplication(Elemtype a, Elemtype b, Elemtype n) {

Elemtype f = 1;

Elemtype bin[32];

switch_to_bit(b,bin);

for(int i=31; i=0; i--) {

f = (f * f) % n;

if(bin[i] == 1) {

f = (f * a) % n;

}

}

return f;

}

/* 产生密钥 */

void produce_key() {

cout"input two primes p and q:";

cinpq;

while (!(test_prime(p)test_prime(q))){

cout"wrong input,please make sure two number are both primes!"endl;

cout"input two primes p and q:";

cinpq;

};

pr.n = p * q;

pu.n = p * q;

fn = (p - 1) * (q - 1);

cout"fn = "fnendl;

cout"input e :";

cine;

while((gcd(fn,e)!=1)) {

cout"e is error,try again!";

cout"input e :";

cine;

}

pr.d = (extend_euclid(fn,e) + fn) % fn;

pu.e = e;

flag = 1;

cout"PR.d: "pr.d" PR.n: "pr.nendl;

cout"PU.e: "pu.e" PU.n: "pu.nendl;

}

/* 加密 */

void encrypt() {

if(flag == 0) {

cout"setkey first:"endl;

produce_key();

}

cout"input m:";

cinm;

c = modular_multiplication(m,pu.e,pu.n);

cout"c is:"cendl;

}

/* 解密 */

void decrypt() {

if(flag == 0) {

cout"setkey first:"endl;

produce_key();

}

cout"input c:";

cinc;

m = modular_multiplication(c,pr.d,pr.n);

cout"m is:"mendl;

}

/* 版权信息 */

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 18679376@qq.com ! ***"endl;

cout"*** Computer of science and engineering ***"endl;

cout"*** XiDian University 2010-4-29 ***"endl;

cout"*********************************************"endl;

coutendlendl;

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[i].ch) {

if(Messagemap[i].ch == d) {

Messagemap[i].handler();

break;

}

i++;

}

}

}

//欢迎分享,盗窃可耻

mfcrsa源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于mfc编写、mfcrsa源代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/20976.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载