com.rsa.jsafe
Class JSAFE_SymmetricCipher

java.lang.Object
  extended bycom.rsa.jsafe.JSAFE_Object
      extended bycom.rsa.jsafe.JSAFE_SymmetricCipher
All Implemented Interfaces:
Cloneable, Serializable

public abstract class JSAFE_SymmetricCipher
extends JSAFE_Object
implements Cloneable, Serializable

This class defines the factory methods and API for all symmetric cipher objects. This is the actual encryption object; it does not perform key management or creation.

See Overview of Crypto-J for background and reference material on using and understanding Crypto-J.

Copyright © RSA Security Inc., 1997-2005. All rights reserved.

See Also:
Serialized Form

Constructor Summary
JSAFE_SymmetricCipher()

 

 
Method Summary

 void

clearSensitiveData()

This method clears sensitive data from an object.

 byte[]

decryptFinal()

Finalizes the decryption process and strips any padding.

abstract  int

decryptFinal(byte[] partOut, int offset)

Finalizes the decryption process and strips any padding.

 void

decryptInit(JSAFE_SecretKey key)

Initializes this object to decrypt using key, which must be instantiated with the same algorithm.

abstract  void

decryptInit(JSAFE_SecretKey key, SecureRandom random)

This method initializes an object to perform decryption with key, which must have been instantiated with the same algorithm.

abstract  void

decryptReInit()

Re-initializes this object to decrypt using the key passed in during an earlier call to decryptInit().

 byte[]

decryptUpdate(byte[] partIn, int offset, int partInLen)

Decrypts the first partInLen bytes of the array partIn, beginning at offset.

abstract  int

decryptUpdate(byte[] partIn, int inOffset, int partInLen, byte[] partOut, int outOffset)

Decrypts the first partInLen bytes of the array partIn, beginning at inOffset.

 byte[]

encryptFinal()

This method finalizes the encryption process by encrypting any leftover bytes and padding (if padding is requested).

abstract  int

encryptFinal(byte[] partOut, int offset)

This method finalizes the encryption process by encrypting any leftover bytes and padding (if padding is requested).

 void

encryptInit(JSAFE_SecretKey key)

Initializes an object to perform encryption with key, which must be instantiated with the same algorithm.

abstract  void

encryptInit(JSAFE_SecretKey key, SecureRandom random)

Initializes this object to encrypt, using key and random.

abstract  void

encryptReInit()

Re-initializes this object to encrypt using the key passed in during an earlier call to the encryptInit() method.

 byte[]

encryptUpdate(byte[] partIn, int offset, int partInLen)

Encrypts the first partInLen bytes of the array partIn, beginning at offset.

abstract  int

encryptUpdate(byte[] partIn, int inOffset, int partInLen, byte[] partOut, int outOffset)

Encrypts the first partInLen bytes of the array partIn, beginning at inOffset.

 void

generateIV(SecureRandom random)

Generates an initialization vector using random.

 void

generateSalt(SecureRandom random)

Generates an 8-byte salt for PBE using random.

abstract  int[]

getAlgorithmParameters()

Returns a new int array containing the algorithm's parameters.

 JSAFE_SecretKey

getBlankKey()

Returns an empty JSAFE_SecretKey object to match the algorithm and device of this object.

 int

getBlockSize()

Returns the size, in bytes, of the blocks on which an object operates.

abstract  byte[]

getDERAlgorithmID()

Returns a new byte array containing the DER encoding of the algorithm ID of the algorithm in this object.

 String

getDevice()

Returns the name of the device of record.

 String[]

getDeviceList()

Returns a String array that describes all the devices used to execute the transformation.

 String

getDigestAlgorithm()

Returns the standard name of the digest algorithm used for PBE.

abstract  String

getEncryptionAlgorithm()

Returns the standard name of the encryption algorithm.

 String

getFeedbackMode()

Returns the standard feedback mode name.

 int[]

getFeedbackParameters()

Returns a new int array containing the feedback mode's parameters.

static JSAFE_SymmetricCipher

getInstance(byte[] berAlgID, int offset, String device)

Builds an object that performs an algorithm using the possible parameters defined in berAlgID, beginning at offset.

static JSAFE_SymmetricCipher

getInstance(String transformation, String device)

Builds an object that performs the transformation on the given device.

 byte[]

getIV()

Returns the initialization vector (IV) in a new byte array.

static int

getNextBEROffset(byte[] berAlgID, int offset)

Gets the next offset after the algorithm ID.

abstract  int

getOutputBufferSize(int inputLen)

Returns the maximum size (in bytes) that an output buffer should be to hold the results of a symmetric cipher processing (Update and Final) given the length of the input inputLen.

 int[]

getPaddingParameters()

Returns a new int array containing the padding scheme's parameters.

 String

getPaddingScheme()

Returns the standard padding scheme name.

 String

getPasswordStandard()

Returns the standard name of the PBE standard.

 int[]

getPBEParameters()

Returns a new int array containing the PBE parameters, which are generally the iteration count and key size.

 byte[]

getSalt()

Returns the salt in a new byte array.

abstract  void

setAlgorithmParameters(int[] params)

Sets this object to possess the algorithm parameters given.

 void

setIV(byte[] iv, int offset, int ivLen)

Sets the IV to the first ivLen bytes of iv, beginning at offset.

 void

setPBEParameters(int[] params)

Sets this object to possess the PBE parameters given.

 void

setSalt(byte[] newSalt, int offset, int saltLen)

Sets the salt value for PBE algorithms.

 JSAFE_PrivateKey

unwrapPrivateKey(byte[] wrappedKey, int offset, int keyLen, boolean pkcs8)

This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_PrivateKey.

abstract  JSAFE_PrivateKey

unwrapPrivateKey(byte[] wrappedKey, int offset, int keyLen, boolean pkcs8, String unwrappedKeyDevice)

