Cryptography CS5830 - Jake Magid - [email protected]
The file can be called from the terminal as python oneway.py
and will generate x, f(x), and x_ret using RSA implementation and trapdoor. This will also print a safe prime number with n_in
bits. Change this value as necessary.
Other functions can be tested in the Print Section
of the code by uncommenting them and manipulating the inputs as necessary.
-
Extended Euclid
extendedeuclid(a,b)
Input: Two valuesa
andb
Output:x,y
such thatax + by = 1
-
Inverse Modulus
inv_mod(a,b)
Input: Two valuesa
andb
Output: Modular inverse ofa mod b
(i.e.x
such thatax mod b = 1
) -
Exponential Modulus
exp_mod(a,x,N)
Input: Valuesa, x, N
Output:a^x mod N
-
Miller-Rabin Primality Test
is_prime(n)
Input: odd integern
to be tested for primality
Output:True
if number is probably prime,False
if number is composite -
Random n-bit Prime
rand_nbit_prime(n)
Input: max number of bitsn
which prime number can be represented by
Output: random prime integer that can be represented usingn
bits -
Random n-bit Safe Prime
rand_nbit_safe_prime(n)
Input: number of bits to find a safe primen
Output:p
, an n-bit safe prime andq
, the corresponding Sophie Germain prime -
Random n-bit Safe Prime Generator
rand_nbit_safe_prime_generator(n)
Input: number of bits to find a safe primen
Output:p
, an n-bit safe prime andg
, a generator for Zp* -
Exponentiation One Way Permutation
class exponentiation_OWP
-
gen(n)
Input: number of bitsn
Output:p
, an n-bit safe prime andg
, a generator for Zp* -
sample(p)
Input:p
fromgen()
Output:x
, a random integer from1
top-1
-
evaluate(p,g,x)
Input:p,g,x
fromgen()
andsample()
Output:fx
which isg^x mod p
-
-
RSA One Way Permutation
class RSA_OWP
-
gen(n)
Input: number of bitsn
forp
andq
Output: The public keysN,e
and the private trapdoor keyd
.
N
isp*q
.e
is a random integer from 2 tophi-1
wherephi = (p-1)*(q-1)
.d
is the inverse modulus ofe
andphi
. -
sample(N)
Input:N
fromgen()
Output:x
, a random integer from1
toN
-
evaluate(x,e,N)
Input:x,e,N
fromgen()
andsample()
Output:fx
which isx^e mod N
-
-
Trapdoor for RSA
trapdoor(fx, d, N)
Input:fx, d, N
fromRSA_OWP.gen()
,RSA_OWP.sample()
, andRSA_OWP.evaluate()
Output: The correspondingx
that producedfx
. The magic here is thatx
can be retrieved fromfx
using the public informationN
, and the private keyd
.