![]() |
Java
1.0
|
An RSA algorithm for public-key cryptography. More...
Public Member Functions | |
long | generateKeys (long p, long q) |
Generates the public and private keys. More... | |
long | encrypt (long val) |
Encrypts a number. More... | |
long | decrypt (long val) |
Decrypts a number. More... | |
Static Public Member Functions | |
static void | main (String[] args) |
Main function for testing. More... | |
Private Member Functions | |
long | fixRemainder (long a, long b) |
Calculates the correct remainder if (a % b) has the opposite sign of b. More... | |
long | egcd (long a, long b, long[]last) |
The extended Euclidean algorithm for the Greatest Common Divisor of two integers. More... | |
long | modinv (long a, long m) |
Calculates the modular inverse of a long. More... | |
long | modinv2 (long a, long b, long c) |
Calculates the modular inverse of a long. More... | |
long | power_mod_n (long c, long d, long n) |
Calculates c to the power of d mod n. More... | |
Static Private Attributes | |
static long | n |
p*q. More... | |
static long | d |
Private key: (n,d). More... | |
static long | e |
Public key: (n,e). More... | |
An RSA algorithm for public-key cryptography.
The RSA algorithm is named after Ron Rivest, Adi Shamir and Len Adleman,
who invented it in 1977. The basic technique was first discovered in 1973 by Clifford Cocks of CESG (part of the British GCHQ), but this was a secret until 1997. The patent taken out by RSA Labs has expired.
The RSA cryptosystem is the most widely-used public key cryptography algorithm in the word. It can be used to encrypt a message without the need to exchange a secret key separately.
The RSA algorithm can be used for both public key encryption and digital signatures. Its security is based on the difficulty of factoring large integers.
Summary of RSA
To compile:
To execute:
long rsa.decrypt | ( | long | val | ) |
|
private |
long rsa.encrypt | ( | long | val | ) |
|
private |
Calculates the correct remainder if (a % b) has the opposite sign of b.
In C, 17 % -4 = 1, 18 % -4 = 2, 19 % -4 = 3, 20 % -4 = 0 but it shoud be -3, -2, -1 and 0:
e.g., \(\frac{17}{-4}=-4,\) plus a remainder \(\Rightarrow 17 = -4 \times -4+1\), but \(17 =-5 \times -4-3\), either.
a | dividend. |
b | divisor. |
Definition at line 62 of file rsa.java.
Referenced by modinv().
long rsa.generateKeys | ( | long | p, |
long | q | ||
) |
Generates the public and private keys.
In practice, common choices for e are 3, 17 and 65537 \((2^{16}+1)\). These are Fermat primes, sometimes referred to as \(F_0, F_2\) and \(F_4\) respectively \((F_x=2^{2^x}+1)\). They are chosen because they make the modular exponentiation operation faster.
Also, having chosen e, it is simpler to test whether gcd(e, p-1)=1 and gcd(e, q-1)=1 while generating and testing the primes p and q. Values of p or q that fail this test can be rejected there and then.
(Even better: if e is prime and greater than 2 then you can do the less-expensive test: (p mod e)!=1 instead of gcd(p-1,e)==1.)
p | a big prime number. |
q | another prime number. |
Definition at line 181 of file rsa.java.
References d, e, modinv(), and n.
Referenced by main().
|
static |
|
private |
Calculates the modular inverse of a long.
d * a = 1 mod m
a | given long. |
m | divisor. |
Definition at line 110 of file rsa.java.
References egcd(), and fixRemainder().
Referenced by generateKeys(), and main().
|
private |
|
private |
|
staticprivate |
Private key: (n,d).
Definition at line 45 of file rsa.java.
Referenced by decrypt(), generateKeys(), main(), and power_mod_n().
|
staticprivate |
Public key: (n,e).
Definition at line 49 of file rsa.java.
Referenced by encrypt(), generateKeys(), main(), and power_mod_n().
|
staticprivate |
p*q.
Definition at line 41 of file rsa.java.
Referenced by decrypt(), encrypt(), generateKeys(), main(), and power_mod_n().