This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_PrivateKey object, the device of the resulting key described by unwrappedKeyDevice.

 JSAFE_PublicKey

unwrapPublicKey(byte[] wrappedKey, int offset, int keyLen, boolean ber)

This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_PublicKey.

abstract  JSAFE_PublicKey

unwrapPublicKey(byte[] wrappedKey, int offset, int keyLen, boolean ber, String unwrappedKeyDevice)

This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_PublicKey object where the device of the resulting key is described by unwrappedKeyDevice.

 JSAFE_SecretKey

unwrapSecretKey(byte[] wrappedKey, int offset, int keyLen, boolean ber, String encryptedKeyAlgorithm)

This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_SecretKey.

abstract  JSAFE_SecretKey

unwrapSecretKey(byte[] wrappedKey, int offset, int keyLen, boolean ber, String encryptedKeyAlgorithm, String unwrappedKeyDevice)

This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_SecretKey object where the device of the resulting key is described by unwrappedKeyDevice.

 byte[]

wrapPrivateKey(JSAFE_PrivateKey keyToWrap, boolean pkcs8)

Encrypt the private key's data.

 byte[]

wrapPrivateKey(JSAFE_PrivateKey keyToWrap, boolean pkcs8, String format)

Encrypt the private key's data.

 byte[]

wrapPublicKey(JSAFE_PublicKey keyToWrap, boolean ber)

This method encrypts keyToWrap.

 byte[]

wrapSecretKey(JSAFE_SecretKey keyToWrap, boolean ber)

Encrypts keyToWrap.

 
Methods inherited from class com.rsa.jsafe.JSAFE_Object
clone, overwrite, overwrite
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSAFE_SymmetricCipher

public JSAFE_SymmetricCipher()
Method Detail

getInstance

public static JSAFE_SymmetricCipher getInstance(byte[] berAlgID,
                                                int offset,
                                                String device)
                                         throws JSAFE_UnimplementedException,
                                                JSAFE_InvalidParameterException,
                                                JSAFE_IVException
Builds an object that performs an algorithm using the possible parameters defined in berAlgID, beginning at offset. This is the BER encoding of the algorithm identifier. The specified device is used.

The value encoded in berAlgID must follow the definition in X.509:

   AlgorithmIdentifier ::= SEQUENCE {
     algorithm OBJECT IDENTIFIER,
     parameters ANY DEFINED BY algorithm OPTIONAL }
 

Parameters:
berAlgID - A byte array containing the BER encoding of an algorithm ID.
offset - The offset into berAlgID where the encoding begins.
device - A list of devices used to build the object.
Returns:
A new JSAFE_SymmetricCipher object that performs the transformation as described by the algorithm ID.
Throws:
JSAFE_UnimplementedException - If the device or devices cannot perform the designated algorithm, or the toolkit cannot decode the BER encoding.
JSAFE_InvalidParameterException - If the BER encoding contains parameters that do not work.
JSAFE_IVException - If the transformation needs an initialization vector (iv), but the BER encoding did not contain one.

getNextBEROffset

public static int getNextBEROffset(byte[] berAlgID,
                                   int offset)
                            throws JSAFE_UnimplementedException
Gets the next offset after the algorithm ID. Given the berAlgID, the BER encoding of the algorithm ID, beginning at offset, returns the index to the next byte in the array.

Parameters:
berAlgID - A byte array containing the BER encoding of an algorithm ID.
offset - The offset into berAlgID where the encoding begins.
Returns:
The index into berAlgID of the byte following the algorithm ID.
Throws:
JSAFE_UnimplementedException - If the toolkit cannot decode the BER encoding.

getInstance

public static JSAFE_SymmetricCipher getInstance(String transformation,
                                                String device)
                                         throws JSAFE_UnimplementedException,
                                                JSAFE_InvalidParameterException
Builds an object that performs the transformation on the given device. The transformation values are:

The algorithm value must be one of the following:
algorithm values Description
RC2-e Where e is effective key bits, and is an integer from 1 to 1024.
RC5-v-w-r Where v is the version number (currently, only 0x10 is supported), w is the word size in bits (an integer that is either 32 or 64), and r is the rounds (an integer from 0 to 255).
DES, 3DES_EDE Data Encryption Standard and Triple-DES EDE.
DESX DESX is an extension to DES. DESX is proprietary to RSA Security.
RC4 RC4 Stream Cipher.
AES General AES, implementation based on actual key size
AES128 AES with 128-bit key, can be initialized only with 128-bit AES key
AES192 AES with 192-bit key, can be initialized only with 192-bit AES key
AES256 AES with 256-bit key, can be initialized only with 256-bit AES key

Feedback Mode and Padding Schemes

The RC2 and RC5 ciphers, DES, and 3DES_EDE (Triple-DES EDE) are block ciphers that require a feedback mode and a padding scheme. The RC4 cipher is a stream cipher that does not let you specify feedback or padding. Possible feedback values are as follows:

Feedback Values Description
ECB Electronic Codebook
CBC Cipher Block Chaining
CFB-b Cipher Feedback
OFB-b Output Feedback

where b is the block size. It must be a multiple of eight and cannot be larger than the underlying encryption block size. If no block size is given, the default is the cipher's block size.

The Crypto-J nomenclature differs from nomenclature used in the documentation of the standard, as shown in the following table:
AlgorithmCrypto-J NameStandard Name
AES CFB CFB128
CFB-128 CFB128
CFB-8 CFB8
OFB OFB128
OFB-128 OFB128
OFB-8 OFB8
DES/TDES CFB CFB64
CFB-64 CFB64
CFB-8 CFB8
OFB OFB64
OFB-64 OFB64
OFB-8 OFB8

Possible padding schemes are as follows:
Padding Schemes Description
"NoPad" Do not use padding
"PKCS5Padding" Use PKCS #5 padding
"ISO10126Padding" Use ISO 10126 padding

