Implementing cryptographic algorithms in SageMath
This worksheet contains
Diffie-Hellman(-Merkle) Key Exchange (1976)
RSA Cryptosystem (Rivest, Shamir, Adleman, 1977)
Homework exercises
ElGamal Cryptosystem (Unlike RSA, ElGamal offers randomized encryption) (1985)
Naor-Pinkas Oblivious Transfer (standard model version, 1999)
Diffie-Hellman-Merkle Key Exchange
Key Exchange protocols allow two parties (Alice and Bob) to establish a shared secret key over a public channel. The shared key is typically used later to encrypt communications using a symmetric cipher. DH key exchange was published in 1977, and it used even today, see for example the SSL/TLS protocol.
Please note that we use a subgroup of the multiplicative group mod p in this example, but the protocol is generic and works with any cyclic group . Today, it is more typical to use the group of points on an elliptic curve. For example, see Dan Bernstein's paper Curve25519: new Diffie-Hellman speed records (2006).
Find a cyclic group where discrete logarithm is difficult. In this example, we will use a -bit safe prime and work in the subgroup generated by an element of order . These parameters can be re-used for multiple key exchanges; however, in this case please consider Imperfect Forward Secrecy: How Diffie-Hellman Fails in Practice by Adrian et al. (2015).
Alice picks a random and sends to Bob.
Bob picks a random and sends to Alice.
Both Alice and Bob can compute the shared key .
RSA Cryptosystem
RSA is (one of?) the first asymetric encryption algorithm.
The public key is a product of two large primes and a small prime exponent, typically .
Private key , where is the order of multiplicative group mod . It is easy to compute the inverse, when factorization of is known, using the extended euclidean algorithm.
Encryption and decryption
To encrypt a message , you compute hte e-th power of M (mod n).
.
So, to decrypt a cryptotext , you have to compute its -th root. .