Implementation RSA Java
Implementation RSA Java
in Java
RSA algorithm
v = 280
• Select small odd integer k
gcd(k, v) = 1
– compute d such that
(d k)%v = 1
• RSA algorithm for encryption/decryption
– encryption: compute E(M) = (Mk)%n
– decryption: compute D(M) = (E(M)d)%n
RSA algorithm for key generation
• Input: none
• Computation:
– select
two prime integers p, q
compute integers n = p q
–
v = (p-1) (q-1)
– select small odd integer k such that gcd(k, v) = 1
– compute integer d such that (d k)%v = 1
• Output: n, k, and d
RSA algorithm for encryption
• Input: integers k, n, M
– M is integer representation of plaintext message
• Computation:
–
let C be integer representation of ciphertext
C = (Mk)%n
• Output: integer C
– ciphertext or encrypted message
RSA algorithm for decryption
• Input: integers d, n, C
– C is integer representation of ciphertext message
• Computation:
–
let D be integer representation of decrypted ciphertext
D = (Cd)%n
• Output: integer D
– decrypted message
This seems hard …
•
Most importantly: RSA manipulates big numbers
– Java integers are of limited size
– how can we handle this?
• Two key items make the implementation easier
– understanding the math
– Java’s BigInteger class
What is a BigInteger?
–
addition and subtraction
– multiplication and division
• Along with operations for
– modular arithmetic
– gcd calculation
– generation of primes
• http://java.sun.com/j2se/1.4.2/docs/api/
Using BigInteger
• Predefined constants
BigInteger.ZERO
BigInteger.ONE
Randomly generated primes
–
probability that BigInteger is not prime < 2-100
• Examples:
BigInteger w = p.add(q);
BigInteger x = p.subtract(q);
BigInteger y = p.multiply(q);
BigInteger z = p.divide(q);
Greatest common divisor
– 30 is divided by 1,2,3,5,6,10,15,30
• Example: gcd(13,15) = 1
– 13 is divided by 1,13
– 15 is divided by 1,3,5,15
• When the gcd of two numbers is one, these numbers are
said to be relatively prime
Euler’s Phi Function
(5) = 4 1,2,3,4
• For any prime number p,
(p) = p-1
• For any integer n that is the product of two distinct
primes p and q,
(n) = (p)(q)
= (p-1)(q-1)
Relative primes
–
specifying an input bit length smaller than that of x
gives a value z<x
• The expression
(x.gcd(z)).equals(BigInteger.ONE)
returns true if gcd(x,z)=1 and false otherwise
• While the above expression evaluates to false, assign a
new random to z
Multiplicative identities and inverses
x x-1 = x-1 x = 1
• The multiplicative inverse of x mod n is the element x-1
such that
(x x-1) mod n = (x-1 x ) mod n = 1
– x and x-1 are inverses only in multiplication mod n
modInverse
BigInteger z = x.modInverse(y);
Implementing RSA key generation