Possible digest values are as follows:

 Note:  Cryptanalytic work has discovered a collision in MD2's internal compression
        function, and there is a chance that the attack on MD2 may be extended to the
        full hash function. RSA Security recommends not using MD2 except for
        backward compatibility.

        Another attack has been applied to the compression function on MD5, though
        this has yet to be extended to the full MD5. RSA Security recommends that
        before you use MD2 or MD5, you should consult the RSA Laboratories Web
        site at http://www.rsasecurity.com/rsalabs/ to be sure that their use is
        consistent with the latest information. One bulletin that discusses this issue is
        Recent Results for MD2, MD4, and MD5; it is available at http://
        www.rsasecurity.com/rsalabs/bulletins/.
 
The value of PBEStandard must be one of the following. Refer to PBEConverter.pdf for the correct usage of PBE. i is the iteration count and k refers to the key size. If the algorithm is Triple DES, k is the number of keys. For other algorithms, k is the number of bits.

RSA Security recommends using an iteration count of 1000 (one thousand). There is no real limit to this value: the number must be positive and the bigger the number, the slower the operation. Following is a list of valid key size values allowed by the encryption algorithms.

Note that PKCS #5 PBE specifies the number of bits for DES or the RC2 cipher at 64. See the Crypto-J Developer's Guide ("Overview of Cryptography") for more information on Password-Based Encryption.

The device value is as follows: "choice1[/choice2[...[/choicen]]]" where the choices for device value are as follows:

Crypto-J tries to instantiate a class using the first choice; if it cannot, it tries the other choices.
 Note:  Do not attempt to use hardware versions of Crypto-J classes unless you are
        very familiar with the hardware. Refer to the "Cryptographic Hardware" section
        in the Crypto-J Developer's Guide , which describes hardware usage,
        its benefits, and its problems.
 
Examples:

The following example demonstrates how to use DES in ECB mode with padding and a hardware accelerator, as shown below. Crypto-J tries to do it first using the fictional company Acme's hardware accelerator. If the Acme device is not attached (or is attached but cannot perform DES), Crypto-J tries to build an object that performs DES using native code. If a native version is not available, Crypto-J uses the Java version.

   JSAFE_SymmetricCipher des = JSAFE_SymmetricCipher.getInstance
       ("DES/ECB/NoPad", "Acme/Native/Java");
 
The following example demonstrates how to use the RC4 cipher with Native code, as shown below. If a native version is not available, Crypto-J throws an exception.
   JSAFE_SymmetricCipher rc4 =
       JSAFE_SymmetricCipher.getInstance (“RC4”, “Native”);
 
The following example demonstrates how to use PKCS #12 PBE with SHA1 and the RC2 cipher in CBC mode.
   JSAFE_SymmetricCipher rc2PBE = JSAFE_SymmetricCipher.getInstance
      ("PBE/SHA1/RC2/CBC/PKCS12PBE–5–128", "Java");
 

Parameters:
transformation - The representation of the desired operation (for example, "RC2-128/CBC/PKCS5Padding").
device - A list of devices to use when building the object (for example, "Java", "Native/Java").
Returns:
A new JSAFE_SymmetricCipher object that performs the transformation.
Throws:
JSAFE_UnimplementedException - If the device or devices cannot perform the designated algorithm.
JSAFE_InvalidParameterException - If the transformation contains parameters that do not work.

getDERAlgorithmID

public abstract byte[] getDERAlgorithmID()
                                  throws JSAFE_UnimplementedException
Returns a new byte array containing the DER encoding of the algorithm ID of the algorithm in this object. The PBE salt must be set before calling getDERAlgorithmID().

Returns:
A new byte array that contains the algorithm ID.
Throws:
JSAFE_UnimplementedException - If the algorithm has no OID, or there is not enough information to build the algorithm ID.

getDevice

public String getDevice()
Returns the name of the device of record. Possible device values are:

Returns:
The device name.

getDeviceList

public String[] getDeviceList()
Returns a String array that describes all the devices used to execute the transformation. Because a transformation often consists of component algorithms, the implementation can consist of several component Objects. A device can perform one or more of the algorithms, but it may let Java or another device perform one or more. For example, because padding is not time-consuming, a device might let the Crypto-J implementation perform padding. Returns the names of the devices used by each component.

Returns:
A String array that describes the device used for a specific component.

getEncryptionAlgorithm

public abstract String getEncryptionAlgorithm()
Returns the standard name of the encryption algorithm.

Returns:
A String array that describes the encryption algorithm.

getFeedbackMode

public String getFeedbackMode()
Returns the standard feedback mode name. If the underlying algorithm is a stream cipher, this method returns null.

Returns:
A String that describes the feedback mode.

getPaddingScheme

public String getPaddingScheme()
Returns the standard padding scheme name. If the underlying algorithm is a stream cipher, this method returns null.

Returns:
A String that describes the padding algorithm.

getDigestAlgorithm

public String getDigestAlgorithm()
Returns the standard name of the digest algorithm used for PBE. If an object is not set for PBE, it returns null.

Returns:
A String that describes the digest algorithm.

getPasswordStandard

public String getPasswordStandard()
Returns the standard name of the PBE standard. If an object is not set for PBE, it returns null.

Returns:
A String that describes the PBE standard.

getBlockSize

public int getBlockSize()
Returns the size, in bytes, of the blocks on which an object operates. This is meaningful only with block ciphers.

Returns:
An int that is the block size.

getBlankKey

public JSAFE_SecretKey getBlankKey()
Returns an empty JSAFE_SecretKey object to match the algorithm and device of this object. Key data can be set or generated with this object.

Returns:
A new, empty JSAFE_SecretKey object.

getAlgorithmParameters

public abstract int[] getAlgorithmParameters()
Returns a new int array containing the algorithm's parameters. For example, if the underlying algorithm is the RC2 algorithm, the return value is an array of length 1, with the only element being effective key bits. If the algorithm is the RC5 algorithm, the return value is an array of length 3: version, word size in bits, and rounds. If the underlying algorithm does not have parameters, the return value is a zero-element array. This may be an array of length 0.

