Package com.gengoai

Enum EncryptionMethod

  • All Implemented Interfaces:
    Serializable, Comparable<EncryptionMethod>

    public enum EncryptionMethod
    extends Enum<EncryptionMethod>

    Convenience methods for encryption with common algorithms. Common usages is as follows:

     
       String encryptedText = EncryptionMethod.AES.encrypt("This is my secret text", "MY SECURE PASSWORD");
       String decryptedText = EncryptionMethod.AES.decryptToString(encryptedText, "MY SECURE PASSWORD");
     
     
    Author:
    David B. Bracewell
    • Method Detail

      • values

        public static EncryptionMethod[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (EncryptionMethod c : EncryptionMethod.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static EncryptionMethod valueOf​(String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        IllegalArgumentException - if this enum type has no constant with the specified name
        NullPointerException - if the argument is null
      • fromName

        public static EncryptionMethod fromName​(String name)
        Parses a String to find the correct EncryptionMethod.
        Parameters:
        name - The name;
        Returns:
        An EncryptionMethod
      • constructCipher

        protected Cipher constructCipher​(@NonNull
                                         @lombok.NonNull byte[] key,
                                         int mode)

        Constructs a cipher for the given key and mode.

        Parameters:
        key - The key
        mode - The mode
        Returns:
        The Cipher
      • decrypt

        public final byte[] decrypt​(@NonNull
                                    @NonNull String content,
                                    @NonNull
                                    @NonNull String key)
        Decrypts encrypted content in a Base64 encoded string into a byte array.
        Parameters:
        content - The encrypted content
        key - The password
        Returns:
        An unencrypted version of the content
      • decrypt

        public byte[] decrypt​(@NonNull
                              @NonNull String content,
                              @NonNull
                              @lombok.NonNull byte[] key)
        Decrypts encrypted content in a Base64 encoded byte array into a byte array.
        Parameters:
        content - The encrypted content
        key - The password
        Returns:
        An unencrypted version of the content
      • decryptToString

        public final String decryptToString​(@NonNull
                                            @NonNull String content,
                                            @NonNull
                                            @NonNull String key)
        Decrypts encrypted content in a Base64 encoded string into a string.
        Parameters:
        content - The encrypted content
        key - The password
        Returns:
        An unencrypted version of the content
      • encrypt

        public String encrypt​(@NonNull
                              @lombok.NonNull byte[] content,
                              @NonNull
                              @lombok.NonNull byte[] key)
        Encrypts content into a Base64 encoded string.
        Parameters:
        content - The content
        key - The password
        Returns:
        A Base64 encoded version of the encrypted content
      • encrypt

        public final String encrypt​(@NonNull
                                    @NonNull String content,
                                    @NonNull
                                    @NonNull String key)
        Encrypts content into a Base64 encoded string.
        Parameters:
        content - The content
        key - The password
        Returns:
        A Base64 encoded version of the encrypted content
      • ensureKeyLength

        protected final byte[] ensureKeyLength​(byte[] key)

        Ensures the the key is needed length for the encryption method.

        Parameters:
        key - The key
        Returns:
        A key of the need length