Class JCEHandler
java.lang.Object
org.jpos.security.jceadapter.JCEHandler
Provides some higher level methods that are needed by the JCE Security Module, yet they are generic and can be used elsewhere.
It depends on the JavaTM Cryptography Extension (JCE).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classClass used for indexing MAC algorithms in cache -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbyte[]decryptData(byte[] encryptedData, Key key) Decrypts data using ECB mode.byte[]decryptDataCBC(byte[] encryptedData, Key key, byte[] iv) Decrypts data using CBC mode with the given IV.decryptDESKey(short keyLength, byte[] encryptedDESKey, Key encryptingKey, boolean checkParity) Decrypts an encrypted DES/Triple-DES keybyte[]encryptData(byte[] data, Key key) Encrypts data using ECB mode.byte[]encryptDataCBC(byte[] data, Key key, byte[] iv) Encrypts data using CBC mode with the given IV.byte[]encryptDESKey(short keyLength, Key clearDESKey, Key encryptingKey) Encrypts (wraps) a clear DES Key, it also sets odd parity before encryptionprotected byte[]extractDESKeyMaterial(short keyLength, Key clearDESKey) Extracts the DES/DESede key materialprotected KeyformDESKey(short keyLength, byte[] clearKeyBytes) Forms the clear DES key given its "RAW" encoded bytes Does the inverse of extractDESKeyMaterialgenerateDESKey(short keyLength) Generates a clear DES (DESede) keybyte[]generateMAC(byte[] data, Key kd, String macAlgorithm) Generates MAC (Message Message Authentication Code) for some data.
-
Constructor Details
-
JCEHandler
public JCEHandler()Default constructor; no instance state to initialise.
-
-
Method Details
-
generateDESKey
Generates a clear DES (DESede) key- Parameters:
keyLength- the bit length (key size) of the generated key (LENGTH_DES, LENGTH_DES3_2KEY or LENGTH_DES3_3KEY)- Returns:
- generated clear DES (or DESede) key
- Throws:
JCEHandlerException- if the underlying JCE provider cannot generate the key
-
encryptDESKey
public byte[] encryptDESKey(short keyLength, Key clearDESKey, Key encryptingKey) throws JCEHandlerException Encrypts (wraps) a clear DES Key, it also sets odd parity before encryption- Parameters:
keyLength- bit length (key size) of the clear DES key (LENGTH_DES, LENGTH_DES3_2KEY or LENGTH_DES3_3KEY)clearDESKey- DES/Triple-DES key whose format is "RAW" (for a DESede with 2 Keys, keyLength = 128 bits, while DESede key with 3 keys keyLength = 192 bits)encryptingKey- can be a key of any type (RSA, DES, DESede...)- Returns:
- encrypted DES key
- Throws:
JCEHandlerException- if key extraction or encryption fails
-
extractDESKeyMaterial
Extracts the DES/DESede key material- Parameters:
keyLength- bit length (key size) of the DES key. (LENGTH_DES, LENGTH_DES3_2KEY or LENGTH_DES3_3KEY)clearDESKey- DES/Triple-DES key whose format is "RAW"- Returns:
- encoded key material
- Throws:
JCEHandlerException- if the key format/algorithm is unsupported
-
decryptDESKey
public Key decryptDESKey(short keyLength, byte[] encryptedDESKey, Key encryptingKey, boolean checkParity) throws JCEHandlerException Decrypts an encrypted DES/Triple-DES key- Parameters:
keyLength- bit length (key size) of the DES key to be decrypted. (LENGTH_DES, LENGTH_DES3_2KEY or LENGTH_DES3_3KEY)encryptedDESKey- the byte[] representing the encrypted keyencryptingKey- can be of any algorithm (RSA, DES, DESede...)checkParity- if true, the parity of the key is checked- Returns:
- clear DES (DESede) Key
- Throws:
JCEHandlerException- if checkParity==true and the key does not have correct parity
-
formDESKey
Forms the clear DES key given its "RAW" encoded bytes Does the inverse of extractDESKeyMaterial- Parameters:
keyLength- bit length (key size) of the DES key. (LENGTH_DES, LENGTH_DES3_2KEY or LENGTH_DES3_3KEY)clearKeyBytes- the RAW DES/Triple-DES key- Returns:
- clear key
- Throws:
JCEHandlerException- ifkeyLengthis not a supported DES key size
-
encryptData
Encrypts data using ECB mode.- Parameters:
data- plaintext to encryptkey- cipher key- Returns:
- encrypted data
- Throws:
JCEHandlerException- if the JCE cipher operation fails
-
decryptData
Decrypts data using ECB mode.- Parameters:
encryptedData- ciphertext to decryptkey- cipher key- Returns:
- clear data
- Throws:
JCEHandlerException- if the JCE cipher operation fails
-
encryptDataCBC
Encrypts data using CBC mode with the given IV.- Parameters:
data- plaintext to encryptkey- cipher keyiv- 8 bytes initial vector- Returns:
- encrypted data
- Throws:
JCEHandlerException- if the JCE cipher operation fails
-
decryptDataCBC
Decrypts data using CBC mode with the given IV.- Parameters:
encryptedData- ciphertext to decryptkey- cipher keyiv- 8 bytes initial vector- Returns:
- clear data
- Throws:
JCEHandlerException- if the JCE cipher operation fails
-
generateMAC
Generates MAC (Message Message Authentication Code) for some data.- Parameters:
data- the data to be MACedkd- the key used for MACingmacAlgorithm- MAC algorithm name suitable forMac.getInstance(String)- Returns:
- the MAC
- Throws:
JCEHandlerException- if the MAC algorithm cannot be assigned or evaluated
-