Returns:
A new int array containing the parameters.

setAlgorithmParameters

public abstract void setAlgorithmParameters(int[] params)
                                     throws JSAFE_InvalidParameterException
Sets this object to possess the algorithm parameters given.

Parameters:
params - The algorithm parameters.
Throws:
JSAFE_InvalidParameterException - If the parameters given do not match the encryption algorithm.

getFeedbackParameters

public int[] getFeedbackParameters()
Returns a new int array containing the feedback mode's parameters. This may be an array of length 0.

Returns:
A new int array that contains the parameters.

getPaddingParameters

public int[] getPaddingParameters()
Returns a new int array containing the padding scheme's parameters. This may be an array of length 0.

Returns:
A new int array that contains the parameters of the padding scheme.

getPBEParameters

public int[] getPBEParameters()
Returns a new int array containing the PBE parameters, which are generally the iteration count and key size.

Returns:
A new int array that contains the PBE parameters.

setPBEParameters

public void setPBEParameters(int[] params)
                      throws JSAFE_InvalidParameterException
Sets this object to possess the PBE parameters given.

Parameters:
params - The PBE parameters. This is at most a two element int array. The first element is the iteration count. The second element is the key size and is optional.
Throws:
JSAFE_InvalidParameterException - If the parameters given do not match the encryption algorithm.

generateIV

public void generateIV(SecureRandom random)
                throws JSAFE_IVException,
                       JSAFE_InvalidUseException
Generates an initialization vector using random. This IV is stored internally and used after the next call to an Init method.

The class JSAFE_SecureRandom is a subclass of java.security.SecureRandom, so an object of that class is valid input for random.

Call getIV() to see the IV that is generated.

This method does not save the random object. It does not use a random object passed in during a previous call to generateIV(), generateSalt(), or Init. If null is passed in, this method throws an exception.

Parameters:
random - A SecureRandom object random bytes are drawn from.
Throws:
JSAFE_IVException - If the feedback mode rejects the IV.
JSAFE_InvalidUseException - If no random object is passed in.
See Also:
getIV(), setIV(byte[], int, int)

setIV

public void setIV(byte[] iv,
                  int offset,
                  int ivLen)
           throws JSAFE_IVException
Sets the IV to the first ivLen bytes of iv, beginning at offset. If an object already possesses an IV, this method replaces it. However, the new IV does not take effect until after the next Init, ReInit, or Final call. This means that this new IV is not used if the next method call is an Update.

The new IV must be the same length as the block size, or this method throws an exception. This method is meaningful only with block ciphers using a feedback mode (not ECB).

Parameters:
iv - The byte array containing the data.
offset - The offset into iv where the data begins.
ivLen - The length of the IV.
Throws:
JSAFE_IVException - If the IV given is not the appropriate length.
See Also:
generateIV(java.security.SecureRandom), getIV()

getIV

public byte[] getIV()
Returns the initialization vector (IV) in a new byte array. If the underlying algorithm does not use an IV (such as the RC4 cipher), or if the IV has not yet been set, this method returns null.

Returns:
A new byte array containing the IV.
See Also:
generateIV(java.security.SecureRandom), setIV(byte[], int, int)

generateSalt

public void generateSalt(SecureRandom random)
                  throws JSAFE_InvalidUseException
Generates an 8-byte salt for PBE using random. The class JSAFE_SecureRandom object is a subclass of java.security.SecureRandom, so an object of that class is valid input for random.

getSalt() can be called to retrieve the salt that is generated. This salt is stored internally and used after the next call to an Init method.

This method will not save the random object. It will also not use a random object passed in during a previous call to generateIV(), generateSalt(), or Init. If random is null, this method throws an exception.

Parameters:
random - A SecureRandom object random bytes are drawn from.
Throws:
JSAFE_InvalidUseException - If no random object is passed in.
See Also:
getSalt(), setSalt(byte[], int, int)

setSalt

public void setSalt(byte[] newSalt,
                    int offset,
                    int saltLen)
Sets the salt value for PBE algorithms. This method sets the PBE salt to the first saltLen bytes of salt, beginning at offset. If an object already possesses a salt, this method replaces it. However,the new salt does not take effect until after the next Init, ReInit, or Final call. This means that this new salt is not used if the next method call is an Update. This method is meaningful only when employing PBE.

Parameters:
newSalt - The byte array containing the data.
offset - The offset into newSalt where the data begins.
saltLen - The length of the salt.
See Also:
getSalt(), generateSalt(java.security.SecureRandom)

getSalt

public byte[] getSalt()
Returns the salt in a new byte array. If an object is not performing PBE, or if the salt has not been set yet, this method returns null.

Returns:
A new byte array containing the salt.
See Also:
setSalt(byte[], int, int), generateSalt(java.security.SecureRandom)

getOutputBufferSize

public abstract int getOutputBufferSize(int inputLen)
Returns the maximum size (in bytes) that an output buffer should be to hold the results of a symmetric cipher processing (Update and Final) given the length of the input inputLen. The computation includes the length of any unprocessed data. However, the actual output may be smaller than the reported value.

Parameters:
inputLen - The length of the input, in bytes.
Returns:
An int that indicates the size of the output.

encryptInit

public void encryptInit(JSAFE_SecretKey key)
                 throws JSAFE_InvalidUseException,
                        JSAFE_InvalidKeyException,
                        JSAFE_IVException
Initializes an object to perform encryption with key, which must be instantiated with the same algorithm.

If the encryption algorithm, feedback, or padding scheme require random bytes, this method throws an exception. (Currently, no symmetric encryption algorithm feedback or symmetric padding scheme supported by Crypto-J requires random bytes. However, if a future version contains an algorithm that needs a random object, encryptInit() throws an exception when trying to call Init.)

