Class AbstractCipher

  • All Implemented Interfaces:
    ICipher, ICipherSpi
    Direct Known Subclasses:
    MessageDigestCipher, SymmetricCipher

    @StableMinor(version="16.1",
                 sinceVersion="5.5")
    public abstract class AbstractCipher
    extends java.lang.Object
    implements ICipherSpi
    An abstract ICipher implementation for easing concrete implementations.

    This abstract class provides implementations for the string-based encryption and decryption operations, by encoding the encrypted bytes in Base64.

    Since:
    5.5.0
    Version:
    $Revision: 26156 $ $Date: 2024-01-13 10:26:46 -0300 (Sat, 13 Jan 2024) $
    • Constructor Summary

      Constructors 
      Constructor Description
      AbstractCipher()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String decrypt​(java.lang.String value)
      Decrypts a string.
      java.lang.String encrypt​(java.lang.String value)
      Encrypts a string.
      protected java.lang.String getStringCharset()
      The charset used to perform the encoding between String and bytes of the plain text string values in encrypt(String) and decrypt(String).
      protected boolean getUseURLSafeBase64Variant()
      Indicates if a URL safe Base64 variant is to be used when encoding bytes to String.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AbstractCipher

        public AbstractCipher()
    • Method Detail

      • getStringCharset

        protected java.lang.String getStringCharset()
        The charset used to perform the encoding between String and bytes of the plain text string values in encrypt(String) and decrypt(String).

        The default value is UTF-8. This method may be overridden to change it.

        Returns:
        the charset.
        Since:
        5.5.0
      • getUseURLSafeBase64Variant

        protected boolean getUseURLSafeBase64Variant()
        Indicates if a URL safe Base64 variant is to be used when encoding bytes to String.

        The default value is true. This method may be overridden to change it.

        Returns:
        true if a URL safe variant is to be used, false if the standard Base64 is to be used.
        Since:
        5.5.0
      • encrypt

        public java.lang.String encrypt​(java.lang.String value)
        Encrypts a string.

        This abstract implementation converts the given string to bytes based on getStringCharset(), encrypts it with ICipher.encrypt(byte[]), and encodes the result to a string using the Base64 algorithm.

        Specified by:
        encrypt in interface ICipher
        Parameters:
        value - the original string.
        Returns:
        the encrypted string.
        Since:
        5.5.0
      • decrypt

        public java.lang.String decrypt​(java.lang.String value)
                                 throws InvalidEncryptedValueException
        Decrypts a string.

        This abstract implementation decodes the value to bytes using the Base64 algorithm, decrypts it with ICipher.decrypt(byte[]), and creates the resulting plain text string based on getStringCharset().

        Specified by:
        decrypt in interface ICipher
        Parameters:
        value - the encrypted string.
        Returns:
        the string decrypted.
        Throws:
        InvalidEncryptedValueException - if value is incompatible with this cipher (probably was not encrypted by it or is corrupted).
        Since:
        5.5.0