0% found this document useful (0 votes)
168 views19 pages

Implementing RSA Encryption in Java

The document describes how to implement RSA encryption in Java using the BigInteger class. It explains the key steps of RSA, including generating prime numbers, computing the modulus and Euler's phi function, finding the private and public keys, and encrypting and decrypting messages. It outlines how BigInteger can be used to perform the necessary arithmetic on large numbers, such as computing greatest common divisors, modular inverses, and generating random prime numbers of arbitrary precision.

Uploaded by

saeeed3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views19 pages

Implementing RSA Encryption in Java

The document describes how to implement RSA encryption in Java using the BigInteger class. It explains the key steps of RSA, including generating prime numbers, computing the modulus and Euler's phi function, finding the private and public keys, and encrypting and decrypting messages. It outlines how BigInteger can be used to perform the necessary arithmetic on large numbers, such as computing greatest common divisors, modular inverses, and generating random prime numbers of arbitrary precision.

Uploaded by

saeeed3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Implementing RSA Encryption

in Java

RSA algorithm
Select two large prime numbers
p, q
Compute
n=pq
(n) = (p-1) (q-1)
Select small odd integer e
relatively prime to (n)
gcd(e, (n) ) = 1
Compute d such that
(d e)% (n) = (e d)% (n) = 1
Public key is (e, n)
Private key is (d, n)

example
p = 11
q = 29
n = 319
(n) = 280
e=3
d = 187
public key
(3, 319)
private key
(187, 319)

Encryption and decryption


Alice and Bob would like to communicate in private
Alice uses RSA algorithm to generate her public and
private keys
Alice makes key (e, n) publicly available to Bob and
anyone else wanting to send her private messages
Bob uses Alices private key (d, n) to encrypt message
M:
compute E(M) =(Md)%n
Bob sends encrypted message E(M) to Alice
Alice receives E(M) and uses public key (e, n) to decrypt
it:
compute D(M) = (E(M)e)%n
decrypted message D(M) is original message M

Outline of implementation
RSA algorithm for key generation
select two prime numbers p, q
compute n = p q
(n) = (p-1) (q-1)
select small odd integer k such that
gcd(e, (n) ) = 1
compute d such that
(d e)% (n) = 1
RSA algorithm for encryption/decryption
encryption: compute E(M) = (Md)%n
decryption: compute D(M) = (E(M)e)%n

RSA algorithm for key generation


Input: none
Computation:
select two prime integers p, q
compute integers n = p q
(n) = (p-1) (q-1)
select small odd integer e such that gcd(e, (n) ) = 1
compute integer d such that (d e)% (n) = 1
Output: n, e, and d

RSA algorithm for encryption


Input: integers d, n, M
M is integer representation of plaintext message
Computation:
let C be integer representation of ciphertext
C = (Md)%n
Output: integer C
ciphertext or encrypted message

RSA algorithm for decryption


Input: integers e, n, C
C is integer representation of ciphertext message
Computation:
let D be integer representation of decrypted ciphertext
D = (Ce)%n
Output: integer D
decrypted message

This seems hard

How to find big primes?


How to find mod inverse?
How to compute greatest common divisor?
How to translate text input to numeric values?
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
Javas BigInteger class

What is a BigInteger?
Java class to represent and perform operations on
integers of arbitrary precision
Provides analogues to Javas primitive integer
operations, e.g.
addition and subtraction
multiplication and division
Along with operations for
modular arithmetic
gcd calculation
generation of primes

Using BigInteger
If we understand what mathematical computations are
involved in the RSA algorithm, we can use Javas
BigInteger methods to perform them

To declare a BigInteger named B


BigInteger B;
Predefined constants
BigInteger.ZERO
BigInteger.ONE

Randomly generated primes


public BigInteger(int bitLength,int
certainty,Random rng)
bitLength - bitLength of the returned BigInteger.
certainty - The probability that the new BigInteger
represents a prime number will exceed (1 - 1/2certainty).
Random is Javas class for random number generation
The following statement
Random rng = new Random();
creates a new random number generator named rng

BigInteger Object
Example: randomly generate two BigInteger primes
named p and q of bit length 4 :
/* create a random number generator */
BigInteger p,q;
/*Generate Two Random Prime numbers p,q*/
p= new BigInteger(4, 15, new Random());
q= new BigInteger(4, 15, new Random());

Integer operations
Suppose have declared and assigned values for p and q
and now want to perform integer operations on them
use methods add, subtract, multiply, divide
result of BigInteger operations is a BigInteger
Examples:
BigInteger
BigInteger
BigInteger
BigInteger

w
x
y
z

=
=
=
=

p.add(q);
p.subtract(q);
p.multiply(q);
p.divide(q);

Greatest common divisor


The greatest common divisor of two numbers x and y is
the largest number that divides both x and y
this is usually written as gcd(x,y)
Example: gcd(20,30) = 10
20 is divided by 1,2,4,5,10,20
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

Eulers Phi Function


For a positive integer n, (n) is the number of positive
integers less than n and relatively prime to n
Examples:
(3) = 2
1,2
(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

We know that x and z are relatively prime if their


greatest common divisor is one
randomly generate prime values for z until gcd(x,z)=1
if x is a product of distinct primes, there is a value of z
satisfying this equality

Relative BigInteger primes


Suppose we have declared a BigInteger x and
assigned it a value
Declare a BigInteger z
Assign a prime value to z using the probablePrime
method
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


The multiplicative identity is the element e such that
e*x=x*e=x
The multiplicative inverse of x is the element x-1 such that
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
Suppose we have declared BigInteger variables x, y
and assigned values to them
We want to find a BigInteger z such that
(x*z)%y =(z*x)%y = 1
that is, we want to find the inverse of x mod y and
assign its value to z

This is accomplished by the following statement:


BigInteger z = x.modInverse(y);

You might also like