Package de.das.encrypter.model
Class KeyFile
java.lang.Object
de.das.encrypter.model.KeyFile
- All Implemented Interfaces:
Key
Class, which represents a key with its identifier and current path to
its location.
Instances of this class are created when keys are searched for and found in
the system. However, the class also contains many static methods for
generating new keys and checking the validity of given keys.
The key itself is represented by a byte array headed with a header and filled with random bytes of data. And it is important to note that these are true random numbers and not pseudo-random numbers.
The header structure is the key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier represented as an hexadecimal string, "NN" is the number of following bytes as a 16 Bit integer (little endian) representing the name of the key "nnnnnnn".
The key itself is represented by a byte array headed with a header and filled with random bytes of data. And it is important to note that these are true random numbers and not pseudo-random numbers.
The header structure is the key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier represented as an hexadecimal string, "NN" is the number of following bytes as a 16 Bit integer (little endian) representing the name of the key "nnnnnnn".
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe length of the CRC value appended to the keys.static final intThe length of the key header for key signature and key ID.static final intThe length of the key header plus two for the bytes for the length of the following key name.Fields inherited from interface de.das.encrypter.model.Key
DIVERSITY_AVE_DISTANCES, DIVERSITY_EQUAL_COUNTS, DIVERSITY_RELATIVE_EQUALS, ENCRYPTION_ID, ENCRYPTION_ID_LENGTH, ENCRYPTION_PREFIX_CRC_16_POSITION, ENCRYPTION_PREFIX_ENTRY_POINT_LSB_POSITION, ENCRYPTION_PREFIX_ENTRY_POINT_MSB_POSITION, ENCRYPTION_PREFIX_FILE_NAME_POSITION, ENCRYPTION_PREFIX_ID_POSITION, ENCRYPTION_PREFIX_KEY_ID_POSITION, ENCRYPTION_PREFIX_LENGHT, ENCRYPTION_PREFIX_NAME_LENGTH_POSITION, KB, KEY_DRIVE_MARK, KEY_DRIVE_MARK_FILE_NAME, KEY_HEADER_LENGTH, KEY_ID_LENGHT, KEY_NAME_POSITION, KEY_SIGNATURE_PREFIX, MB, RANDOM_FILE_NAME_LENGTH, RANDOM_FOLDER_PREFIX -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of the class KeyFile.Creates a new instance of the class KeyFile.Creates a new instance of the class KeyFile. -
Method Summary
Modifier and TypeMethodDescriptionstatic Double[]calculateDiversity(byte[] pData_1, byte[] pData_2) Calculates quantities that make statements about the difference between two key bundless.static byte[]Creates a new key using the given byte array for the key data and the given name for the key name.static voidCreates a key file with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier.static voidCreates a key file with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier.static voidCreates a file with a key with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier.static voidCreates a new key using the bytes of a given file.
It is recommended not to use such keys if the encryption is to be 100% secure.static byte[]Creates a key with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier.static voidcreatePseudoRandomKey(File f, int length, long seed) Creates a new key bundle by using pseudo random numbers.static voidcreatePseudoRandomKey(File f, int length, long seed, boolean balanced) Creates a new key bundle by using pseudo random numbers.booleanCompares a given object with this key file object and returns true, if it is.intReturns the CRC value of the key by forming it from its bytes except the last four.getDifferentEntryPoints(double f, byte[] key, int count) Calculates a desired number of random key entry points and makes sure they are all different.getDifferentEntryPoints(double f, byte[] key, int count, ArrayList<Integer> existing) Calculates a desired number of random key entry points and makes sure they are all different.getDrive()Return the drive letter of the medium on which the key is located.longgetId()Returns the ID of the associated key.byte[]getKey()Returns the associated key.static LonggetKeyFileIdentifier(byte[] dataStart) Checks, whether the given data starting part starts with the keyword "KEYFILE_" and decodes the following "XXXXXXXXXXXXXXXX" bytes as the key identifier.
"XXXXXXXXXXXXXXXX" is the string representation of the hex identifier.static LongChecks, whether the content of the given file starts with the keyword "KEYFILE_" and decodes the following "XXXXXXXXXXXXXXXX" bytes as the key identifier.
"XXXXXXXXXXXXXXXX" is the string representation of the hex identifier.static StringgetKeyName(byte[] key) Extracts and returns the key name from the header of the given key.static intgetKeyStartingPoint(byte[] key) Determines the starting point of the key values.intReturns the length of the associated key.getPath()Returns the path of the location of the associated key.byte[]Extracts the pure key data without header from the key of this KeyFile instance.static byte[]getPureKeyData(byte[] key) Extracts the pure key data without header from the given key.intCalculates the length of pure key data.static byte[]harmonicallyBalance(byte[] orgKey, double scat) Performs an analysis of how frequently each byte occurs in the given key bundle and swaps out more frequently occurring bytes for less frequently occurring bytes until the scatter around the mean is minimized or approaches zero.inthashCode()Returns a hash code value for the object.booleanhasKey()Returns true, if this object contains already a key.static Stringstatic booleanisValidKey(byte[] key) Checks whether the specified byte array contains a valid key.static booleanisValidKey(byte[] key, boolean withoutCrcCheck) Checks whether the specified byte array contains a valid key.static booleanisValidKey(File f) Checks whether the specified file contains a valid key.static booleanisValidKey(File f, boolean withoutCheck) Checks whether the specified file contains a valid key.static byte[]provideKeyPart(byte[] key, int start, int length) Provides a specified part from the given key starting at the specified position.static StringrandomFileName(int length) Creates a random file name with a length as specified.static voidreplacePureKey(byte[] key, byte[] pureKey) Replaces the pure key data within the specified key bundle and considers the CRC-32 value.voidsetUsedKeys(Integer used) Sets the number of already used virtual keys.static KeyFileAssumes that the specified file contains a valid key and attempts to read it to create an associated KeyFile instance.toString()Generates and returns a string consisting of the key name followed by information about the drive it is on and its length and how many virtual keys are still available.
-
Field Details
-
HEADER_LENGTH
public static final int HEADER_LENGTHThe length of the key header for key signature and key ID.- See Also:
-
CRC_LENGTH
public static final int CRC_LENGTHThe length of the CRC value appended to the keys.- See Also:
-
HEADER_LENGTH_PLUS_NAME_AMOUNT
public static final int HEADER_LENGTH_PLUS_NAME_AMOUNTThe length of the key header plus two for the bytes for the length of the following key name.- See Also:
-
-
Constructor Details
-
KeyFile
Creates a new instance of the class KeyFile.- Parameters:
id- the ID of the associated key.key- the associated key itself.driveLetter- the letter of the drive on which it is located.
-
KeyFile
Creates a new instance of the class KeyFile.- Parameters:
id- the ID of the associated key.path- the path where the key is located.length- the length of the key.
-
KeyFile
Creates a new instance of the class KeyFile.- Parameters:
id- the ID of the associated key.path- the path where the key is located.length- the length of the key.key- the byte array containing the key with header.
-
-
Method Details
-
getId
public long getId()Returns the ID of the associated key.- Returns:
- the ID of the associated key.
-
getPath
Returns the path of the location of the associated key.- Returns:
- the path of the location of the associated key.
-
getLength
public int getLength()Returns the length of the associated key. It is the length of the key including the header.- Returns:
- the length of the associated key.
-
getKeyFileIdentifier
Checks, whether the given data starting part starts with the keyword "KEYFILE_" and decodes the following "XXXXXXXXXXXXXXXX" bytes as the key identifier.
"XXXXXXXXXXXXXXXX" is the string representation of the hex identifier.- Parameters:
dataStart- a byte array with the starting part of encrypted data.- Returns:
- the key file identifier or null, if the data start does not contain valid information.
-
getKeyFileIdentifier
Checks, whether the content of the given file starts with the keyword "KEYFILE_" and decodes the following "XXXXXXXXXXXXXXXX" bytes as the key identifier.
"XXXXXXXXXXXXXXXX" is the string representation of the hex identifier.- Parameters:
f- a containing encrypted data.- Returns:
- the key identifier or null, if the file does not contain valid information.
-
toString
Generates and returns a string consisting of the key name followed by information about the drive it is on and its length and how many virtual keys are still available. -
create
Creates a new key using the bytes of a given file.
It is recommended not to use such keys if the encryption is to be 100% secure.- Parameters:
target- the file that should contain the key.contentFile- the path to a file to be used as a key.- Throws:
Exception- in case of any error.
-
create
Creates a new key using the given byte array for the key data and the given name for the key name. It is recommended not to use such keys if the encryption shall be 100% secure and the given data are not true randomly.- Parameters:
data- the given byte array.name- the name of the key.- Returns:
- a key to be used for encryption.
-
create
Creates a key file with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier. "XXXXXXXXXXXXXXXX" is the string representation of the hex identifier, "NN" is the number of following bytes representing the name of the key "nnnnnnn".- Parameters:
target- the file that should contain the key.length- the request length of the key.- Throws:
Exception- in case of any error.
-
create
Creates a key file with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier. "XXXXXXXXXXXXXXXX" is the string representation of the hex identifier, "NN" is the number of following bytes representing the name of the key "nnnnnnn".- Parameters:
target- the file that should contain the key.length- the request length of the key.- Throws:
Exception- in case of any error.
-
create
Creates a file with a key with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier. "XXXXXXXXXXXXXXXX" is the string representation of the hex identifier, "NN" is the number of following bytes representing the name of the key "nnnnnnn".- Parameters:
target- the file that will contain the key.length- the request length of the key.seed- a start value (type long) for the key development.balanced- a flag that instructs to use the balancer.- Throws:
Exception- in case of any error.
-
create
Creates a key with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier. "XXXXXXXXXXXXXXXX" is the string representation of the hex identifier, "NN" is the number of following bytes representing the name of the key "nnnnnnn".- Parameters:
name- the name of the key (must be a valid file name.length- the requested key length not smaller than 128.seed- a random start value for the creation algorithm.balanced- a flag that instructs to use the balancer.- Returns:
- a byte array containing the new key.
- Throws:
Exception- in case of any error.
-
createPseudoRandomKey
Creates a new key bundle by using pseudo random numbers. Their advantage as well as their disadvantage is their reproducibility.- Parameters:
f- the file that shall contain the key bundle.length- the required key bundle length.seed- a seed for the developing of different values.- Throws:
Exception- in case of any error.
-
createPseudoRandomKey
public static void createPseudoRandomKey(File f, int length, long seed, boolean balanced) throws Exception Creates a new key bundle by using pseudo random numbers. Their advantage as well as their disadvantage is their reproducibility.- Parameters:
f- the file that shall contain the key bundle.length- the required key bundle length.seed- a seed for the developing of different values.balanced- a flag that instructs to use the balancer.- Throws:
Exception- in case of any error.
-
equals
Compares a given object with this key file object and returns true, if it is. -
hashCode
public int hashCode()Returns a hash code value for the object. -
isValidKey
public static boolean isValidKey(byte[] key) Checks whether the specified byte array contains a valid key.- Parameters:
key- a given byte array. It is necessary that the byte array contains not only the beginning of the key (header) but the whole key, because a CRC check is also performed and the CRC value is located at the end of the key.- Returns:
- true if the specified byte array contains a valid key.
-
isValidKey
public static boolean isValidKey(byte[] key, boolean withoutCrcCheck) Checks whether the specified byte array contains a valid key.- Parameters:
key- a given byte array assumed to be a key. Only the start of the key (header) can be used for checking if the CRC check is omitted.withoutCrcCheck- a flag which determines whether a CRC check should be performed or not.- Returns:
- true if the specified byte array contains a valid key.
-
isValidKey
Checks whether the specified file contains a valid key. The check includes the CRC32 check.- Parameters:
f- a given file that is assumed to contain a key.- Returns:
- true if the specified file contains a valid key.
-
isValidKey
Checks whether the specified file contains a valid key. If the key itself is not needed, but only to check whether the file contains a valid key, only the beginning of the file can be used for checking if the CRC check is omitted, so that only the key headers are used for performance reasons.- Parameters:
f- a given file that is assumed to contain a key.withoutCheck- a flag which determines whether a CRC check should be performed or not.- Returns:
- true if the specified file contains a valid key.
-
getCrcValue
public int getCrcValue()Returns the CRC value of the key by forming it from its bytes except the last four.- Returns:
- the CRC value of the key.
-
getKeyName
Extracts and returns the key name from the header of the given key.- Parameters:
key- a given key or key header.- Returns:
- returns the key name from the header.
-
replacePureKey
public static void replacePureKey(byte[] key, byte[] pureKey) Replaces the pure key data within the specified key bundle and considers the CRC-32 value.- Parameters:
key- a given key bundle.pureKey- the pure key bundle data to be replaced.
-
getPureKeyData
public static byte[] getPureKeyData(byte[] key) Extracts the pure key data without header from the given key.- Parameters:
key- a given key.- Returns:
- the pure key data without header.
-
getPureKeyData
Extracts the pure key data without header from the key of this KeyFile instance.- Returns:
- the pure key data without header.
- Throws:
Exception- in case of any error.
-
getPureKeyDataLength
Calculates the length of pure key data. This is key length minus header length;- Returns:
- the length of pure key data.
- Throws:
Exception- in case of any error.
-
provideKeyPart
public static byte[] provideKeyPart(byte[] key, int start, int length) Provides a specified part from the given key starting at the specified position.- Parameters:
key- a given key.start- the specified starting position.length- the length of the requested part.- Returns:
- the specified part from the given key.
-
getKeyStartingPoint
public static int getKeyStartingPoint(byte[] key) Determines the starting point of the key values. It can be different because the name of the key is part of the header and its length can vary.- Parameters:
key- a given key.- Returns:
- the starting point of the key values.
-
getKey
Returns the associated key. If the key does not exist yet, it will now be tried to read it from the specified location.- Returns:
- the associated key.
- Throws:
Exception- incase of any file error.
-
hasKey
public boolean hasKey()Returns true, if this object contains already a key. It is possible that this key file object initially contains only information about a key but not yet the key itself. If the key itself is not yet contained, an attempt will be made to load it when it is accessed for the first time.- Returns:
- true, if this object contains already a key.
-
randomFileName
Creates a random file name with a length as specified. The name is formed from a string of randomly generated hexadecimal digits.- Parameters:
length- the requested length of the name.- Returns:
- a random file name.
-
getDrive
Return the drive letter of the medium on which the key is located.- Returns:
- the drive letter of the medium on which the key is located.
-
getDifferentEntryPoints
public static ArrayList<Integer> getDifferentEntryPoints(double f, byte[] key, int count, ArrayList<Integer> existing) throws Exception Calculates a desired number of random key entry points and makes sure they are all different. If more than 5% of the possible key entry points are requested, the entry points are created as an incremental sequence starting at the point determined by the random factor. This procedure is done because the time for generation increases too much with a larger request. However, this has not unavoidable disadvantages, since the bytes of the key are real random numbers.
The advantage of getting entry points via this method is that when an entire packet of files is encrypted, they are actually all encrypted with a different key.- Parameters:
f- a given random double factor [0.0 .. 1.0[.key- the pure key data of a given key.count- the number of entry points requested.existing- an array list of already used and forbidden entry points.- Returns:
- an array list with different integer entry points.
- Throws:
Exception- in case of more entry point are requested as the key is able to provide or the random factor is out of range.
-
getDifferentEntryPoints
public static ArrayList<Integer> getDifferentEntryPoints(double f, byte[] key, int count) throws Exception Calculates a desired number of random key entry points and makes sure they are all different. This is the right approach if the key is used to encrypt many files. This ensures that a different key from the keyring was used for each file. If a potential attacker who gets hold of the encrypted data tries to decrypt the files in this bundle, he has not had a chance.- Parameters:
f- a given random double factor [0.0 .. 1.0[.key- a given key.count- the number of entry points requested.- Returns:
- an array list with different integer entry points.
- Throws:
Exception- in case of more entry point are requested as the key is able to provide.
-
takeAsKey
Assumes that the specified file contains a valid key and attempts to read it to create an associated KeyFile instance.- Parameters:
f- a given file.- Returns:
- a KeyFile instance with the key from the given file or null if there is any problem with the file content.
-
setUsedKeys
Sets the number of already used virtual keys. This value must be set by an external key manager and, if present, is used only for the toString method.- Parameters:
used- the number of already used virtual keys.
-
insertThousandsSeparators
-
harmonicallyBalance
public static byte[] harmonicallyBalance(byte[] orgKey, double scat) Performs an analysis of how frequently each byte occurs in the given key bundle and swaps out more frequently occurring bytes for less frequently occurring bytes until the scatter around the mean is minimized or approaches zero.- Parameters:
orgKey- a key bundle to be optimized.scat- the requested scattering factor.- Returns:
- a key bundle whose uniform distribution is optimized.
-
calculateDiversity
Calculates quantities that make statements about the difference between two key bundless. There are three values: The first value indicates how often the same byte values were found at the same positions, the second value indicates how often this is not the case, and the third value calculates the average difference of all byte values at the same positions.- Parameters:
pData_1- one key bundle.pData_2- another key bundle.- Returns:
- the calculated values.
-