public class RsaCipher extends AsymmetricCipher
The RSA public-key cryptosystem was invented in 1977 by Ron Rivest, Adi Shamir, and Leonard Adleman; the algorithm is defined in PKCS #1: RSA Cryptography Standard. The security of the algorithm is based on the intractability of the integer factorization.
RSA requires two keys, an RSA public key and an RSA private key, which together form an RSA key pair. The keys used with RSA (an RSA key pair) are defined as follows:
n: the moduluse: the public exponentn: the modulusd: the private exponentThe RSA public-key cryptosystem can be used for different purposes: encryption and decryption; and signature and verification. For each purpose the RSA algorithm is employed in a different manner; this is referred to as RSA primitive operations. The encryption primitive produces ciphertext from a message under the control of a public key (public key encipherment), and the decryption primitive recovers the message from the ciphertext (private key decipherment) under the control of the corresponding private key. These operations are defined in PKCS #1 as follows:
(n, e)m; message must be an integer in the interval
[0 .. n - 1]c = me mod nc; ciphertext will be an integer in the interval
[0 .. n - 1](n, d)c; ciphertext must be an integer in the interval
[0 .. n - 1]m = cd mod nm; message will be an integer in the interval
[0 .. n - 1]The signature primitive produces a signature from a message under the control of a private key (private key encipherment), and a verification primitive recovers the message from the signature under the control of the corresponding public key (public key decipherment). These operations are defined in PKCS #1 as follows:
(n, d)m; message must be an integer in the interval
[0 .. n - 1]s = md mod ns; signature will be an integer in the interval
[0 .. n - 1](n, e)s; signature must be an integer in the interval
[0 .. n - 1]m = se mod nm; message will be an integer in the interval
[0 .. n - 1]If either primitive that employs decipherment (decryption or verification primitive) was used alone, it would be impossible to determine the length of the original message unless some sort of message padding (encoding) is employed. For this reason, PKCS #1 defines encoding methods that address this problem. Together, the primitive operation pairs and an encoding method form a cryptographic scheme (encryption scheme or signature scheme). This RSA implementation provides support for the following schemes:
An RSA cipher algorithm instance operating according to any of the above
schemes can be obtained using the Java Cryptography Architecture (JCA), by
requesting 'RSA/<scheme>' cipher from the
Entrust cryptographic
service provider. This can be done for each scheme using the following calls:
Cipher.getInstance("RSA/OAEP/PKCS1Padding", "Entrust");Cipher.getInstance("RSA/2/PKCS1Padding", "Entrust");Cipher.getInstance("RSA/1/PKCS1Padding", "Entrust");The following calls are also supported; in both cases, the algorithm will operate according to the either RSAESPKCS1-v1_5 or RSASSA-PKCS1-v1_5, automatically selecting the appropriate scheme based on the type of key (public or private) and the operation being performed (encipherment or decipherment)
Cipher.getInstance("RSA", "Entrust");Cipher.getInstance("RSA/ECB/PKCS1Padding", "Entrust");
One disadvantage of RSA is that the message being encrypted cannot be larger
than the modulus n. Also, because RSA requires modular
exponentiation operations, the RSA public-key cryptosystem is substantially
slower at processing data than most symmetric cipher algorithms. Because of
these factors, RSA is typically used in conjunction with a symmetric cipher
algorithm. Usually, RSA is used to protect a symmetric key, which in turn is
used to protect the data.
This implementation has been designed to support the following RSA key types:
java.security.interfaces.RSAPrivateKey or
java.security.interfaces.RSAPublicKey.RsaConfinedPrivateKey.TokenRSAPrivateKey
.CapiRsaPrivateKey.When the cipher is initialized with a key, an appropriate underlying RSA algorithm implementation is automatically selected based on the type of key passed in. For token keys and CAPI keys, the requested scheme is always ignored; instead the RSAESPKCS1-v1_5 encryption scheme or RSASSA-PKCS1-v1_5 signature scheme is automatically used (for these types of keys RSAES-OAEP is not yet supported).
This class SHOULD NOT be used directly; it should only be used through the JCA/JCE.
RSAPrivateKey,
RSAPublicKey| Modifier and Type | Class and Description |
|---|---|
static class |
RsaCipher.EsOaep
An implementation of the RSA asymmetric cipher algorithm configured for
operation as the RSAES-OAEP encryption scheme.
|
static class |
RsaCipher.EsPkcs1v15
An implementation of the RSA asymmetric cipher algorithm configured for
operation as the RSAESPKCS1-v1_5 encryption scheme.
|
static class |
RsaCipher.SsaPkcs1v15
An implementation of the RSA asymmetric cipher algorithm configured for
operation as the RSASSA-PKCS1-v1_5 signature scheme.
|
| Constructor and Description |
|---|
RsaCipher()
The constructor; creates a new instance of the RSA asymmetric cipher
algorithm.
|
| Modifier and Type | Method and Description |
|---|---|
protected int |
engineGetKeySize(java.security.Key key)
(non-Javadoc)
|
protected java.security.AlgorithmParameters |
engineGetParameters()
Returns the parameters used with this cipher.
|
protected void |
engineSetMode(java.lang.String mode)
Sets the block mode of this cipher.
|
protected void |
engineSetPadding(java.lang.String padding)
Sets the padding type of this cipher.
|
protected int |
implGetOutputSize(int inputLen)
Determines the maximum amount of data in bytes that will be output by a
'process data' operation.
|
protected int |
implInit(int opmode,
java.security.Key key,
java.security.AlgorithmParameters opaqueParams,
java.security.spec.AlgorithmParameterSpec transparentParams)
Initializes the underlying asymmetric cipher implementation.
|
protected byte[] |
implProcessData(byte[] input,
int inputOffset,
int inputLen)
Processes the data using the underlying asymmetric cipher implementation.
|
engineDoFinal, engineDoFinal, engineGetBlockSize, engineGetIV, engineGetOutputSize, engineInit, engineInit, engineInit, engineUnwrap, engineUpdate, engineUpdate, engineWrap, getPrngpublic RsaCipher()
Applications should never use this constructor, instead the symmetric
cipher algorithm should be requested from the appropriate JCA/JCE
cryptographic service provider as follows:
Cipher.getInstance("RSA", "Entrust").
protected final void engineSetPadding(java.lang.String padding)
throws javax.crypto.NoSuchPaddingException
AsymmetricCipherengineSetPadding in class AsymmetricCipherpadding - [FIPS 140-2 control input] the padding typejavax.crypto.NoSuchPaddingException - [FIPS 140-2 status output] if the requested padding type does
not existprotected final void engineSetMode(java.lang.String mode)
throws java.security.NoSuchAlgorithmException
AsymmetricCipherengineSetMode in class AsymmetricCiphermode - [FIPS 140-2 control input] the cipher block modejava.security.NoSuchAlgorithmException - [FIPS 140-2 status output] if the requested cipher block mode
is not supportedprotected final java.security.AlgorithmParameters engineGetParameters()
AsymmetricCipherThe returned parameters may be the same that were used to initialize this cipher, or may contain a combination of default and random parameter values used by the underlying cipher implementation if this cipher requires algorithm parameters but was not initialized with any.
engineGetParameters in class AsymmetricCipherprotected final int engineGetKeySize(java.security.Key key)
throws java.security.InvalidKeyException
engineGetKeySize in class AsymmetricCipherkey - [FIPS 140-2 data input] [FIPS 140-2 CSP] the key objectjava.security.InvalidKeyException - [FIPS 140-2 status output] if key is invalidAsymmetricCipher.engineGetKeySize(java.security.Key)protected int implInit(int opmode,
java.security.Key key,
java.security.AlgorithmParameters opaqueParams,
java.security.spec.AlgorithmParameterSpec transparentParams)
throws java.security.InvalidKeyException,
java.security.InvalidAlgorithmParameterException
AsymmetricCipherimplInit in class AsymmetricCipherkey - the keyopaqueParams - the algorithm parameters in opaque formattransparentParams - the algorithm parameters in transparent formatjava.security.InvalidKeyException - if the given key is inappropriate for initializing this
cipherjava.security.InvalidAlgorithmParameterException - if the given algorithm parameters are inappropriate for
this cipher, or if this cipher is being initialized for
decryption and requires algorithm parameters and
params is null.protected final byte[] implProcessData(byte[] input,
int inputOffset,
int inputLen)
throws javax.crypto.BadPaddingException,
javax.crypto.IllegalBlockSizeException
AsymmetricCipherimplProcessData in class AsymmetricCipherinput - the input bufferinputOffset - the offset in input where the input startsinputLen - the input lengthjavax.crypto.BadPaddingException - if this cipher is in decryption mode, but the decrypted
data is not bounded by the appropriate padding bytesjavax.crypto.IllegalBlockSizeException - if the amount of data (plaintext or ciphertext) provided
to the underlying asymmetric cipher implementation is in
appropriate for the key being usedprotected final int implGetOutputSize(int inputLen)
AsymmetricCipherimplGetOutputSize in class AsymmetricCipherinputLen - the input length (in bytes)