Class SymmetricCipher

  • All Implemented Interfaces:
    CommonCipher

    public class SymmetricCipher
    extends Object
    implements CommonCipher
    This class provides simple and stateless way to encrypt and decrypt messages using selected symmetric cipher.
    It requires to have base password provided. Unique cryptography key is generated by this implementation every time it encrypts and decrypts. The key uniqueness is ensured by randomly generated salt which is added to the password.
    • Field Detail

      • ALGORITHM_AES_CBC

        public static final String ALGORITHM_AES_CBC
        AES algorithm with CBC method and PKCS5 padding.
        It is strongly advised to be used together with HMAC or other authenticated message digest.
        Value is: "AES/CBC/PKCS5Padding".
        See Also:
        Constant Field Values
      • ALGORITHM_AES_CTR

        public static final String ALGORITHM_AES_CTR
        AES algorithm with CTR method and no padding.
        It is strongly advised to be used together with HMAC or other authenticated message digest.
        Value is: "AES/CTR/NoPadding".
        See Also:
        Constant Field Values
      • ALGORITHM_AES_GCM

        public static final String ALGORITHM_AES_GCM
        AES algorithm with GCM method and no padding.
        Does not need to be used with any authenticated message digest since GCM is making its signature by the design.
        Value is: "AES/GCM/NoPadding".
        See Also:
        Constant Field Values
      • ALGORITHM_CHA_CHA

        public static final String ALGORITHM_CHA_CHA
        ChaCha20 encryption algorithm.
        Value is: "ChaCha20".
        See Also:
        Constant Field Values
      • ALGORITHM_CHA_CHA_POLY1305

        public static final String ALGORITHM_CHA_CHA_POLY1305
        ChaCha20 encryption algorithm with Poly1305 authentication code.
        Value is: "ChaCha20-Poly1305".
        See Also:
        Constant Field Values
    • Method Detail

      • create

        public static SymmetricCipher create​(char[] password)
        Create a new instance based on the password. Default used algorithm is ALGORITHM_AES_GCM.
        Parameters:
        password - password
        Returns:
        new instance
      • encrypt

        public static Base64Value encrypt​(String algorithm,
                                          byte[] key,
                                          byte[] iv,
                                          Base64Value plain)
        Encrypt the message with the usage of provided parameters.
        Parameters:
        algorithm - algorithm to be used
        key - key to encrypt message
        iv - initialization vector
        plain - message
        Returns:
        encrypted message
      • encrypt

        public static Base64Value encrypt​(String algorithm,
                                          String provider,
                                          byte[] key,
                                          byte[] iv,
                                          Base64Value plain)
        Encrypt the message with the usage of provided parameters.
        Parameters:
        algorithm - algorithm to be used
        provider - provider of the algorithm
        key - key to encrypt message
        iv - initialization vector
        plain - message
        Returns:
        encrypted message
      • encrypt

        public static Base64Value encrypt​(String algorithm,
                                          String provider,
                                          byte[] key,
                                          AlgorithmParameterSpec params,
                                          Base64Value plain)
        Encrypt the message with the usage of provided parameters.
        Parameters:
        algorithm - algorithm to be used
        provider - provider of the algorithm
        key - key to encrypt message
        params - cipher parameter object
        plain - message
        Returns:
        encrypted message
      • decrypt

        public static Base64Value decrypt​(String algorithm,
                                          byte[] key,
                                          byte[] iv,
                                          Base64Value encrypted)
        Decrypt the message with the usage of provided parameters.
        Parameters:
        algorithm - algorithm to be used
        key - key to decrypt message
        iv - encrypted message initialization vector
        encrypted - encrypted message
        Returns:
        decrypted message
      • decrypt

        public static Base64Value decrypt​(String algorithm,
                                          String provider,
                                          byte[] key,
                                          byte[] iv,
                                          Base64Value encrypted)
        Decrypt the message with the usage of provided parameters.
        Parameters:
        algorithm - algorithm to be used
        provider - algorithm provider
        key - key to decrypt message
        iv - encrypted message initialization vector
        encrypted - encrypted message
        Returns:
        decrypted message
      • decrypt

        public static Base64Value decrypt​(String algorithm,
                                          String provider,
                                          byte[] key,
                                          AlgorithmParameterSpec params,
                                          Base64Value encrypted)
        Decrypt the message with the usage of provided parameters.
        Parameters:
        algorithm - algorithm to be used
        provider - algorithm provider
        key - key to decrypt message
        params - cipher parameter object
        encrypted - encrypted message
        Returns:
        decrypted message