Class ChaosKey

  • All Implemented Interfaces:
    Serializable, Key, SecretKey, Destroyable, org.refcodes.mixin.ChildAccessor<ChaosKey>, org.refcodes.mixin.Disposable, org.refcodes.mixin.EncodedAccessor

    public class ChaosKey
    extends Object
    implements SecretKey, org.refcodes.mixin.EncodedAccessor, org.refcodes.mixin.ChildAccessor<ChaosKey>, org.refcodes.mixin.Disposable
    The key holding the parameters for the chaos function. Thanks Christian Pontesegger for the very good example on "Writing your own JCA extensions - a full cipher" at: "http://codeandme.blogspot.de/2013/07/writing-your-own-jca-extensions-full.html" and for the very good example on "Writing your own JCA extensions - a simple digest " at: "http://codeandme.blogspot.de/2013/06/writing-your-own-jca-extensions-simple.html"
    See Also:
    Serialized Form
    • Field Detail

      • A_BYTES

        public static final int A_BYTES
        The number of bytes required to store A
        See Also:
        Constant Field Values
      • S_BYTES

        public static final int S_BYTES
        The number of bytes required to store S
        See Also:
        Constant Field Values
      • S_MAX

        public static final long S_MAX
        The maximum valid S value (2^53-1 = MAX_SAFE_INTEGER when floating point is involved).
        See Also:
        Constant Field Values
      • S_MIN

        public static final long S_MIN
        The minimum valid S value (-2^53 = MIN_SAFE_INTEGER when floating point is involved).
        See Also:
        Constant Field Values
      • S_NEGATIVE_MAX

        public static final long S_NEGATIVE_MAX
        The maximum negative S value (S_NEGATIVE_MAX x S_POSITIVE_MIN = 256 possibilities, which is the range of values one byte can represent, which in turn is he atomic data to be encrypted / decrypted).
        See Also:
        Constant Field Values
      • S_POSITIVE_MIN

        public static final long S_POSITIVE_MIN
        The minimum positive S value (S_NEGATIVE_MAX x S_POSITIVE_MIN = 256 possibilities, which is the range of values one byte can represent, which in turn is he atomic data to be encrypted / decrypted).
        See Also:
        Constant Field Values
      • X_BYTES

        public static final int X_BYTES
        The number of bytes required to store X
        See Also:
        Constant Field Values
    • Constructor Detail

      • ChaosKey

        public ChaosKey​(byte[] aEncoded)
        Instantiates the ChaosKey by reversing the result of the getEncoded() operation where the byte array contains the values x0, a and s (excluding the ChaosOptions). The values use a big endian representation. The byte array being passed is to be of the size as returned by getEncodedLength(). The number of bytes required by each value in the byte array are defined in the constants 8, A_BYTES and S_BYTES respectively and in that order.
        Parameters:
        aEncoded - The encoded representation of the chaos key.
      • ChaosKey

        public ChaosKey​(double x0,
                        double a,
                        long s)
        Instantiates a new ChaosKey directly from the provided double values. Make sure your provided arguments are within the allowed bounds;
        Parameters:
        x0 - the x0: (0 <= x0 <= 1 )
        a - the a: (a <= 3.57 <= 4 )
        s - the s: (Integer.MAX_VALUE >= s >=Integer.MAX_VALUE AND 16 <= s <= -16)
      • ChaosKey

        public ChaosKey​(double x0,
                        double a,
                        long s,
                        ChaosOptions aChaosOptions)
        Instantiates a new ChaosKey directly from the provided double values. Make sure your provided arguments are within the allowed bounds;
        Parameters:
        x0 - the x0: (0 <= x0 <= 1 )
        a - the a: (a <= 3.57 <= 4 )
        s - the s: (Integer.MAX_VALUE >= s >=Integer.MAX_VALUE AND 16 <= s <= -16)
        aChaosOptions - The ChaosMode (ChaosOptions) to use to enhance Chaos-based encryption by enabling various additional processing steps.
      • ChaosKey

        public ChaosKey​(int x0,
                        int a,
                        int s)
        Instantiates a new ChaosKey from the provided integer values. The integers are converted to the valid ranges of double values for invoking the constructor ChaosKey(double, double, long). You can use values in the range of Integer.MIN_VALUE and Integer.MAX_VALUE.
        Parameters:
        x0 - The value from which to calculate the valid x0 double.
        a - The value from which to calculate the valid a double.
        s - The value from which to calculate the valid s double.
      • ChaosKey

        public ChaosKey​(int x0,
                        int a,
                        int s,
                        ChaosOptions aChaosOptions)
        Instantiates a new ChaosKey from the provided integer values. The integers are converted to the valid ranges of double values for invoking the constructor ChaosKey(double, double, long). You can use values in the range of Integer.MIN_VALUE and Integer.MAX_VALUE.
        Parameters:
        x0 - The value from which to calculate the valid x0 double.
        a - The value from which to calculate the valid a double.
        s - The value from which to calculate the valid s double.
        aChaosOptions - The ChaosMode (ChaosOptions) to use to enhance Chaos-based encryption by enabling various additional processing steps.
      • ChaosKey

        public ChaosKey​(char[] aSecret)
        Instantiates a new ChaosKey by calculating x0, a and s from the provided char array.
        Parameters:
        aSecret - The char array (in contrast to a String, a char array can be invalidated after use by overwriting its array elements with random values) from which to calculate x0, a and s.
      • ChaosKey

        public ChaosKey​(String aSecret)
        Instantiates a new ChaosKey by calculating x0, a and s from the provided String.
        Parameters:
        aSecret - The String from which to calculate x0, a and s.
      • ChaosKey

        public ChaosKey​(char[] aSecret,
                        ChaosOptions aChaosOptions)
        Instantiates a new ChaosKey by calculating x0, a and s from the provided char array.
        Parameters:
        aSecret - The char array (in contrast to a String, a char array can be invalidated after use by overwriting its array elements with random values) from which to calculate x0, a and s.
        aChaosOptions - The ChaosMode (ChaosOptions) to use to enhance Chaos-based encryption.
      • ChaosKey

        public ChaosKey​(String aSecret,
                        ChaosOptions aChaosOptions)
        Instantiates a new ChaosKey by calculating x0, a and s from the provided String.
        Parameters:
        aSecret - The String from which to calculate x0, a and s.
        aChaosOptions - The ChaosMode (ChaosOptions) to use to enhance Chaos-based encryption.
      • ChaosKey

        public ChaosKey​(double x0,
                        double a,
                        long s,
                        ChaosKey aChildKey)
        Instantiates a new ChaosKey directly from the provided double values. Make sure your provided arguments are within the allowed bounds;
        Parameters:
        x0 - the x0: (0 <= x0 <= 1 )
        a - the a: (a <= 3.57 <= 4 )
        s - the s: (Integer.MAX_VALUE >= s >=Integer.MAX_VALUE AND 16 <= s <= -16)
        aChildKey - The child ChaosKey being the successor of this ChaosKey to encrypt and the predecessor of this ChaosKey to decrypt.
      • ChaosKey

        public ChaosKey​(int x0,
                        int a,
                        int s,
                        ChaosKey aChildKey)
        Instantiates a new ChaosKey from the provided integer values. The integers are converted to the valid ranges of double values for invoking the constructor ChaosKey(double, double, long). You can use values in the range of Integer.MIN_VALUE and Integer.MAX_VALUE.
        Parameters:
        x0 - The value from which to calculate the valid x0 double.
        a - The value from which to calculate the valid a double.
        s - The value from which to calculate the valid s double.
        aChildKey - The child ChaosKey being the successor of this ChaosKey to encrypt and the predecessor of this ChaosKey to decrypt.
      • ChaosKey

        public ChaosKey​(int x0,
                        int a,
                        int s,
                        ChaosOptions aChaosOptions,
                        ChaosKey aChildKey)
        Instantiates a new ChaosKey from the provided integer values. The integers are converted to the valid ranges of double values for invoking the constructor ChaosKey(double, double, long). You can use values in the range of Integer.MIN_VALUE and Integer.MAX_VALUE.
        Parameters:
        x0 - The value from which to calculate the valid x0 double.
        a - The value from which to calculate the valid a double.
        s - The value from which to calculate the valid s double.
        aChaosOptions - The ChaosMode (ChaosOptions) to use to enhance Chaos-based encryption by enabling various additional processing steps.
        aChildKey - The child ChaosKey being the successor of this ChaosKey to encrypt and the predecessor of this ChaosKey to decrypt.
      • ChaosKey

        public ChaosKey​(char[] aSecret,
                        ChaosKey aChildKey)
        Instantiates a new ChaosKey by calculating x0, a and s from the provided char array.
        Parameters:
        aSecret - The char array (in contrast to a String, a char array can be invalidated after use by overwriting its array elements with random values) from which to calculate x0, a and s.
        aChildKey - The child ChaosKey being the successor of this ChaosKey to encrypt and the predecessor of this ChaosKey to decrypt.
      • ChaosKey

        public ChaosKey​(String aSecret,
                        ChaosKey aChildKey)
        Instantiates a new ChaosKey by calculating x0, a and s from the provided String.
        Parameters:
        aSecret - The String from which to calculate x0, a and s.
        aChildKey - The child ChaosKey being the successor of this ChaosKey to encrypt and the predecessor of this ChaosKey to decrypt.
      • ChaosKey

        public ChaosKey​(byte[] aEncoded,
                        ChaosKey aChildKey)
        Instantiates the ChaosKey by reversing the result of the getEncoded() operation where the byte array contains the values x0, a and s (including the ChaosOptions.getEncoded()). The values use a big endian representation. The byte array being passed is to be of the size as returned by getEncodedLength(). The number of bytes required by each value in the byte array are defined in the constants 8, A_BYTES and S_BYTES respectively and in that order.
        Parameters:
        aEncoded - The encoded representation of the chaos key.
        aChildKey - The child ChaosKey being the successor of this ChaosKey to encrypt and the predecessor of this ChaosKey to decrypt.
      • ChaosKey

        public ChaosKey​(double x0,
                        double a,
                        long s,
                        ChaosOptions aChaosOptions,
                        ChaosKey aChildKey)
        Instantiates a new ChaosKey directly from the provided double values. Make sure your provided arguments are within the allowed bounds;
        Parameters:
        x0 - the x0: (0 <= x0 <= 1 )
        a - the a: (a <= 3.57 <= 4 )
        s - the s: (Integer.MAX_VALUE >= s >=Integer.MAX_VALUE AND 16 <= s <= -16)
        aChaosOptions - The ChaosMode (ChaosOptions) to use to enhance Chaos-based encryption by enabling various additional processing steps.
        aChildKey - The child ChaosKey being the successor of this ChaosKey to encrypt and the predecessor of this ChaosKey to decrypt.
      • ChaosKey

        public ChaosKey​(char[] aSecret,
                        ChaosOptions aChaosOptions,
                        ChaosKey aChildKey)
        Instantiates a new ChaosKey by calculating x0, a and s from the provided char array.
        Parameters:
        aSecret - The char array (in contrast to a String, a char array can be invalidated after use by overwriting its array elements with random values) from which to calculate x0, a and s.
        aChaosOptions - The ChaosMode (ChaosOptions) to use to enhance Chaos-based encryption.
        aChildKey - The child ChaosKey being the successor of this ChaosKey to encrypt and the predecessor of this ChaosKey to decrypt.
      • ChaosKey

        public ChaosKey​(String aSecret,
                        ChaosOptions aChaosOptions,
                        ChaosKey aChildKey)
        Instantiates a new ChaosKey by calculating x0, a and s from the provided String.
        Parameters:
        aSecret - The String from which to calculate x0, a and s.
        aChaosOptions - The ChaosMode (ChaosOptions) to use to enhance Chaos-based encryption.
        aChildKey - The child ChaosKey being the successor of this ChaosKey to encrypt and the predecessor of this ChaosKey to decrypt.
    • Method Detail

      • dispose

        public void dispose()
        Specified by:
        dispose in interface org.refcodes.mixin.Disposable
      • getA

        public double getA()
        Gets the a.
        Returns:
        the a
      • getChaosOptions

        public ChaosOptions getChaosOptions()
        Returns the ChaosOptions being used to enhance Chaos-based encryption by enabling various additional processing steps. Defaults to ChaosMode.NONE.
        Returns:
        the chaos metrics
      • getEncoded

        public byte[] getEncoded()
        Returns the key in its primary encoding format. Returns the bytes in a defined order representing the x0, a and s values (including the ChaosOptions.getEncoded()). The values use a big endian representation. The byte array being returned is of the size as returned by getEncodedLength(). The number of bytes required by each value in the byte array are defined in the constants 8, A_BYTES and S_BYTES respectively and in that order.
        Specified by:
        getEncoded in interface org.refcodes.mixin.EncodedAccessor
        Specified by:
        getEncoded in interface Key
        Returns:
        The encoded key being the according bytes representing the ChaosKey x0, a and s values.
      • toEncodedChain

        public byte[] toEncodedChain()
        Returns all encoded ChaosKey representations including this ChaosKey as well as all the nested ChaosKey children being their concatenated getEncoded() representation, the this being first and the bottom most child the last. Invoking the createKeyChain(byte[]) method with the result of this method reconstructs the ChaosKey chain's instances.
        Returns:
        The bytes representation of this ChaosKey and its nested ChaosKey children as of their getEncoded() representation.
      • getChild

        public ChaosKey getChild()
        Returns the next outer ChaosKey. It is the successor to encrypt (the last child ChaosKey is the last one to encrypt) and the predecessor to decrypt (the last child ChaosKey is the first one to decrypt).
        Specified by:
        getChild in interface org.refcodes.mixin.ChildAccessor<ChaosKey>
        Returns:
        the child ChaosKey being the successor of this ChaosKey to encrypt and the predecessor of this ChaosKey to decrypt.
      • getS

        public long getS()
        Gets the s.
        Returns:
        the s
      • getX0

        public double getX0()
        Gets the x0.
        Returns:
        the x0
      • toCertificate

        public String toCertificate()
        Creates a certificate for encryption and decryption from this ChaosKey. The certificate is not password protected!
        Returns:
        The String certificate representation of this ChaosKey.
      • toCertificate

        public String toCertificate​(int aLineWidth)
        Creates a certificate for encryption and decryption from this ChaosKey. The certificate is not password protected!
        Parameters:
        aLineWidth - The line width to use for the encoded certificate data (excluding the head and the tail).
        Returns:
        The String certificate representation of this ChaosKey.
      • toCertificate

        public String toCertificate​(String aPassword)
        Creates a password protected certificate for encryption and decryption from this ChaosKey.
        Parameters:
        aPassword - The password with which to protect the key.
        Returns:
        The String certificate representation of this ChaosKey.
      • toCertificate

        public String toCertificate​(String aPassword,
                                    int aLineWidth)
        Creates a password protected certificate for encryption and decryption from this ChaosKey.
        Parameters:
        aPassword - The password with which to protect the key.
        aLineWidth - The line width to use for the encoded certificate data (excluding the head and the tail).
        Returns:
        The String certificate representation of this ChaosKey.
      • toCertificate

        public String toCertificate​(String aPassword,
                                    ChaosOptions aChaosOptions)
        Creates password protected a certificate for encryption and decryption from this ChaosKey.
        Parameters:
        aPassword - The password with which to protect the key.
        aChaosOptions - The ChaosOptions to use for the certificate encryption.
        Returns:
        The String certificate representation of this ChaosKey.
      • toCertificate

        public String toCertificate​(String aPassword,
                                    ChaosOptions aChaosOptions,
                                    int aLineWidth)
        Creates password protected a certificate for encryption and decryption from this ChaosKey.
        Parameters:
        aPassword - The password with which to protect the key.
        aChaosOptions - The ChaosOptions to use for the certificate encryption.
        aLineWidth - The line width to use for the encoded certificate data (excluding the head and the tail).
        Returns:
        The String certificate representation of this ChaosKey.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • createFromCertificate

        public static ChaosKey createFromCertificate​(File aCertFile,
                                                     String aPassword)
                                              throws IOException
        Creates a ChaosKey (chain) from the password protected certificate retrieved from the given File.
        Parameters:
        aCertFile - The File pointing to the certificate representing the ChaosKey.
        aPassword - The password protecting the certificate.
        Returns:
        The ChaosKey (chain) represented by the certificate.
        Throws:
        IOException - thrown in case an I/O related problem occurred while accessing the File.
      • createFromCertificate

        public static ChaosKey createFromCertificate​(InputStream aInputStream,
                                                     String aPassword)
                                              throws IOException
        Creates a ChaosKey (chain) from the password protected certificate retrieved from the given InputStream.
        Parameters:
        aInputStream - The InputStream providing the certificate representing the ChaosKey.
        aPassword - The password protecting the certificate.
        Returns:
        The ChaosKey (chain) represented by the certificate.
        Throws:
        IOException - thrown in case an I/O related problem occurred while accessing the InputStream.
      • createFromCertificate

        public static ChaosKey createFromCertificate​(String aCertificate,
                                                     String aPassword)
        Creates a ChaosKey (chain) from the password protected certificate.
        Parameters:
        aCertificate - The certificate representing the ChaosKey.
        aPassword - The password protecting the certificate.
        Returns:
        The ChaosKey (chain) represented by the certificate.
      • createFromCertificate

        public static ChaosKey createFromCertificate​(File aCertFile)
                                              throws IOException
        Creates a ChaosKey (chain) from the unsecured certificate retrieved from the given File.
        Parameters:
        aCertFile - The File pointing to the certificate representing the ChaosKey.
        Returns:
        The ChaosKey (chain) represented by the certificate.
        Throws:
        IOException - thrown in case an I/O related problem occurred while accessing the File.
      • createFromCertificate

        public static ChaosKey createFromCertificate​(String aCertificate)
        Creates a ChaosKey (chain) from the unsecured certificate.
        Parameters:
        aCertificate - The certificate representing the ChaosKey.
        Returns:
        The ChaosKey (chain) represented by the certificate.
      • createKeyChain

        public static ChaosKey createKeyChain​(byte[] aEncodedChain)
        Creates a ChaosKey from the provided encoded chain representations (as of toEncodedChain()) including all the nested ChaosKey children.
        Parameters:
        aEncodedChain - The byte array representing the ChaosKey chain's encoding (as of toEncodedChain()).
        Returns:
        The reconstructed ChaosKey chain from the provided encoded chain representation.
      • createRndKeyChain

        public static ChaosKey createRndKeyChain​(int aChainLength)
        Creates a random ChaosKey chain, where each ChaosKey references a child ChaosKey (as of getChild()) as defined by the provided chain length and each ChaosKey in the chain is initialized by its unique random values.
        Parameters:
        aChainLength - The number of ChaosKey instances chained behind each other as of getChild()
        Returns:
        the ChaosKey with the according chain length being initialized with random values.
      • createRndKeyChain

        public static ChaosKey createRndKeyChain​(int aChainLength,
                                                 ChaosOptions... aChaosOptions)
        Creates a random ChaosKey chain, where each ChaosKey references a child ChaosKey (as of getChild()) as defined by the provided chain length and each ChaosKey in the chain is initialized by its unique random values.
        Parameters:
        aChainLength - The number of ChaosKey instances chained behind each other as of getChild()
        aChaosOptions - The ChaosOptions to use for each ChaosKey, the first one is used for the top most parent ChaosKey, the others for the succeeding ChaosKey children. The last ChaosOptions element is used for any ChaosKey to be created exceeding the length of the ChaosOptions elements.
        Returns:
        the ChaosKey with the according chain length being initialized with random values.
      • getEncodedLength

        public static int getEncodedLength()
        Returns the length of this ChaosKey when represented as bytes (as of getEncoded()) (excluding the ChaosOptions).
        Returns:
        The number of bytes required to represent a ChaosKey x0, a and s values.
      • addToS

        protected static long addToS​(long s,
                                     long aAddend)
        Adds a value to s so that a result exceeding S_MAX or {S_MIN is wrapped around accordingly and any result inbetween S_POSITIVE_MIN and S_NEGATIVE_MAX is moved past S_POSITIVE_MIN or S_NEGATIVE_MAX accordingly.
        Parameters:
        s - The value to which to add an summand.
        aAddend - The summand to be added to s.
        Returns:
        The accordingly wrapped new s value.
      • toA

        protected static double toA​(int a)
        Creates a valid a double from the provided integer (values from Integer.MIN_VALUE to Integer.MAX_VALUE are allowed.
        Parameters:
        a - The integer as source for a
        Returns:
        The a inside the valid scope.
      • toA

        protected static double toA​(long a)
        Creates a valid a double from the provided long (values from Long.MIN_VALUE to Long.MAX_VALUE are allowed).
        Parameters:
        a - The long as source for a
        Returns:
        The a inside the valid scope.
      • toS

        protected static long toS​(int s)
        Creates a valid s double from the provided integer (values from Integer.MIN_VALUE to Integer.MAX_VALUE are allowed).
        Parameters:
        s - The integer as source for s
        Returns:
        The s inside the valid scope.
      • toS

        protected static long toS​(long s)
        Creates a valid s long from the provided long (values from Long.MIN_VALUE to Long.MAX_VALUE are allowed).
        Parameters:
        s - The long as source for s
        Returns:
        The s inside the valid scope.
      • toX0

        protected static double toX0​(int x0)
        Creates a valid x0 double from the provided integer (values from Integer.MIN_VALUE to Integer.MAX_VALUE are allowed.
        Parameters:
        x0 - The integer as source for x0
        Returns:
        The x0 inside the valid scope.
      • toX0

        protected static double toX0​(long x0)
        Creates a valid x0 double from the provided long (values from Long.MIN_VALUE to Long.MAX_VALUE are allowed).
        Parameters:
        x0 - The long as source for x0
        Returns:
        The x0 inside the valid scope.
      • validate

        protected static void validate​(double x0,
                                       double a,
                                       double s)
        Validates the given values whether them are in scope.
        Parameters:
        x0 - The x0 value to validate.
        a - The a value to validate.
        s - The s value to validate.
      • toCertificateHead

        protected static String toCertificateHead()
        Creates a certificate head for an unsecured certificate.
        Returns:
        The accordingly created certificate head.
      • toCertificateHead

        protected static String toCertificateHead​(ChaosOptions aChaosOptions)
        Creates a certificate head from the provided ChaosOptions instance for a password protected certificate.
        Parameters:
        aChaosOptions - The ChaosOptions from which to create the certificate head.
        Returns:
        The accordingly created certificate head.
      • toCertificateTail

        protected static String toCertificateTail()
        Creates a certificate tail for an unsecured certificate.
        Returns:
        The accordingly created certificate tail.
      • toCertificateTail

        protected static String toCertificateTail​(ChaosOptions aChaosOptions)
        Creates a certificate tail from the provided ChaosOptions instance for a password protected certificate.
        Parameters:
        aChaosOptions - The ChaosOptions from which to create the certificate tail.
        Returns:
        The accordingly created certificate tail.