Class KeyFile

java.lang.Object
de.das.encrypter.model.KeyFile
All Implemented Interfaces:
Key

public class KeyFile extends Object implements 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".
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The length of the CRC value appended to the keys.
    static final int
    The length of the key header for key signature and key ID.
    static final int
    The length of the key header plus two for the bytes for the length of the following key name.
  • Constructor Summary

    Constructors
    Constructor
    Description
    KeyFile(long id, byte[] key, String driveLetter)
    Creates a new instance of the class KeyFile.
    KeyFile(long id, String path, int length)
    Creates a new instance of the class KeyFile.
    KeyFile(long id, String path, int length, byte[] key)
    Creates a new instance of the class KeyFile.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Double[]
    calculateDiversity(byte[] pData_1, byte[] pData_2)
    Calculates quantities that make statements about the difference between two key bundless.
    static byte[]
    create(byte[] data, String name)
    Creates a new key using the given byte array for the key data and the given name for the key name.
    static void
    create(File target, int length)
    Creates a key file with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier.
    static void
    create(File target, int length, boolean balanced)
    Creates a key file with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier.
    static void
    create(File target, int length, long seed, boolean balanced)
    Creates a file with a key with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier.
    static void
    create(File target, String contentFile)
    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.
    static byte[]
    create(String name, int length, long seed, boolean balanced)
    Creates a key with leading key signature "KEYFILE_XXXXXXXXXXXXXXXXNNnnnnnnnn" where "XXXXXXXXXXXXXXX" is a unique key file identifier.
    static void
    createPseudoRandomKey(File f, int length, long seed)
    Creates a new key bundle by using pseudo random numbers.
    static void
    createPseudoRandomKey(File f, int length, long seed, boolean balanced)
    Creates a new key bundle by using pseudo random numbers.
    boolean
    Compares a given object with this key file object and returns true, if it is.
    int
    Returns 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.
    Return the drive letter of the medium on which the key is located.
    long
    Returns the ID of the associated key.
    byte[]
    Returns the associated key.
    static Long
    getKeyFileIdentifier(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 Long
    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.
    static String
    getKeyName(byte[] key)
    Extracts and returns the key name from the header of the given key.
    static int
    getKeyStartingPoint(byte[] key)
    Determines the starting point of the key values.
    int
    Returns the length of the associated key.
    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.
    int
    Calculates 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.
    int
    Returns a hash code value for the object.
    boolean
    Returns true, if this object contains already a key.
    static String
     
    static boolean
    isValidKey(byte[] key)
    Checks whether the specified byte array contains a valid key.
    static boolean
    isValidKey(byte[] key, boolean withoutCrcCheck)
    Checks whether the specified byte array contains a valid key.
    static boolean
    Checks whether the specified file contains a valid key.
    static boolean
    isValidKey(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 String
    randomFileName(int length)
    Creates a random file name with a length as specified.
    static void
    replacePureKey(byte[] key, byte[] pureKey)
    Replaces the pure key data within the specified key bundle and considers the CRC-32 value.
    void
    Sets the number of already used virtual keys.
    static KeyFile
    Assumes that the specified file contains a valid key and attempts to read it to create an associated KeyFile instance.
    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.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • HEADER_LENGTH

      public static final int HEADER_LENGTH
      The length of the key header for key signature and key ID.
      See Also:
    • CRC_LENGTH

      public static final int CRC_LENGTH
      The length of the CRC value appended to the keys.
      See Also:
    • HEADER_LENGTH_PLUS_NAME_AMOUNT

      public static final int HEADER_LENGTH_PLUS_NAME_AMOUNT
      The length of the key header plus two for the bytes for the length of the following key name.
      See Also:
  • Constructor Details

    • KeyFile

      public KeyFile(long id, byte[] key, String driveLetter)
      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

      public KeyFile(long id, String path, int length)
      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

      public KeyFile(long id, String path, int length, byte[] key)
      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

      public String 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

      public static Long getKeyFileIdentifier(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.
      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

      public static Long getKeyFileIdentifier(File f)
      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

      public String 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.
      Overrides:
      toString in class Object
      Returns:
      a string consisting of the key name followed by information about the drive it is on and its length.
    • create

      public static void create(File target, String contentFile) throws Exception
      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

      public static byte[] create(byte[] data, String name)
      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

      public static void create(File target, int length) throws Exception
      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

      public static void create(File target, int length, boolean balanced) throws Exception
      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

      public static void create(File target, int length, long seed, boolean balanced) throws Exception
      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

      public static byte[] create(String name, int length, long seed, boolean balanced) throws Exception
      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

      public static void createPseudoRandomKey(File f, int length, long seed) 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.
      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

      public boolean equals(Object obj)
      Compares a given object with this key file object and returns true, if it is.
      Overrides:
      equals in class Object
      Parameters:
      obj - a given object.
      Returns:
      true, if it is.
    • hashCode

      public int hashCode()
      Returns a hash code value for the object.
      Overrides:
      hashCode in class Object
      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

      public static boolean isValidKey(File f)
      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

      public static boolean isValidKey(File f, boolean withoutCheck)
      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

      public static String getKeyName(byte[] key)
      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

      public byte[] getPureKeyData() throws Exception
      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

      public int getPureKeyDataLength() throws Exception
      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

      public byte[] getKey() throws Exception
      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

      public static String randomFileName(int length)
      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

      public String 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

      public static KeyFile takeAsKey(File f)
      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

      public void setUsedKeys(Integer used)
      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

      public static String insertThousandsSeparators(String str)
    • 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

      public static Double[] calculateDiversity(byte[] pData_1, byte[] pData_2)
      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.