Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// /////////////////////////////////////////////////////////////////////////////
// REFCODES.ORG
// /////////////////////////////////////////////////////////////////////////////
// This code is copyright (c) by Siegfried Steiner, Munich, Germany, distributed
// on an "AS IS" BASIS WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, and licen-
// sed under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
// licenses:
// -----------------------------------------------------------------------------
// GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
// -----------------------------------------------------------------------------
// Apache License, v2.0 ("http://www.apache.org/licenses/TEXT-2.0")
// -----------------------------------------------------------------------------
// Please contact the copyright holding author(s) of the software artifacts in
// question for licensing issues not being covered by the above listed licenses,
// also regarding commercial licensing models or regarding the compatibility
// with other open source licenses.
// /////////////////////////////////////////////////////////////////////////////
package org.refcodes.security.alt.chaos;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.SecretKey;
import javax.security.auth.DestroyFailedException;
import org.refcodes.codec.BaseDecoderInputStream;
import org.refcodes.codec.BaseEncoderOutputStream;
import org.refcodes.codec.BaseMetricsConfig;
import org.refcodes.data.Numbers;
import org.refcodes.exception.BugException;
import org.refcodes.io.LineBreakOutputStream;
import org.refcodes.mixin.ChildAccessor;
import org.refcodes.mixin.Disposable;
import org.refcodes.mixin.EncodedAccessor;
import org.refcodes.numerical.Endianess;
import org.refcodes.numerical.NumericalUtility;
/**
* 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"
*/
public class ChaosKey implements SecretKey, EncodedAccessor, ChildAccessor, Disposable {
private static final long serialVersionUID = 1L;
// /////////////////////////////////////////////////////////////////////////
// STATICS:
// /////////////////////////////////////////////////////////////////////////
private static final Logger LOGGER = Logger.getLogger( ChaosKey.class.getName() );
// /////////////////////////////////////////////////////////////////////////
// CONSTANTS:
// /////////////////////////////////////////////////////////////////////////
/**
* The name of the security provider.
*/
public static final String PROVIDER_NAME = "CHAOS";
/**
* The number of bytes required to store A
*/
public static final int A_BYTES = Double.BYTES;
/**
* The maximum valid A value.
*/
public static final double A_MAX = 4;
/**
* The minimum valid A value.
*/
public static final double A_MIN = 3.57;
/**
* The number of bytes required to store S
*/
public static final int S_BYTES = Numbers.SAFE_INTEGER_BYTES;
/**
* The maximum valid S value (2^53-1 = MAX_SAFE_INTEGER when floating point
* is involved).
*/
public static final long S_MAX = Numbers.MAX_SAFE_INTEGER;
/**
* The minimum valid S value (-2^53 = MIN_SAFE_INTEGER when floating point
* is involved).
*/
public static final long S_MIN = Numbers.MIN_SAFE_INTEGER;
/**
* The maximum negative S value ({@link #S_NEGATIVE_MAX} x
* {@link #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).
*/
public static final long S_NEGATIVE_MAX = -16;
/**
* The minimum positive S value ({@link #S_NEGATIVE_MAX} x
* {@link #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).
*/
public static final long S_POSITIVE_MIN = 16;
/**
* The number of bytes required to store X
*/
public static final int X_BYTES = Double.BYTES; // The number of bytes required to store X
/**
* The maximum valid X value.
*/
public static final double X_MAX = 1;
/**
* The minimum valid X value.
*/
public static final double X_MIN = 0;
/**
* The overall encoded length in bytes of the {@link ChaosKey} including the
* {@link ChaosOptions}.
*/
public static final int ENCODED_LENGTH = X_BYTES + A_BYTES + S_BYTES + ChaosOptions.ENCODED_LENGTH;
// -------------------------------------------------------------------------
private static final long S_MAX_RANGE = S_MAX - S_POSITIVE_MIN;
private static final long S_MIN_RANGE = S_MIN - S_NEGATIVE_MAX;
// -------------------------------------------------------------------------
private static final int CERTIFICATE_LINE_WIDTH = 80;
private static final String CERTIFICATE_BEGIN_TAG_PREFIX = "-----BEGIN";
private static final String CERTIFICATE_BEGIN_TAG_SUFFIX = "CHAOS-KEY-CHAIN-----";
private static final String CERTIFICATE_ENCRYPTED = "ENCRYPTED";
private static final String CERTIFICATE_SALTED = "SALTED";
private static final String CERTIFICATE_MUTATE = "MUTATE";
private static final String CERTIFICATE_XOR = "XOR";
private static final String CERTIFICATE_END_TAG_PREFIX = "-----END";
private static final String CERTIFICATE_END_TAG_SUFFIX = "CHAOS-KEY-CHAIN-----";
private static final String PLAIN_CERTIFICATE_BEGIN = CERTIFICATE_BEGIN_TAG_PREFIX + " " + CERTIFICATE_BEGIN_TAG_SUFFIX;
private static final String PLAIN_CERTIFICATE_END = CERTIFICATE_END_TAG_PREFIX + " " + CERTIFICATE_END_TAG_SUFFIX;
// /////////////////////////////////////////////////////////////////////////
// VARIABLES:
// /////////////////////////////////////////////////////////////////////////
private double _x0;
private double _a;
private long _s;
private ChaosOptions _chaosOptions;
private ChaosKey _childKey = null;
// /////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS:
// /////////////////////////////////////////////////////////////////////////
/**
* Instantiates the {@link ChaosKey} by reversing the result of the
* {@link #getEncoded()} operation where the byte array contains the values
* x0, a and s (excluding the {@link ChaosOptions}). The values use a big
* endian representation. The byte array being passed is to be of the size
* as returned by {@link #getEncodedLength()}. The number of bytes required
* by each value in the byte array are defined in the constants
* {@value #X_BYTES}, {@link #A_BYTES} and {@link #S_BYTES} respectively and
* in that order.
*
* @param aEncoded The encoded representation of the chaos key.
*/
public ChaosKey( byte[] aEncoded ) {
this( aEncoded, null );
}
/**
* Instantiates a new {@link ChaosKey} directly from the provided double
* values. Make sure your provided arguments are within the allowed bounds;
*
* @param x0 the x0: (0 <= x0 <= 1 )
* @param a the a: (a <= 3.57 <= 4 )
* @param s the s: ({@link Integer#MAX_VALUE} >= s
* >={@link Integer#MAX_VALUE} AND 16 <= s <= -16)
*/
public ChaosKey( double x0, double a, long s ) {
this( x0, a, s, ChaosMode.NONE, null );
}
/**
* Instantiates a new {@link ChaosKey} directly from the provided double
* values. Make sure your provided arguments are within the allowed bounds;
*
* @param x0 the x0: (0 <= x0 <= 1 )
* @param a the a: (a <= 3.57 <= 4 )
* @param s the s: ({@link Integer#MAX_VALUE} >= s
* >={@link Integer#MAX_VALUE} AND 16 <= s <= -16)
* @param aChaosOptions The {@link ChaosMode} ({@link ChaosOptions}) to use
* to enhance Chaos-based encryption by enabling various additional
* processing steps.
*/
public ChaosKey( double x0, double a, long s, ChaosOptions aChaosOptions ) {
this( x0, a, s, aChaosOptions, null );
}
/**
* Instantiates a new {@link ChaosKey} from the provided integer values. The
* integers are converted to the valid ranges of double values for invoking
* the constructor {@link #ChaosKey(double, double, long)}. You can use
* values in the range of {@link Integer#MIN_VALUE} and
* {@link Integer#MAX_VALUE}.
*
* @param x0 The value from which to calculate the valid x0 double.
* @param a The value from which to calculate the valid a double.
* @param s The value from which to calculate the valid s double.
*/
public ChaosKey( int x0, int a, int s ) {
this( x0, a, s, ChaosMode.NONE, null );
}
/**
* Instantiates a new {@link ChaosKey} from the provided integer values. The
* integers are converted to the valid ranges of double values for invoking
* the constructor {@link #ChaosKey(double, double, long)}. You can use
* values in the range of {@link Integer#MIN_VALUE} and
* {@link Integer#MAX_VALUE}.
*
* @param x0 The value from which to calculate the valid x0 double.
* @param a The value from which to calculate the valid a double.
* @param s The value from which to calculate the valid s double.
* @param aChaosOptions The {@link ChaosMode} ({@link ChaosOptions}) to use
* to enhance Chaos-based encryption by enabling various additional
* processing steps.
*/
public ChaosKey( int x0, int a, int s, ChaosOptions aChaosOptions ) {
this( x0, a, s, aChaosOptions, null );
}
/**
* Instantiates a new {@link ChaosKey} by calculating x0, a and s from the
* provided char array.
*
* @param aSecret The char array (in contrast to a {@link 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.
*/
public ChaosKey( char[] aSecret ) {
this( aSecret, ChaosMode.NONE, null );
}
/**
* Instantiates a new {@link ChaosKey} by calculating x0, a and s from the
* provided {@link String}.
*
* @param aSecret The {@link String} from which to calculate x0, a and s.
*/
public ChaosKey( String aSecret ) {
this( aSecret, ChaosMode.NONE, null );
}
/**
* Instantiates a new {@link ChaosKey} by calculating x0, a and s from the
* provided char array.
*
* @param aSecret The char array (in contrast to a {@link 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.
*
* @param aChaosOptions The {@link ChaosMode} ({@link ChaosOptions}) to use
* to enhance Chaos-based encryption.
*/
public ChaosKey( char[] aSecret, ChaosOptions aChaosOptions ) {
this( aSecret, aChaosOptions, null );
}
/**
* Instantiates a new {@link ChaosKey} by calculating x0, a and s from the
* provided {@link String}.
*
* @param aSecret The {@link String} from which to calculate x0, a and s.
*
* @param aChaosOptions The {@link ChaosMode} ({@link ChaosOptions}) to use
* to enhance Chaos-based encryption.
*/
public ChaosKey( String aSecret, ChaosOptions aChaosOptions ) {
this( aSecret, aChaosOptions, null );
}
// -------------------------------------------------------------------------
/**
* Instantiates a new {@link ChaosKey} directly from the provided double
* values. Make sure your provided arguments are within the allowed bounds;
*
* @param x0 the x0: (0 <= x0 <= 1 )
* @param a the a: (a <= 3.57 <= 4 )
* @param s the s: ({@link Integer#MAX_VALUE} >= s
* >={@link Integer#MAX_VALUE} AND 16 <= s <= -16)
* @param aChildKey The child {@link ChaosKey} being the successor of this
* {@link ChaosKey} to encrypt and the predecessor of this
* {@link ChaosKey} to decrypt.
*/
public ChaosKey( double x0, double a, long s, ChaosKey aChildKey ) {
this( x0, a, s, ChaosMode.NONE, aChildKey );
}
/**
* Instantiates a new {@link ChaosKey} from the provided integer values. The
* integers are converted to the valid ranges of double values for invoking
* the constructor {@link #ChaosKey(double, double, long)}. You can use
* values in the range of {@link Integer#MIN_VALUE} and
* {@link Integer#MAX_VALUE}.
*
* @param x0 The value from which to calculate the valid x0 double.
* @param a The value from which to calculate the valid a double.
* @param s The value from which to calculate the valid s double.
* @param aChildKey The child {@link ChaosKey} being the successor of this
* {@link ChaosKey} to encrypt and the predecessor of this
* {@link ChaosKey} to decrypt.
*/
public ChaosKey( int x0, int a, int s, ChaosKey aChildKey ) {
this( x0, a, s, ChaosMode.NONE, aChildKey );
}
/**
* Instantiates a new {@link ChaosKey} from the provided integer values. The
* integers are converted to the valid ranges of double values for invoking
* the constructor {@link #ChaosKey(double, double, long)}. You can use
* values in the range of {@link Integer#MIN_VALUE} and
* {@link Integer#MAX_VALUE}.
*
* @param x0 The value from which to calculate the valid x0 double.
* @param a The value from which to calculate the valid a double.
* @param s The value from which to calculate the valid s double.
* @param aChaosOptions The {@link ChaosMode} ({@link ChaosOptions}) to use
* to enhance Chaos-based encryption by enabling various additional
* processing steps.
* @param aChildKey The child {@link ChaosKey} being the successor of this
* {@link ChaosKey} to encrypt and the predecessor of this
* {@link ChaosKey} to decrypt.
*/
public ChaosKey( int x0, int a, int s, ChaosOptions aChaosOptions, ChaosKey aChildKey ) {
this( toX0( x0 ), toA( a ), toS( s ), aChaosOptions, aChildKey );
}
/**
* Instantiates a new {@link ChaosKey} by calculating x0, a and s from the
* provided char array.
*
* @param aSecret The char array (in contrast to a {@link 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.
* @param aChildKey The child {@link ChaosKey} being the successor of this
* {@link ChaosKey} to encrypt and the predecessor of this
* {@link ChaosKey} to decrypt.
*/
public ChaosKey( char[] aSecret, ChaosKey aChildKey ) {
this( aSecret, ChaosMode.NONE, aChildKey );
}
/**
* Instantiates a new {@link ChaosKey} by calculating x0, a and s from the
* provided {@link String}.
*
* @param aSecret The {@link String} from which to calculate x0, a and s.
* @param aChildKey The child {@link ChaosKey} being the successor of this
* {@link ChaosKey} to encrypt and the predecessor of this
* {@link ChaosKey} to decrypt.
*/
public ChaosKey( String aSecret, ChaosKey aChildKey ) {
this( aSecret, ChaosMode.NONE, aChildKey );
}
/**
* Instantiates the {@link ChaosKey} by reversing the result of the
* {@link #getEncoded()} operation where the byte array contains the values
* x0, a and s (including the {@link ChaosOptions#getEncoded()}). The values
* use a big endian representation. The byte array being passed is to be of
* the size as returned by {@link #getEncodedLength()}. The number of bytes
* required by each value in the byte array are defined in the constants
* {@value #X_BYTES}, {@link #A_BYTES} and {@link #S_BYTES} respectively and
* in that order.
*
* @param aEncoded The encoded representation of the chaos key.
* @param aChildKey The child {@link ChaosKey} being the successor of this
* {@link ChaosKey} to encrypt and the predecessor of this
* {@link ChaosKey} to decrypt.
*/
public ChaosKey( byte[] aEncoded, ChaosKey aChildKey ) {
final byte[] theX0 = new byte[X_BYTES];
final byte[] theA = new byte[A_BYTES];
final byte[] theS = new byte[S_BYTES];
final byte[] theOptions = new byte[ChaosOptions.getEncodedLength()];
System.arraycopy( aEncoded, 0, theX0, 0, X_BYTES );
System.arraycopy( aEncoded, X_BYTES, theA, 0, A_BYTES );
System.arraycopy( aEncoded, X_BYTES + A_BYTES, theS, 0, S_BYTES );
System.arraycopy( aEncoded, X_BYTES + A_BYTES + S_BYTES, theOptions, 0, ChaosOptions.getEncodedLength() );
final ChaosOptions theChaosOptions = new ChaosOptionsImpl( theOptions );
init( Endianess.BIG.toDouble( theX0 ), Endianess.BIG.toDouble( theA ), Endianess.BIG.toLong( theS ), theChaosOptions, aChildKey );
}
/**
* Instantiates a new {@link ChaosKey} directly from the provided double
* values. Make sure your provided arguments are within the allowed bounds;
*
* @param x0 the x0: (0 <= x0 <= 1 )
* @param a the a: (a <= 3.57 <= 4 )
* @param s the s: ({@link Integer#MAX_VALUE} >= s
* >={@link Integer#MAX_VALUE} AND 16 <= s <= -16)
* @param aChaosOptions The {@link ChaosMode} ({@link ChaosOptions}) to use
* to enhance Chaos-based encryption by enabling various additional
* processing steps.
* @param aChildKey The child {@link ChaosKey} being the successor of this
* {@link ChaosKey} to encrypt and the predecessor of this
* {@link ChaosKey} to decrypt.
*/
public ChaosKey( double x0, double a, long s, ChaosOptions aChaosOptions, ChaosKey aChildKey ) {
init( x0, a, s, aChaosOptions, aChildKey );
}
/**
* Instantiates a new {@link ChaosKey} by calculating x0, a and s from the
* provided char array.
*
* @param aSecret The char array (in contrast to a {@link 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.
*
* @param aChaosOptions The {@link ChaosMode} ({@link ChaosOptions}) to use
* to enhance Chaos-based encryption.
* @param aChildKey The child {@link ChaosKey} being the successor of this
* {@link ChaosKey} to encrypt and the predecessor of this
* {@link ChaosKey} to decrypt.
*/
public ChaosKey( char[] aSecret, ChaosOptions aChaosOptions, ChaosKey aChildKey ) {
final int[] theIds = NumericalUtility.toHashCodes( aSecret, 3 );
init( toX0( theIds[0] ), toA( theIds[1] ), toS( theIds[2] ), aChaosOptions, aChildKey );
}
/**
* Instantiates a new {@link ChaosKey} by calculating x0, a and s from the
* provided {@link String}.
*
* @param aSecret The {@link String} from which to calculate x0, a and s.
*
* @param aChaosOptions The {@link ChaosMode} ({@link ChaosOptions}) to use
* to enhance Chaos-based encryption.
* @param aChildKey The child {@link ChaosKey} being the successor of this
* {@link ChaosKey} to encrypt and the predecessor of this
* {@link ChaosKey} to decrypt.
*/
public ChaosKey( String aSecret, ChaosOptions aChaosOptions, ChaosKey aChildKey ) {
final int[] theIds = NumericalUtility.toHashCodes( aSecret, 3 );
init( toX0( theIds[0] ), toA( theIds[1] ), toS( theIds[2] ), aChaosOptions, aChildKey );
}
// -------------------------------------------------------------------------
private void init( double x0, double a, long s, ChaosOptions aChaosOptions, ChaosKey aChildKey ) {
validate( x0, a, s );
_x0 = x0;
_a = a;
_s = s;
_chaosOptions = aChaosOptions;
_childKey = aChildKey;
if ( x0 == X_MIN ) {
LOGGER.log( Level.WARNING, "The provided value <" + X_MIN + "> causes degenerated security ( =