Class CryptoUtil


  • public class CryptoUtil
    extends Object
    The CryptoUtil class provides a number of cryptography related utility functions.
    Author:
    Marcus Portmann
    • Constructor Detail

      • CryptoUtil

        public CryptoUtil()
    • Method Detail

      • createRandomEncryptionIV

        public static byte[] createRandomEncryptionIV​(int length)
        Creates a random encryption initialization vector with the specified length.
        Parameters:
        length - the length of the random encryption initialization vector
        Returns:
        the random encryption initialization vector
      • getKeyPair

        public static KeyPair getKeyPair​(KeyStore keyStore,
                                         String alias,
                                         String password)
                                  throws GeneralSecurityException
        Retrieve the key pair with the specified alias from the key store.
        Parameters:
        keyStore - the key store
        alias - the alias for the key pair
        password - the password for the key
        Returns:
        the key pair
        Throws:
        GeneralSecurityException - if the key pair could not be retrieved
      • getRandomAESKey

        public static byte[] getRandomAESKey()
        Returns a randomly generated AES key.
        Returns:
        a randomly generated AES key
      • loadKeyStore

        public static KeyStore loadKeyStore​(String type,
                                            String path,
                                            String password)
                                     throws GeneralSecurityException
        Load a key store.
        Parameters:
        type - the key store type e.g. JKS, PKCS12, etc
        path - the path to the key store
        password - the key store password
        Returns:
        the key store that was loaded
        Throws:
        GeneralSecurityException - if the key store could not be loaded
      • loadKeyStore

        public static KeyStore loadKeyStore​(String type,
                                            String path,
                                            String password,
                                            String alias)
                                     throws GeneralSecurityException
        Load a key store and query it to confirm a key pair with the specified alias is present.
        Parameters:
        type - the key store type e.g. JKS, PKCS12, etc
        path - the path to the key store
        password - the key store password
        alias - the alias for the key pair in the key store that should be retrieved
        Returns:
        the key store that was loaded
        Throws:
        GeneralSecurityException - if the key store could not be loaded
      • loadTrustStore

        public static KeyStore loadTrustStore​(String type,
                                              String path,
                                              String password)
                                       throws GeneralSecurityException
        Load the trust store.
        Parameters:
        type - the trust store type e.g. JKS, PKCS12, etc
        path - the path to the trust store
        password - the trust store password
        Returns:
        the trust store that was loaded
        Throws:
        GeneralSecurityException - if the trust store could not be loaded
      • passwordTo3DESKey

        public static byte[] passwordTo3DESKey​(String password)
                                        throws CryptoException
        Convert the specified password to a 3DES key that can be used with the 3DES cypher encrypt and decrypt functions.
        Parameters:
        password - the password to convert to a 3DES key
        Returns:
        the 3DES key
        Throws:
        CryptoException
      • passwordToAESKey

        public static byte[] passwordToAESKey​(String password)
                                       throws CryptoException
        Convert the specified password to an AES key that can be used with the AES cypher encrypt and decrypt functions.
        Parameters:
        password - the password to convert to an AES key
        Returns:
        the AES key
        Throws:
        CryptoException
      • passwordToAESKey

        public static byte[] passwordToAESKey​(String password,
                                              byte[] salt)
                                       throws CryptoException
        Convert the specified password to an AES key that can be used with the AES cypher encrypt and decrypt functions.
        Parameters:
        password - the password to convert to an AES key
        salt - the salt to use when generating the AES key
        Returns:
        the AES key
        Throws:
        CryptoException
      • passwordToAESKey

        public static byte[] passwordToAESKey​(String password,
                                              String salt)
                                       throws CryptoException
        Convert the specified password to an AES key that can be used with the AES cypher encrypt and decrypt functions.
        Parameters:
        password - the password to convert to an AES key
        salt - the salt to use when generating the AES key
        Returns:
        the AES key
        Throws:
        CryptoException