The RSA cipher system
Package RSAExample
Import "math/modular".
{---------------------------------------------
inverse m x = the inverse of x (mod m).
The algorithm is the Extended Euclidean Algorithm,
and is taken from the math/modular library.
----------------------------------------------}
Let inverse m x = y |
Match (y `modulo` ?) = /(x `modulo` m).
%Let
{----------------------------------------------
Main program
-----------------------------------------------}
Execute
Let
p = 5;
q = 11;
n = p*q;
r = (p-1)*(q-1);
e = head [x | x from [3,...,r-1], gcd(x,r) == 1];
d = inverse r e;
enc(x) = x^e `mod` n;
dec(x) = x^d `mod` n;
a = enc(2);
aa = dec(a);
b = enc(3);
bb = dec(b);
c = enc(50);
cc = dec(c);
%Let
Display
"n = "; n; "\n";
"r = "; r; "\n";
"d = "; d; "\n";
"e = "; e; "\n";
"\n";
"enc(2) = "; a; "\n";
"dec(enc(2)) = "; aa; "\n";
"enc(3) = "; b; "\n";
"dec(enc(3)) = "; bb; "\n";
"enc(50) = "; c; "\n";
"dec(enc(50)) = "; cc; "\n";
%Display
%Execute
%Package
{------------------------------------
Output:
n = 55
r = 40
e = 3
d = 27
enc(2) = 8
dec(enc(2)) = 2
enc(3) = 27
dec(enc(3)) = 3
enc(50) = 40
dec(enc(50)) = 50
-------------------------------------}