Class Crypto


  • public class Crypto
    extends java.lang.Object
    Utility classes and interfaces for message payload encryption. This class supports AES/CBC/PKCS5 with a default key length of 256 bits but supporting other key lengths. Other algorithms and chaining modes are not supported directly, but supportable by extending/implementing the base classes and interfaces here. Secure random data for creation of Initialisation Vectors (IVs) and keys is obtained from the default system SecureRandom. Future extensions of this class might make the SecureRandom pluggable or at least seedable with client-provided entropy. Each message payload is encrypted with an IV in CBC mode, and the IV is concatenated with the resulting raw ciphertext to construct the "ciphertext" data passed to the recipient.
    • Field Detail

      • DEFAULT_ALGORITHM

        public static final java.lang.String DEFAULT_ALGORITHM
        See Also:
        Constant Field Values
      • DEFAULT_KEYLENGTH

        public static final int DEFAULT_KEYLENGTH
    • Constructor Detail

      • Crypto

        public Crypto()
    • Method Detail

      • getDefaultParams

        public static Crypto.CipherParams getDefaultParams()
        Obtain a default CipherParams. This uses default algorithm, mode and padding and key length. A key and IV are generated using the default system SecureRandom; the key may be obtained from the returned CipherParams for out-of-band distribution to other clients.
        Returns:
        the CipherParams
      • getDefaultParams

        public static Crypto.CipherParams getDefaultParams​(byte[] key)
        Obtain a default CipherParams. This uses default algorithm, mode and padding and initialises a key based on the given key data. The cipher key length is derived from the length of the given key data. An IV is generated using the default system SecureRandom. Use this method of constructing CipherParams if initialising a Channel with a client-provided key, or to obtain a system-generated key of a non-default key length.
        Returns:
        the CipherParams
      • getDefaultParams

        public static Crypto.CipherParams getDefaultParams​(java.lang.String base64Key)
        Obtain a default CipherParams using Base64-encoded key. Same as above, throws IllegalArgumentException if base64Key is invalid
        Parameters:
        base64Key -
        Returns:
      • getParams

        public static Crypto.CipherParams getParams​(java.lang.String algorithm,
                                                    int keyLength)
      • getParams

        public static Crypto.CipherParams getParams​(java.lang.String algorithm,
                                                    byte[] key)
                                             throws java.security.NoSuchAlgorithmException
        Throws:
        java.security.NoSuchAlgorithmException
      • getParams

        public static Crypto.CipherParams getParams​(java.lang.String algorithm,
                                                    byte[] key,
                                                    byte[] iv)
                                             throws java.security.NoSuchAlgorithmException
        Throws:
        java.security.NoSuchAlgorithmException
      • generateRandomKey

        public static byte[] generateRandomKey​(int keyLength)
      • generateRandomKey

        public static byte[] generateRandomKey()
      • getRandomMessageId

        public static java.lang.String getRandomMessageId()