Parameters:
key - A JSAFE_SecretKey that is used to encrypt.
Throws:
JSAFE_InvalidUseException - If the object cannot be initialized.
JSAFE_InvalidKeyException - If the key object does not match this object (for example, if it is a different algorithm or different device).
JSAFE_IVException - If the algorithm needs an IV, and the one entered is inappropriate.

encryptInit

public abstract void encryptInit(JSAFE_SecretKey key,
                                 SecureRandom random)
                          throws JSAFE_InvalidUseException,
                                 JSAFE_InvalidKeyException,
                                 JSAFE_IVException
Initializes this object to encrypt, using key and random. If this object ever needs random bytes, encryptInit() uses random. If an algorithm does not need random bytes, random can have a null value or use the encryptInit() method that does not take a random object.

The JSAFE_SecureRandom is a subclass of java.security.SecureRandom, so an object of that class is valid input for random.

Parameters:
key - A JSAFE_SecretKey object that will be used to encrypt.
random - A SecureRandom from which this object draws random bytes if necessary.
Throws:
JSAFE_InvalidUseException - If the object cannot be initialized.
JSAFE_InvalidKeyException - If the key object does not match this object (for example, if it is a different algorithm or a different device).
JSAFE_IVException - If the algorithm needs an IV and the one entered is inappropriate.

encryptReInit

public abstract void encryptReInit()
                            throws JSAFE_InvalidUseException,
                                   JSAFE_IVException
Re-initializes this object to encrypt using the key passed in during an earlier call to the encryptInit() method.

This method cannot be called when the RC4 cipher is used.

Throws:
JSAFE_InvalidUseException - If the object is not initialized for encryption.
JSAFE_IVException - If the algorithm needs an initialization vector (IV), and the one entered is inappropriate.

encryptUpdate

public byte[] encryptUpdate(byte[] partIn,
                            int offset,
                            int partInLen)
                     throws JSAFE_InvalidUseException,
                            JSAFE_IVException
Encrypts the first partInLen bytes of the array partIn, beginning at offset. It returns the result in a new byte array. The result may be a zero-length array. If there is not enough data to form a block, encryptUpdate() will not encrypt. In that case, it stores the input in an internal buffer.

Parameters:
partIn - The data to encrypt.
offset - The offset into partIn where the data begins.
partInLen - The length of the data to encrypt.
Returns:
A new byte array containing the encrypted data. This may be an array of length 0 if the object could not encrypt yet.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to encrypt.
JSAFE_IVException - If the object needs an initialization vector (IV), and does not have one.

encryptUpdate

public abstract int encryptUpdate(byte[] partIn,
                                  int inOffset,
                                  int partInLen,
                                  byte[] partOut,
                                  int outOffset)
                           throws JSAFE_InvalidUseException,
                                  JSAFE_IVException
Encrypts the first partInLen bytes of the array partIn, beginning at inOffset. It returns the result in the byte array partOut, beginning at outOffset. The return value is the number of bytes placed in the output buffer.

This method does not encrypt if there is not enough data to form a block. In that case, it stores the input in an internal buffer.

Parameters:
partIn - The data to encrypt.
inOffset - The offset into partIn where the data begins.
partInLen - The length of the data to encrypt.
partOut - The buffer where the output is placed.
outOffset - The offset into partOut where the writing begins.
Returns:
The number of bytes placed into the output buffer.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to encrypt.
JSAFE_IVException - If the object needs an initialization vector (IV), and does not have one.

wrapPrivateKey

public byte[] wrapPrivateKey(JSAFE_PrivateKey keyToWrap,
                             boolean pkcs8)
                      throws JSAFE_InvalidUseException
Encrypt the private key's data. To get a BER encoding of the wrapped key following PKCS #8, pass in true for the argument pkcs8. This method encrypts keyToWrap. The encrypted key is the BER encoding of the private key (PrivateKeyInfo, as defined in PKCS #8).

encryptInit() or encryptReInit() must be called prior to calling this method.

If the argument pkcs8 is false, the result is the key data encrypted. If the argument pkcs8 is true, the result is returned in the PKCS #8 EncryptedPrivateKeyInfo standard definition format, as defined below:

 EncryptedPrivateKeyInfo ::= SEQUENCE {
   encryptionAlgorithm EncryptionAlgorithmIdentifier,
   encryptedData EncryptedData }

 EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier

 EncryptedData ::= OCTET STRING
 

Parameters:
keyToWrap - The object containing the key to encrypt.
pkcs8 - Pass in true to get a result that is BER encoded following PKCS #8, false to get only the encrypted data.
Returns:
A new byte array containing the encrypted key.
Throws:
JSAFE_InvalidUseException - If the object has not been initialized to encrypt.

wrapPrivateKey

public byte[] wrapPrivateKey(JSAFE_PrivateKey keyToWrap,
                             boolean pkcs8,
                             String format)
                      throws JSAFE_InvalidUseException
Encrypt the private key's data. To get a BER encoding of the wrapped key following PKCS #8, pass in true for the argument pkcs8. This method encrypts keyToWrap. The encrypted key is the BER encoding of the private key (PrivateKeyInfo, as defined in PKCS #8).

encryptInit or encryptReInit must be called prior to calling this method.

If the argument pkcs8 is false, the result is the key data encrypted. If the argument pkcs8 is true, the result is returned in the PKCS #8 EncryptedPrivateKeyInfo standard definition format, as defined below:

 EncryptedPrivateKeyInfo ::= SEQUENCE {
   encryptionAlgorithm EncryptionAlgorithmIdentifier,
   encryptedData EncryptedData }

 EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier

 EncryptedData ::= OCTET STRING
 

Parameters:
keyToWrap - The object containing the key to encrypt.
pkcs8 - true to get a result that is BER encoded following PKCS #8, false to get only the encrypted data.
format - The key data format.
Returns:
A new byte array containing the encrypted key.
Throws:
JSAFE_InvalidUseException - If the object has not been initialized to encrypt.

