...
The Crypto Data Format integrates the Java Cryptographic Extension into Camel, allowing simple and flexible encryption and decryption of messages using Camel's familiar marshall and unmarshal formatting mechanism. It assumes marshalling to mean encryption to cyphertext and unmarshalling to mean decryption back to the original plaintext. This data format implements only symmetric (shared-key) encryption and decyption.
Options
Wiki Markup |
{div:class=confluenceTableSmall}
|| Name || Type || Default || Description ||
| {{algorithm}} | {{String}} | {{DES/CBC/PKCS5Padding}} | The JCE algorithm name indicating the cryptographic algorithm that will be used. |
| {{algorithmParameterSpec}} | {{java.security.spec.AlgorithmParameterSpec}} | {{null}} |A JCE AlgorithmParameterSpec used to initialize the Cipher. |
| {{bufferSize}} | {{Integer}} | {{2048}} | the size of the buffer used in the signature process. |
| {{cryptoProvider}} | {{String}} | {{null}} | The name of the JCE Security Provider that should be used. |
| {{initializationVector}} | {{byte[]}} | {{null}} | A byte array containing the Initialization Vector that will be used to initialize the Cipher. |
| {{inline}} | {{boolean}} | {{false}} | Flag indicating that the configured IV should be inlined into the encrypted data stream. |
| {{macAlgorithm}} | {{String}} | {{null}} | The JCE algorithm name indicating the Message Authentication algorithm. |
| {{shouldAppendHMAC}} | {{boolean}} | {{null}} | Flag indicating that a Message Authentication Code should be calculated and appended to the encrypted data.|
{div}
|
Basic Usage
At its most basic all that is required to encrypt/decrypt an exchange is a shared secret key. If one or more instances of the Crypto data format are configured with this key the format can be used to encrypt the payload in one route (or part of one) and decrypted in another. For example, using the Java DSL as follows:
...
PGPDataFormat Options
Wiki Markup |
{div:class=confluenceTableSmall}
|| Name || Type || Default || Description ||
| {{keyUserid}} | {{String}} | {{null}} | The user ID of the key in the PGP keyring used during encryption. See also option {{keyUserids}}. Can also be only a part of a user ID. For example, if the user ID is "Test User <t...@camel.com>" then you can use the part "Test User" or "<t...@camel.com>" to address the user ID. |
| {{keyUserids}} | {{List<String>}} | {{null}} | *Since camel 2.12.2*: PGP allows to encrypt the symmetric key by several asymmetric public receiver keys. You can specify here the User IDs or parts of User IDs of several public keys contained in the PGP keyring. If you just have one User ID, then you can also use the option {{keyUserid}}. The User ID specified in {{keyUserid}} and the User IDs in {{keyUserids}} will be merged together and the corresponding public keys will be used for the encryption. |
| {{password}} | {{String}} | {{null}} | Password used when opening the private key (not used for encryption). |
| {{keyFileName}} | {{String}} | {{null}} | Filename of the keyring; must be accessible as a classpath resource (but you can specify a location in the file system by using the "file:" prefix). |
| {{encryptionKeyRing}} | {{byte\[\]}} | {{null}} | *Since camel 2.12.1*; encryption keyring; you can not set the keyFileName and encryptionKeyRing at the same time. |
| {{signatureKeyUserid}} | {{String}} | {{null}} | *Since Camel 2.11.0*; optional User ID of the key in the PGP keyring used for signing (during encryption) or signature verification (during decryption). During the signature verification process the specified User ID restricts the public keys from the public keyring which can be used for the verification. If no User ID is specified for the signature verficiation then any public key in the public keyring can be used for the verification. Can also be only a part of a user ID. For example, if the user ID is "Test User <t...@camel.com>" then you can use the part "Test User" or "<t...@camel.com>" to address the User ID. |
| {{signatureKeyUserids}} | {{List<String>}} | {{null}} | *Since Camel 2.12.3*; optional list of User IDs of the key in the PGP keyring used for signing (during encryption) or signature verification (during decryption). You can specify here the User IDs or parts of User IDs of several keys contained in the PGP keyring. If you just have one User ID, then you can also use the option {{keyUserid}}. The User ID specified in {{keyUserid}} and the User IDs in {{keyUserids}} will be merged together and the corresponding keys will be used for the signing or signature verification. If the specified User IDs reference several keys then for each key a signature is added to the PGP result during the encryption-signing process. In the decryption-verifying process the list of User IDs restricts the list of public keys which can be used for signature verification. If the list of User IDs is empty then any public key in the public keyring can be used for the signature verification. |
| {{signaturePassword}} | {{String}} | {{null}} | *Since Camel 2.11.0*; optional password used when opening the private key used for signing (during encryption). |
| {{signatureKeyFileName}} | {{String}} | {{null}} | *Since Camel 2.11.0*; optional filename of the keyring to use for signing (during encryption) or for signature verification (during decryption); must be accessible as a classpath resource (but you can specify a location in the file system by using the "file:" prefix). |
| {{signatureKeyRing}} | {{byte\[\]}} | {{null}} | *Since camel 2.12.1*; signature keyring; you can not set the signatureKeyFileName and signatureKeyRing at the same time. |
| {{algorithm}} | {{int}} | {{SymmetricKeyAlgorithmTags.CAST5}} | *Since camel 2.12.2*; symmetric key encryption algorithm; possible values are defined in {{org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags}}; for example 2 (= TRIPLE DES), 3 (= CAST5), 4 (= BLOWFISH), 6 (= DES), 7 (= AES_128). Only relevant for encrypting. |
| {{compressionAlgorithm}} | {{int}} | {{CompressionAlgorithmTags.ZIP}} | *Since camel 2.12.2*; compression algorithm; possible values are defined in {{org.bouncycastle.bcpg.CompressionAlgorithmTags}}; for example 0 (= UNCOMPRESSED), 1 (= ZIP), 2 (= ZLIB), 3 (= BZIP2). Only relevant for encrypting. |
| {{hashAlgorithm}} | {{int}} | {{HashAlgorithmTags.SHA1}} | *Since camel 2.12.2*: signature hash algorithm; possible values are defined in {{org.bouncycastle.bcpg.HashAlgorithmTags}}; for example 2 (= SHA1), 8 (= SHA256), 9 (= SHA384), 10 (= SHA512), 11 (=SHA224). Only relevant for signing. |
| {{armored}} | {{boolean}} | {{false}} | This option will cause PGP to base64 encode the encrypted text, making it available for copy/paste, etc. |
| {{integrity}} | {{boolean}} | {{true}} | Adds an integrity check/sign into the encryption file. |
| {{passphraseAccessor}} | [PGPPassphraseAccessor|https://github.com/apache/camel/blob/master/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPPassphraseAccessor.java] | {{null}} | *Since Camel 2.12.2*; provides passphrases corresponding to user Ids. If no passpharase can be found from the option {{password}} or {{signaturePassword}} and from the headers {{CamelPGPDataFormatKeyPassword}} or {{CamelPGPDataFormatSignatureKeyPassword}} then the passphrase is fetechedfetched from the passphrase accessor. You provide a bean which implements the interface [PGPPassphraseAccessor|https://github.com/apache/camel/blob/master/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPPassphraseAccessor.java]. A default implementation is given by [PGPPassphraseAccessorDefaultDefaultPGPPassphraseAccessor|https://github.com/apache/camel/blob/master/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPPassphraseAccessorDefaultDefaultPGPPassphraseAccessor.java]. The passphrase accessor is especially useful in the decrypt case; see chapter 'PGP Decrypting/Verifying of Messages Encrypted/Signed by Different Private/Public Keys' below. |
| {div}{publicKeyAccessor}} | [PGPPublicKeyAccessor|https://github.com/apache/camel/blob/master/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPPublicKeyAccessor.java] | {{null}} | *Sinace Camel 2.13.0*; provides the publicKeyAccessor which can be used to help PGPDataFormat to find the PublicKey and EncryptionKeys. You provide a bean which implements the interface [PGPPublicKeyAccessor|https://github.com/apache/camel/blob/master/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPPublicKeyAccessor.java]. A default implementation is given by [DefaultPGPPublicKeyAccessor|https://github.com/apache/camel/blob/master/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/DefaultPGPPublicKeyAccessor.java].|
| {{secretKeyAccessor}} | [PGPSecretKeyAccessor|https://github.com/apache/camel/blob/master/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPSecretAccessor.java] | {{null}} | *Sinace Camel 2.13.0*; provides the secretKeyAccessor which can be used to help PGPDataFormat to find the SecretKey and DecryptionKeys. You provide a bean which implements the interface [PGPSecretKeyAccessor|https://github.com/apache/camel/blob/master/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPSecretKeyAccessor.java]. A default implementation is given by [DefaultPGPSecretKeyAccessor|https://github.com/apache/camel/blob/master/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/DefaultPGPSecretKeyAccessor.java].|
|
You can override the PGPDataFormat options by applying below headers into message dynamically.
Wiki Markup |
{div:class=confluenceTableSmall}{div}
|
Name |
Type |
Description |
CamelPGPDataFormatKeyFileName |
String |
Since Camel 2.11.0; filename of the keyring; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatEncryptionKeyRing |
byte[] |
Since Camel 2.12.1; the encryption keyring; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatKeyUserid |
String |
Since Camel 2.11.0; the User ID of the key in the PGP keyring; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatKeyUserids |
List<String> |
Since camel 2.12.2: the User IDs of the key in the PGP keyring; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatKeyPassword |
String |
Since Camel 2.11.0; password used when opening the private key; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatSignatureKeyFileName |
String |
Since Camel 2.11.0; filename of the signature keyring; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatSignatureKeyRing |
byte[] |
Since Camel 2.12.1; the signature keyring; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatSignatureKeyUserid |
String |
Since Camel 2.11.0; the User ID of the signature key in the PGP keyring; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatSignatureKeyUserids |
List<String> |
Since Camel 2.12.3; the User IDs of the signature keys in the PGP keyring; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatSignatureKeyPassword |
String |
Since Camel 2.11.0; password used when opening the signature private key; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatEncryptionAlgorithm |
int |
Since Camel 2.12.2; symmetric key encryption algorithm; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatSignatureHashAlgorithm |
int |
Since Camel 2.12.2; signature hash algorithm; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatCompressionAlgorithm |
int |
Since Camel 2.12.2; compression algorithm; will override existing setting directly on the PGPDataFormat. |
CamelPGPDataFormatNumberOfEncryptionKeys |
Integer |
Since Camel 2.12.3; number of public keys used for encrypting the symmectric key, set by PGPDataFormat during encryptiion process |
CamelPGPDataFormatNumberOfSigningKeys |
Integer |
Since Camel 2.12.3; number of private keys used for creating signatures, set by PGPDataFormat during signing process |
...
A PGP Data Formater can decrypt/verify messages which have been encrypted by different public keys or signed by different private keys. Just, provide the corresponding private keys in the secret keyring, the corresponding public keys in the public keyring, and the passphrases in the passphrase accessor.
Code Block |
|
Map<String, String> userId2Passphrase = new HashMap<String, String>(2);
// add passphrases of several private keys whose corresponding public keys have been used to encrypt the messages
userId2Passphrase.put("UserIdOfKey1","passphrase1"); // you must specify the exact User ID!
userId2Passphrase.put("UserIdOfKey2","passphrase2");
PGPPassphraseAccessor passphraseAccessor = new PGPPassphraseAccessorDefault(userId2Passphrase);
PGPDataFormat pgpVerifyAndDecrypt = new PGPDataFormat();
pgpVerifyAndDecrypt.setPassphraseAccessor(passphraseAccessor);
// the method getSecKeyRing() provides the secret keyring as byte array containing the private keys
pgpVerifyAndDecrypt.setEncryptionKeyRing(getSecKeyRing()); // alternatively you can use setKeyFileName(keyfileName)
// the method getPublicKeyRing() provides the public keyring as byte array containing the public keys
pgpVerifyAndDecrypt.setSignatureKeyRing((getPublicKeyRing()); // alternatively you can use setSignatureKeyFileName(signatgureKeyfileName)
// it is not necessary to specify the encryption or signer User Id
from("direct:start")
...
.unmarshal(pgpVerifyAndDecrypt) // can decrypt/verify messages encrypted/signed by different private/public keys
...
|
...