Comp Sec Assign 2
Comp Sec Assign 2
NAME:
Tatenda T. Taodzera
STUDENT ID:
N0082840R
COURSE:
Computer Security
COURSE CODE:
SCS4107
LECTURER:
MR D Musundire
DUE DATE:
6 Nov 2012
ASSIGNMENT 2
QUESTION:
1. The RSA algorithm uses number theory to generate keys for use in the transmission
of data in an unsecure environment.
a.) Given the following values generate the public and private keys p=3, q=11 and e=7.
b.) Using the generated key in a.) provide proof that the keys are valid by encrypting
and decrypting plaintext p=2.
c.) The java library class java.lang.math contains a static method pow(x,y) which
translates to x^y. write a java application which makes use of three methods which
generates the two keys, encrypt which accepts plaintext and outputs the cipher and
decrypt which accepts a cipher and returns plaintext.
1 a.)
Choose p = 3 and q = 11
Compute n = p * q = 3 * 11 = 33
Compute (n) = (p - 1) * (q - 1) = 2 * 10 = 20
Choose e such that 1 < e < (n) and e and n are coprime. Let e = 7
Compute a value for d such that (d * e) mod (n) = 1. One solution is d = 3 [(3 * 7) mod
20 = 1]
Public key is (e, n) => (7, 33)
Private key is (d, n) => (3, 33)
b.)
c.)
import java.math.BigInteger;
import java.security.SecureRandom;
public class RSA {
private final static BigInteger one = new BigInteger("1");
private final static SecureRandom random = new SecureRandom();
private BigInteger privateKey;
private BigInteger publicKey;
private BigInteger modulus;
// generate an N-bit (roughly) public and private key
RSA(int N) {
p = BigInteger.probablePrime(N/2, random);
BigInteger q = BigInteger.probablePrime(N/2, random);
BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));
modulus = p.multiply(q);
publicKey = new BigInteger("65537"); // common value in practice =
2^16 + 1
privateKey = publicKey.modInverse(phi);
}
BigInteger encrypt(BigInteger message) {
References
http://java-samples.com/showtutorial.php?tutorialid=288