public class RSA
extends java.lang.Object
RSA/ECB/PKCS1Padding
), also known as PKCS #1.
RSA myRSA = new RSA();
myRSA.init(1024);
System.out.println(myRSA.decrypt(myRSA.encrypt("Hello world!")));
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CIPHER
Name of the cipher implemented,
provided for interoperability.
|
private java.math.BigInteger |
d
The RSA private exponent, a positive integer where
e*d = 1 mod φ(n).
|
private java.math.BigInteger |
e
|
private static int |
MINIMUM_PADDING_BYTES
The standard requires that "[t]he length of the data D shall not be more
than k-11 octets."
|
private java.math.BigInteger |
n
The RSA modulus, a positive integer where n = p*q.
|
private java.math.BigInteger |
p
A large probable prime.
|
static java.lang.String |
PKCS_VERSION
|
private java.math.BigInteger |
q
The other large probable prime.
|
Constructor and Description |
---|
RSA()
Creates an instance of RSA without generating keys.
|
RSA(java.math.BigInteger e,
java.math.BigInteger p,
java.math.BigInteger q)
Creates an instance of RSA using specified RSA encryption exponent and prime numbers
p and q.
|
RSA(java.math.BigInteger e,
java.math.BigInteger p,
java.math.BigInteger q,
java.math.BigInteger d,
java.math.BigInteger n)
Creates an instance of RSA using specified RSA encryption exponent, prime numbers
p and q, decryption exponent, and modulus with no error checking.
|
RSA(int keyLength)
Creates an instance of RSA and generates keys of the given key length.
|
RSA(java.security.KeyPair kp)
Creates an instance of RSA and imports keys from the given key pair.
|
Modifier and Type | Method and Description |
---|---|
private byte[] |
addPadding(byte[] input)
In PKCS #1v1.5
(RFC 2313), the encryption block (EB) contains a block type (BT)
and padding string (PS), surrounded by zeros and followed by the data (D).
EB = 00 || BT || PS || 00 || D This implementation is only intended to encrypt data using the public key, so the block type is set to 02. The padding string is at least eight octets long, with an exact length of k - 3 - mLen (mLen is the length in octets of the data). |
private java.math.BigInteger |
decrypt(java.math.BigInteger ciphertext,
java.math.BigInteger d,
java.math.BigInteger n)
Decrypts a cipher C such that
D(C)≅Cd mod n.
|
java.lang.String |
decrypt(java.lang.String ciphertext)
Decrypts a string cipher C such that
D(C)≅Cd mod n
using previously generated values of d and n.
|
java.lang.String |
decrypt(java.lang.String ciphertext,
java.nio.charset.Charset charset)
Decrypts a string cipher C such that
D(C)≅Cd mod n
using previously generated values of d and n.
|
java.lang.String |
decryptNonstandard(java.lang.String ciphertext)
Decrypts given cipher and assumes that the unencrypted data is not
padded according to PKCS.
|
private java.math.BigInteger |
encrypt(java.math.BigInteger message,
java.math.BigInteger e,
java.math.BigInteger n)
Encrypts a message M such that
E(M)≅Me mod n
|
java.lang.String |
encrypt(java.lang.String m)
Encrypts a string message M such that
E(M)≅Me mod n
using previously generated values of e and n.
|
java.lang.String |
encrypt(java.lang.String plaintext,
java.nio.charset.Charset charset)
Encrypts a string message M such that
E(M)≅Me mod n
using previously generated values of e and n.
|
java.lang.String |
encryptNonstandard(java.lang.String plaintext)
Encrypts provided string without checking message length restrictions
and without PKCS padding.
|
java.security.KeyPair |
exportKeys()
Exports generated keying material in a familiar KeyPair format
that should be usable in other programs.
|
protected java.math.BigInteger |
getD()
Gives package members direct access to RSA decryption exponent.
|
protected java.math.BigInteger |
getE()
Gives package members direct access to RSA encryption exponent.
|
int |
getMaximumBlockSize()
PKCS #1v1.5
requires 11 bytes of padding before the message, so the maximum block size allowed is
k - 11, where k is the length of the RSA modulus, n, in bytes.
This limit must be enforced to prevent the message to be encrypted from being "larger than the RSA modulus. |
int |
getModulusLength()
Indicates the length of the RSA modulus n in bits.
This should equal the value passed to generateKeys . |
protected java.math.BigInteger |
getN()
Gives package members direct access to RSA modulus.
|
protected java.math.BigInteger |
getP()
Gives package members direct access to prime number p used to generate n.
|
protected java.math.BigInteger |
getQ()
Gives package members direct access to prime number q used to generate n.
|
private static java.math.BigInteger |
getRSAParameter(java.lang.String desc,
java.io.BufferedReader input) |
boolean |
importKeys(java.security.KeyPair kp)
Imports a KeyPair to instance variables d, e, and n.
|
int |
init(int keyLength)
Generates a keypair with the given key length.
|
private static void |
interactive() |
static void |
main(java.lang.String[] args)
Main method will encrypt and decrypt each word given at the command line.
|
private byte[] |
nonZeroRandomByteArray(int length)
Generates a byte array consisting of nonzero values.
|
private java.math.BigInteger |
phi(java.math.BigInteger p,
java.math.BigInteger q)
Euler's Totient Function,
φ(n), gives φ(n) = φ(p)*φ(q) = (p-1)*(q-1)
iff both p and q are prime.
|
private byte[] |
removePadding(byte[] output)
Removes the padding generated
in the
addPadding method. |
private boolean |
testGCD(java.math.BigInteger e,
java.math.BigInteger p,
java.math.BigInteger q)
Tests the quality of randomly chosen integers p and q; their Euler Totient should not
be a multiple of e.
|
public static java.lang.String CIPHER
private java.math.BigInteger d
private java.math.BigInteger e
private static int MINIMUM_PADDING_BYTES
private java.math.BigInteger n
private java.math.BigInteger p
init but are otherwise unused.
public static java.lang.String PKCS_VERSION
private java.math.BigInteger q
public RSA()
generateKeys
or importKeys
before using encrypt
and decrypt
operations.public RSA(java.math.BigInteger e, java.math.BigInteger p, java.math.BigInteger q)
e
- RSA encryption exponent.p
- Large prime number.q
- Another large prime number.public RSA(java.math.BigInteger e, java.math.BigInteger p, java.math.BigInteger q, java.math.BigInteger d, java.math.BigInteger n)
e
- RSA encryption exponent.p
- Large prime number.q
- Another large prime number.d
- RSA decryption exponent.n
- RSA modulus (n = p*q).public RSA(int keyLength)
keyLength
- Length of the key to be generated from 1024 to 4096.public RSA(java.security.KeyPair kp)
kp
- KeyPair to import and replace values of d, e, and n.private byte[] addPadding(byte[] input)
EB = 00 || BT || PS || 00 || D
input
- The byte array to pad.private java.math.BigInteger decrypt(java.math.BigInteger ciphertext, java.math.BigInteger d, java.math.BigInteger n)
ciphertext
- Ciphertext message to decrypt.d
- RSA private exponent.n
- RSA modulus (n=p*q).public java.lang.String decrypt(java.lang.String ciphertext)
ciphertext
- String cipher to decrypt.public java.lang.String decrypt(java.lang.String ciphertext, java.nio.charset.Charset charset)
ciphertext
- String cipher to decrypt.charset
- Charset to use when recreating String (defaults to UTF-8).public java.lang.String decryptNonstandard(java.lang.String ciphertext)
ciphertext
- String cipher to decrypt.private java.math.BigInteger encrypt(java.math.BigInteger message, java.math.BigInteger e, java.math.BigInteger n)
message
- plaintext to encrypt.e
- RSA public exponent.n
- RSA modulus (n=pq).public java.lang.String encrypt(java.lang.String m)
m
- String message to encrypt.public java.lang.String encrypt(java.lang.String plaintext, java.nio.charset.Charset charset)
plaintext
- String message to encrypt.charset
- Charset used to encode String (defaults to UTF-8).public java.lang.String encryptNonstandard(java.lang.String plaintext)
plaintext
- String message to encrypt.public java.security.KeyPair exportKeys() throws java.security.NoSuchAlgorithmException, java.security.spec.InvalidKeySpecException
java.security.NoSuchAlgorithmException
java.security.spec.InvalidKeySpecException
protected java.math.BigInteger getD()
protected java.math.BigInteger getE()
public int getMaximumBlockSize()
public int getModulusLength()
generateKeys
.protected java.math.BigInteger getN()
protected java.math.BigInteger getP()
protected java.math.BigInteger getQ()
private static java.math.BigInteger getRSAParameter(java.lang.String desc, java.io.BufferedReader input)
public boolean importKeys(java.security.KeyPair kp)
kp
- KeyPair to import and replace values of d, e, and n.public int init(int keyLength)
keyLength
- The length of the RSA modulus, in bits, from 1024 to 4096.private static void interactive()
public static void main(java.lang.String[] args)
args
- Each space-delimited word provided will be encrypted and decrypted separately.private byte[] nonZeroRandomByteArray(int length)
private java.math.BigInteger phi(java.math.BigInteger p, java.math.BigInteger q)
p
- A large prime number.q
- Another large prime number.private byte[] removePadding(byte[] output)
addPadding
method.output
- The byte array from which to remove padding.private boolean testGCD(java.math.BigInteger e, java.math.BigInteger p, java.math.BigInteger q)
e
- RSA public exponent.p
- A large prime number.q
- Another large prime number.