wrapPublicKey

public byte[] wrapPublicKey(JSAFE_PublicKey keyToWrap,
                            boolean ber)
                     throws JSAFE_InvalidUseException
This method encrypts keyToWrap. The encrypted key is the BER encoding of the public key (SubjectPublicKeyInfo, as defined in X.509).

encryptInit() or encryptReInit() must be called prior to calling this method.

If the argument ber is false, the result is the key data encrypted. If the argument ber is true, the result is returned as a BER-encoded encryptedPublicKey (proprietary to RSA Security) as defined below:

 EncryptedPublicKey ::=SEQUENCE {
   encryptionAlgorithm EncryptionAlgorithmIdentifier,
   encryptedData EncryptedData }

 EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier

 EncryptedData ::= OCTET STRING
 

Parameters:
keyToWrap - The object containing the key to encrypt.
ber - true to get a result that is BER encoded, false to get only the encrypted data.
Returns:
A new byte array containing the encrypted key.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to encrypt.

wrapSecretKey

public byte[] wrapSecretKey(JSAFE_SecretKey keyToWrap,
                            boolean ber)
                     throws JSAFE_InvalidUseException
Encrypts keyToWrap. Call encryptInit() or encryptReInit() before calling this method.

If the argument ber is false, the result is the key data encrypted. If the argument ber is true, the result is the BER encoding of the secret key SecretKeyInfo (proprietary to RSA Security) as defined below:

 SecretKeyInfo ::=SEQUENCE {
   secretKeyAlgorithm AlgorithmIdentifier
   secretKey OCTET STRING }
 
 EncryptedSecretKey ::=SEQUENCE {
   encryptionAlgorithm EncryptionAlgorithmIdentifier
   encryptedData EncryptedData }

 EncryptionAlgorithmIdentifier ::=AlgorithmIdentifier

 EncryptedData ::= OCTET STRING
 

Parameters:
keyToWrap - The object containing the key to encrypt.
ber - true to get a result that is BER encoded, false to get only the encrypted data.
Returns:
A new byte array containing the encrypted key.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to encrypt.

encryptFinal

public byte[] encryptFinal()
                    throws JSAFE_InvalidUseException,
                           JSAFE_InputException,
                           JSAFE_IVException,
                           JSAFE_PaddingException
This method finalizes the encryption process by encrypting any leftover bytes and padding (if padding is requested). This method returns the result in a new byte array. Call a Final method before reusing the object to encrypt with a new key.

Returns:
A new byte array that contains the encrypted data.
Throws:
JSAFE_InvalidUseException - If the object has not been initialized to encrypt.
JSAFE_InputException - If the input is not an appropriate length (for example, an unpadded block cipher with input that is not a multiple of the block size).
JSAFE_IVException - If the algorithm needs an initialization vector (IV) and does not have one.
JSAFE_PaddingException - If the padding algorithm cannot execute.

encryptFinal

public abstract int encryptFinal(byte[] partOut,
                                 int offset)
                          throws JSAFE_InvalidUseException,
                                 JSAFE_InputException,
                                 JSAFE_IVException,
                                 JSAFE_PaddingException
This method finalizes the encryption process by encrypting any leftover bytes and padding (if padding is requested). It returns the result in the byte array partOut, beginning at offset. The return value is the number of bytes actually placed in the output buffer. A Final method must be called before reusing the object to encrypt with a new key. The return value is the number of bytes actually placed in the output buffer.

Parameters:
partOut - The buffer where the output is placed.
offset - The offset into partOut where the writing begins.
Returns:
The number of bytes placed into the output buffer.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to encrypt.
JSAFE_InputException - If the input is not an appropriate length (for example, an unpadded block cipher with input that is not a multiple of the block size).
JSAFE_IVException - If the algorithm needs an IV, and does not have one.
JSAFE_PaddingException - If the padding algorithm cannot execute.

decryptInit

public void decryptInit(JSAFE_SecretKey key)
                 throws JSAFE_InvalidUseException,
                        JSAFE_InvalidKeyException,
                        JSAFE_IVException
Initializes this object to decrypt using key, which must be instantiated with the same algorithm. If the decryption algorithm, feedback, or padding require random bytes, this method throws an exception. (Currently, neither symmetric decryption algorithm feedback nor symmetric padding schemes supported by Crypto-J require random bytes. However, if a future version contains an algorithm that needs a random object, decryptInit() throws an exception when trying to call Init.)

Parameters:
key - A JSAFE_SecretKey object that is used to decrypt.
Throws:
JSAFE_InvalidUseException - If the object cannot be initialized.
JSAFE_InvalidKeyException - If the key object does not match this object (for example, if it is a different algorithm or different device).
JSAFE_IVException - If the algorithm needs an IV, and the one entered is inappropriate.

decryptInit

public abstract void decryptInit(JSAFE_SecretKey key,
                                 SecureRandom random)
                          throws JSAFE_InvalidUseException,
                                 JSAFE_InvalidKeyException,
                                 JSAFE_IVException
This method initializes an object to perform decryption with key, which must have been instantiated with the same algorithm. If the decryption algorithm, feedback, or padding scheme needs to obtain random bytes, an object gets them from random. If an algorithm does not need random bytes, either pass in a null value or use the decryptInit() method that does not take a random object.

Parameters:
key - A JSAFE_SecretKey object that is used to decrypt.
random - A SecureRandom where random bytes are drawn from, if necessary. The JSAFE_SecureRandom object is a subclass of the SecureRandom object, so it is valid input.
Throws:
JSAFE_InvalidUseException - If the object cannot be initialized.
JSAFE_InvalidKeyException - If the key object does not match this object (for example, if it is a different algorithm or a different device).
JSAFE_IVException - If the algorithm needs an IV, and the one entered is inappropriate.

decryptReInit

public abstract void decryptReInit()
                            throws JSAFE_InvalidUseException,
                                   JSAFE_IVException
