Current location - Quotes Website - Signature design - C++ Implementation of RSA Encryption and Decryption Algorithm
C++ Implementation of RSA Encryption and Decryption Algorithm
# include & ltiostream & gt

Use namespace std

Template & lt Class Hewitt & gt

huge int Power(const huge int & amp; X, const HugeInt & ampn,//Find X n mod p.

const HugeInt & ampp)

{

If (n == 0)

Returns1;

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

if( n % 2! = 0 )

tmp =(tmp * x)% p;

Return to tmp

}

Template & lt Class Hewitt & gt

void full gcd(const huge int & amp; const HugeInt & ampb,//

Hugent & ampx, HugeInt & ampy)

{

HugeInt x 1,y 1;

If (b == 0)

{

x = 1;

y = 0;

}

other

{

fullGcd( b,a % b,x 1,y 1);

x = y 1;

y = x 1-(a/b)* y 1;

}

}

Template & lt Class Hewitt & gt

Inverse HugeInt (const HugeInt & ampp, const HugeInt & ampq,//find d.

const HugeInt & ampe)

{

int fyn =( 1-p)*( 1-q);

HugeInt x,y;

fullGcd( fyn,e,x,y);

return x & gt0 ? x:x+e;

}

int main()

{

Cout & lt& lt Please enter clear text: "<& ltendl"

int m;

CIN & gt; & gtm;

Cout & lt& lt Please enter p, q and e: "<; & ltendl

int p,q,e;

CIN & gt; & gtp & gt& gtq & gt& gte;

int n = p * q;

int d = inverse( p,q,e);

Int C = power (m, e, n);

The ciphertext of cout & lt& lt is: "<& ltC<& ltendl"

Cout & lt& lt\ n \ Please enter the ciphertext: "<& ltendl"

CIN & gt; & gtc;

Cout & lt& lt\ n \ Please enter p, q and d: "< & ltendl

CIN & gt; & gtp & gt& gtq & gt& gtd;

n = p * q;

M = power (c, d, n);

Cout & lt& lt is plain text: "<& ltm<& ltendl & lt& ltendl"

System ("suspended");

Returns 0;

}

This is RSA encryption and decryption algorithm.