Sie sind auf Seite 1von 4

FACULTY OF APPLIED SCIENCES

DEPARTMENT OF COMPUTER SCIENCE

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.)

The encryption of m = 2 is c = 2^7 mod 33 = 29


The decryption of c = 29 is m = 29^3 mod 33 = 2

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) {

return message.modPow(publicKey, modulus);


}
BigInteger decrypt(BigInteger encrypted) {
return encrypted.modPow(privateKey, modulus);
}
public String toString() {
String s = "";
s += "public = " + publicKey + "\n";
s += "private = " + privateKey + "\n";
s += "modulus = " + modulus;
return s;
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
RSA key = new RSA(N);
System.out.println(key);
// create random message, encrypt and decrypt
BigInteger message = new BigInteger(N-1, random);
//// create message by converting string to integer
// String s = "test";
// byte[] bytes = s.getBytes();
// BigInteger message = new BigInteger(s);
BigInteger encrypt = key.encrypt(message);
BigInteger decrypt = key.decrypt(encrypt);
System.out.println("message = " + message);
System.out.println("encrpyted = " + encrypt);
("decrypted = " + decrypt);
}
}

References
http://java-samples.com/showtutorial.php?tutorialid=288