Re-initializes this object to decrypt using the key passed in during an earlier call to decryptInit().

This method cannot be called when RC4 is being used.

Throws:
JSAFE_InvalidUseException - If the object is not initialized for decryption.
JSAFE_IVException - If the algorithm needs an IV, and the one entered is inappropriate.

unwrapPrivateKey

public JSAFE_PrivateKey unwrapPrivateKey(byte[] wrappedKey,
                                         int offset,
                                         int keyLen,
                                         boolean pkcs8)
                                  throws JSAFE_InvalidUseException
This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_PrivateKey.

If the argument pkcs8 is false, pass in the encrypted key data. If the argument pkcs8 is true, this method expects the data to be in the form of PKCS #8 EncryptedPrivateKeyInfo standard definition format, as defined below:

 EncryptedPrivateKeyInfo ::= SEQUENCE {
   encryptionAlgorithm EncryptionAlgorithmIdentifier,
   encryptedData EncryptedData }

 EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier

 EncryptedData ::= OCTET STRING
 
Call decryptInit() or decryptReInit() before calling this method.

Parameters:
wrappedKey - The encrypted key data.
offset - The offset into wrappedKey where the data begins.
keyLen - The length of the encrypted data.
pkcs8 - true if the key is encrypted following PKCS #8, false otherwise.
Returns:
A new JSAFE_PrivateKey object built using the decrypted data.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to decrypt.

unwrapPrivateKey

public abstract JSAFE_PrivateKey unwrapPrivateKey(byte[] wrappedKey,
                                                  int offset,
                                                  int keyLen,
                                                  boolean pkcs8,
                                                  String unwrappedKeyDevice)
                                           throws JSAFE_InvalidUseException
This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_PrivateKey object, the device of the resulting key described by unwrappedKeyDevice.

If the argument pkcs8 is false, pass in the encrypted key data. If the argument pkcs8 is true, this method expects the data to be in the form of PKCS #8 EncryptedPrivateKeyInfo standard definition format, as defined below:

 EncryptedPrivateKeyInfo ::= SEQUENCE {
   encryptionAlgorithm EncryptionAlgorithmIdentifier,
   encryptedData EncryptedData }

 EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier

 EncryptedData ::= OCTET STRING
 
Call decryptInit() or decryptReInit() before calling this method.

Parameters:
wrappedKey - The encrypted key data.
offset - The offset into wrappedKey where the data begins.
keyLen - The length of the encrypted data.
pkcs8 - true if the key is encrypted following PKCS #8, false otherwise.
unwrappedKeyDevice - Specifies the device for the resulting private key.
Returns:
A new JSAFE_PrivateKey object built using the decrypted data.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to decrypt.

unwrapPublicKey

public JSAFE_PublicKey unwrapPublicKey(byte[] wrappedKey,
                                       int offset,
                                       int keyLen,
                                       boolean ber)
                                throws JSAFE_InvalidUseException
This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_PublicKey.

If the argument ber is false, pass in the encrypted key data. If the argument ber is true, this method expects the data to be in the form of an EncryptedPublicKey(proprietary to RSA Security) as defined below:

 EncryptedPublicKey ::=SEQUENCE {
   encryptionAlgorithm EncryptionAlgorithmIdentifier,
   encryptedData EncryptedData }

 EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier

 EncryptedData ::= OCTET STRING
 
Call decryptInit() or decryptReInit() before calling this method.

Parameters:
wrappedKey - The encrypted key data.
offset - The offset into wrappedKey where the data begins.
keyLen - The length of the encrypted data.
ber - true if the key is encrypted BER encoded, false otherwise.
Returns:
A new JSAFE_PublicKey object built using the decrypted data.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to decrypt.

unwrapPublicKey

public abstract JSAFE_PublicKey unwrapPublicKey(byte[] wrappedKey,
                                                int offset,
                                                int keyLen,
                                                boolean ber,
                                                String unwrappedKeyDevice)
                                         throws JSAFE_InvalidUseException
This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_PublicKey object where the device of the resulting key is described by unwrappedKeyDevice.

If the argument ber is false, pass in the encrypted key data. If the argument ber is true, this method expects the data to be in the form of an EncryptedPublicKey (proprietary to RSA Security) as defined below:

 EncryptedPublicKey ::=SEQUENCE {
   encryptionAlgorithm EncryptionAlgorithmIdentifier,
   encryptedData EncryptedData }

 EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier

 EncryptedData ::= OCTET STRING
 
Call decryptInit() or decryptReInit() before calling this method.

Parameters:
wrappedKey - The encrypted key data.
offset - The offset into wrappedKey where the data begins.
keyLen - The length of the encrypted data.
ber - true if the key is encrypted BER encoded, false otherwise.
unwrappedKeyDevice - Specifies the device for the resulting public key.
Returns:
A new JSAFE_PublicKey object built using the decrypted data.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to decrypt.

unwrapSecretKey

public JSAFE_SecretKey unwrapSecretKey(byte[] wrappedKey,
                                       int offset,
                                       int keyLen,
                                       boolean ber,
                                       String encryptedKeyAlgorithm)
                                throws JSAFE_InvalidUseException
This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_SecretKey.

If the argument ber is false, pass in the encrypted key's algorithm in encryptedKeyAlgorithm. If the argument ber is true, this method expects the data to be in the form of an EncryptedSecretKey (proprietary to RSA Security) as defined below, and the method ignores the argument encryptedKeyAlgorithm:

 SecretKeyInfo ::=SEQUENCE {
   secretKeyAlgorithm AlgorithmIdentifier
   secretKey OCTET STRING }
 
 EncryptedSecretKey ::=SEQUENCE {
   encryptionAlgorithm EncryptionAlgorithmIdentifier
   encryptedData EncryptedData }

 EncryptionAlgorithmIdentifier ::=AlgorithmIdentifier

 EncryptedData ::= OCTET STRING
 
