de.flexiprovider.core.rsa
Class RSA_PKCS1_v2_1

java.lang.Object
  |
  +--javax.crypto.CipherSpi
        |
        +--de.flexiprovider.api.Cipher
              |
              +--de.flexiprovider.api.AsymmetricBlockCipher
                    |
                    +--de.flexiprovider.core.rsa.RSA_PKCS1_v2_1

public class RSA_PKCS1_v2_1
extends AsymmetricBlockCipher

This class implements the RSA algorithm as defined in PKCS#1 version 2.1 in the OAEP (Optimal Asymmetric Encryption Padding) mode. The OAEP mode is recommended for new applications.

To encrypt a message, the following steps have to be performed:

 // The message to encrypt
 String message = "secret message";
 byte[] messageBytes = message.getBytes();
 
 // The source of randomness
 SecureRandom secureRandom = Registry.getSecureRandom();
 
 // Obtain a RSA Cipher Object
 Cipher rsaCipher = Cipher.getInstance("RSA_PKCS1_v2_1");
 
 // Obtain the corresponding key pair generator
 KeyPairGenerator rsaKPG = KeyPairGenerator.getInstance("RSA");
 
 // Initialize the key pair generator with the desired strength
 rsaKPG.initialize(1024);
 
 // Generate a key pair
 KeyPair rsaKeyPair = rsaKPG.genKeyPair();
 
 // Initialize the cipher
 // Note: if the public key has n with k(k - the length of n in octets) less than the 
 // HEADER_SIZE of RSA_PKCS1_v1_5, a RuntimeException is thrown.
 cipher.init(Cipher.ENCRYPT_MODE, rsaKeyPair.getPublic(), secureRandom);
 
 // Finally encrypt the message
 // If some of the PKCS1 functions fail during encryption, a RuntimeException is thrown.
 byte[] ciphertextBytes = cipher.doFinal(messageBytes);
 
To decrypt a ciphertext, the Cipher must be initialized with Cipher.DECRYPT_MODE and the private key (rsaKeyPair.getPrivate()). Decrypting, there are some special cases one should take in consideration:
1. If the length of the input is not equal to the maximum cipher text length. A RuntimeException is thrown and the decryption is aborted.
2. If some of the PKCS1 functions fail during encryption, a RuntimeException is thrown.

Author:
Thomas Wahrenbruch, Ralf-Philipp Weinmann

Field Summary
static java.lang.String OID
          The OID of RSA_PKCS1_v2_1.
 
Fields inherited from class de.flexiprovider.api.AsymmetricBlockCipher
buf, cipherTextSize, maxPlainTextSize, paramSpec
 
Fields inherited from class de.flexiprovider.api.Cipher
DECRYPT_MODE, ENCRYPT_MODE, opMode
 
Constructor Summary
RSA_PKCS1_v2_1()
           
 
Method Summary
 int getKeySize(Key key)
          Returns the key size of the given key object.
 java.lang.String getName()
           
protected  void initCipherDecrypt(Key key, AlgorithmParameterSpec params)
          This method initializes the block cipher with a certain key and parameters for data encryption.
protected  void initCipherEncrypt(Key key, AlgorithmParameterSpec params, SecureRandom secureRandom)
          This method initializes the block cipher with a certain key and parameters for data encryption.
protected  byte[] messageDecrypt(byte[] input)
          Decrypt a ciphertext.
protected  byte[] messageEncrypt(byte[] input)
          Encrypt a message.
 
Methods inherited from class de.flexiprovider.api.AsymmetricBlockCipher
checkLength, doFinal, doFinal, getBlockSize, getIV, getOutputSize, getParameters, initDecrypt, initDecrypt, initEncrypt, initEncrypt, initEncrypt, initEncrypt, setMode, setPadding, update, update
 
Methods inherited from class de.flexiprovider.api.Cipher
doFinal, doFinal, engineDoFinal, engineDoFinal, engineGetBlockSize, engineGetIV, engineGetKeySize, engineGetOutputSize, engineGetParameters, engineInit, engineInit, engineInit, engineSetMode, engineSetPadding, engineUpdate, engineUpdate, update
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OID

public static final java.lang.String OID
The OID of RSA_PKCS1_v2_1.
Constructor Detail

RSA_PKCS1_v2_1

public RSA_PKCS1_v2_1()
Method Detail

getName

public java.lang.String getName()
Overrides:
getName in class Cipher
Returns:
the name of this cipher

getKeySize

public int getKeySize(Key key)
               throws InvalidKeyException
Returns the key size of the given key object. Checks whether the key object is an instance of RSAPublicKey or RSAPrivateKey. Would be simpler to just check for RSAKey but this breaks build with Java 1.2.
Overrides:
getKeySize in class Cipher
Parameters:
key - the key object
Returns:
the key size of the given key object.
Throws:
InvalidKeyException - if key is invalid.

initCipherEncrypt

protected void initCipherEncrypt(Key key,
                                 AlgorithmParameterSpec params,
                                 SecureRandom secureRandom)
                          throws InvalidKeyException
This method initializes the block cipher with a certain key and parameters for data encryption.
Overrides:
initCipherEncrypt in class AsymmetricBlockCipher
Parameters:
key - the key which has to be used to encrypt data
params - the algorithm parameters
secureRandom - the source of randomness
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher.

initCipherDecrypt

protected void initCipherDecrypt(Key key,
                                 AlgorithmParameterSpec params)
                          throws InvalidKeyException
This method initializes the block cipher with a certain key and parameters for data encryption.
Overrides:
initCipherDecrypt in class AsymmetricBlockCipher
Parameters:
key - the key which has to be used to decrypt data
params - the algorithm parameters
Throws:
InvalidKeyException - if the given key is inappropriate for initializing this cipher.

messageEncrypt

protected byte[] messageEncrypt(byte[] input)
Encrypt a message.
Overrides:
messageEncrypt in class AsymmetricBlockCipher
Parameters:
input - the plaintext
Returns:
the encrypted plaintext

messageDecrypt

protected byte[] messageDecrypt(byte[] input)
Decrypt a ciphertext.
Overrides:
messageDecrypt in class AsymmetricBlockCipher
Parameters:
input - the ciphertext
Returns:
the decrypted ciphertext