Call decryptInit() or decryptReInit() before calling this method.

Parameters:
wrappedKey - The encrypted key data.
offset - The offset into wrappedKey where the data begins.
keyLen - The length of the encrypted data.
ber - true if the key is encrypted BER encoded, false otherwise.
encryptedKeyAlgorithm - If ber is false, use this parameter to specify which algorithm the key is for (for example, "DES", "RC4").
Returns:
A new JSAFE_SecretKey object built using the decrypted data.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to decrypt.

unwrapSecretKey

public abstract JSAFE_SecretKey unwrapSecretKey(byte[] wrappedKey,
                                                int offset,
                                                int keyLen,
                                                boolean ber,
                                                String encryptedKeyAlgorithm,
                                                String unwrappedKeyDevice)
                                         throws JSAFE_InvalidUseException
This method decrypts wrappedKey (of length keyLen, beginning at offset) and returns the result as a new JSAFE_SecretKey object where the device of the resulting key is described by unwrappedKeyDevice.

If the argument ber is false, pass in the encrypted key's algorithm in encryptedKeyAlgorithm. If the argument for ber is true, this method expects the data to be in the form of an EncryptedSecretKey (proprietary to RSA Security) as defined below, and the method ignores the argument encryptedKeyAlgorithm:

 SecretKeyInfo ::=SEQUENCE {
   secretKeyAlgorithm AlgorithmIdentifier
   secretKey OCTET STRING }
 
 EncryptedSecretKey ::=SEQUENCE {
   encryptionAlgorithm EncryptionAlgorithmIdentifier
   encryptedData EncryptedData }

 EncryptionAlgorithmIdentifier ::=AlgorithmIdentifier

 EncryptedData ::= OCTET STRING
 
Call decryptInit() or decryptReInit() before calling this method.

Parameters:
wrappedKey - The encrypted key data.
offset - The offset into wrappedKey where the data begins.
keyLen - The length of the encrypted data.
ber - true if the key was encrypted BER encoded, false otherwise.
encryptedKeyAlgorithm - If ber is false, use this parameter to specify which algorithm the key is for (for example, "DES", "RC4").
unwrappedKeyDevice - Specifies the device for the resulting secret key.
Returns:
A new JSAFE_SecretKey object built using the decrypted data.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to decrypt.

decryptUpdate

public byte[] decryptUpdate(byte[] partIn,
                            int offset,
                            int partInLen)
                     throws JSAFE_InvalidUseException,
                            JSAFE_IVException
Decrypts the first partInLen bytes of the array partIn, beginning at offset. Returns the result in a new byte array. The result may be a zero-length array. It will not decrypt if there is not enough data to form a block. In that case, it stores the input in an internal buffer.

Parameters:
partIn - The data to decrypt.
offset - The offset into partIn where the data begins.
partInLen - The length of the data to decrypt.
Returns:
A new byte array containing the decrypted data. This may be an array of length 0 if the object could not decrypt yet.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to decrypt.
JSAFE_IVException - If the object needs an IV, and does not have one.

decryptUpdate

public abstract int decryptUpdate(byte[] partIn,
                                  int inOffset,
                                  int partInLen,
                                  byte[] partOut,
                                  int outOffset)
                           throws JSAFE_InvalidUseException,
                                  JSAFE_IVException
Decrypts the first partInLen bytes of the array partIn, beginning at inOffset. It places the result in the byte array partOut, beginning at outOffset. The return value is the number of bytes placed in the output buffer. It will not decrypt if there is not enough data to form a block. In that case, it stores the input in an internal buffer.

Parameters:
partIn - The data to decrypt.
inOffset - The offset into partIn where the data begins.
partInLen - The length of the data to decrypt.
partOut - The buffer where the output is placed.
outOffset - The offset into partOut where the writing begins.
Returns:
The number of bytes placed into the output buffer.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to decrypt.
JSAFE_IVException - If the object needs an initialization vector (IV), and does not have one.

decryptFinal

public byte[] decryptFinal()
                    throws JSAFE_InvalidUseException,
                           JSAFE_InputException,
                           JSAFE_IVException,
                           JSAFE_PaddingException
Finalizes the decryption process and strips any padding. This method returns the result (if any) in a new byte array. If there are no output bytes, returns a zero-length array. Call a Final method before reusing the object to decrypt with a new key.

Returns:
A new byte array containing the decrypted data.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to decrypt.
JSAFE_InputException - If the input is not an appropriate length.
JSAFE_IVException - If the algorithm needs an initialization vector (IV) and does not have one.
JSAFE_PaddingException - If the padding algorithm cannot execute.

decryptFinal

public abstract int decryptFinal(byte[] partOut,
                                 int offset)
                          throws JSAFE_InvalidUseException,
                                 JSAFE_InputException,
                                 JSAFE_IVException,
                                 JSAFE_PaddingException
Finalizes the decryption process and strips any padding. It returns the result (if any) into the byte array partOut, beginning at offset. The return value is the number of bytes that are actually placed in the output buffer. Call a Final method before reusing the object to decrypt with a new key.

Parameters:
partOut - The buffer where the output is placed.
offset - The offset into partOut where the writing begins.
Returns:
The number of bytes placed into the output buffer.
Throws:
JSAFE_InvalidUseException - If the object is not initialized to decrypt.
JSAFE_InputException - If the input is not an appropriate length.
JSAFE_IVException - If the algorithm needs an initialization vector (IV) and does not have one.
JSAFE_PaddingException - If the padding algorithm cannot execute.

clearSensitiveData

public void clearSensitiveData()
This method clears sensitive data from an object. Although the finalizer clears the data, there is no guarantee the garbage collector will quickly call the finalizer. Allows a user to clear data as soon as possible. After calling clearSensitiveData(), an Init (not a ReInit) method is called to perform other operations with the object.



RSA BSAFE Crypto-J 3.5.2