Class CryptoTools

java.lang.Object
i5.las2peer.tools.CryptoTools

public class CryptoTools extends Object
Simple static class collecting useful cryptographic methods end encapsulating the access to the underlying cryptografic library.
  • Constructor Details

    • CryptoTools

      public CryptoTools()
  • Method Details

    • getHashMethod

      public static String getHashMethod()
      used hash method
      Returns:
      used hash method
    • getAsymmetricAlgorithm

      public static String getAsymmetricAlgorithm()
      get the asymmetric encryption algorithm in use
      Returns:
      asymetric algorithm
    • getSymmetricAlgorithm

      public static String getSymmetricAlgorithm()
      get the symmetric algorithm in use
      Returns:
      symetric algorithm
    • getSignatureMethod

      public static String getSignatureMethod()
      get the signature method in use
      Returns:
      signature method
    • getSymmetricKeygenMethod

      public static String getSymmetricKeygenMethod()
      get the factory method for symmetric keys
      Returns:
      factory method in use
    • setAsymmetricKeySize

      public static void setAsymmetricKeySize(int size)
      set the preferred size for asymmetric keys
      Parameters:
      size - The key size that is used to create asymmetric keys.
    • getAsymmetricKeySize

      public static int getAsymmetricKeySize()
    • setSymmetricKeySize

      public static void setSymmetricKeySize(int size)
      set the preferred size for symmetric keys
      Parameters:
      size - The key size that is used to create symmetric keys.
    • generateKeyForPassphrase

      public static SecretKey generateKeyForPassphrase(String passphrase, byte[] salt) throws CryptoException
      generate a symmetric key for the given passphrase using the given salt make sure to use real random salts e.g. via the generateSalt() method
      Parameters:
      passphrase - The secret that is used to generate the key.
      salt - A salt that is used with the given passphrase.
      Returns:
      a symmetric key for the given passphrase
      Throws:
      CryptoException - If the selected algorithm does not exist or an issue with the given key occurs.
    • encryptWithPassphrase

      public static byte[] encryptWithPassphrase(Serializable object, String passphrase, byte[] salt) throws CryptoException, SerializationException
      encrypt a serializable object using the given passphrase an salt make sure to use real random salts e.g. via the generateSalt() method
      Parameters:
      object - The data that is encrypted.
      passphrase - The secret that is used to encrypt the given data.
      salt - A salt that is used with the given passphrase.
      Returns:
      encrypted content
      Throws:
      CryptoException - If an issue occurs with encryption.
      SerializationException - If an issue occurs with deserializing the given data.
    • depryptPassphaseObject

      public static Serializable depryptPassphaseObject(byte[] content, byte[] salt, String passphrase) throws CryptoException, SerializationException
      descrypt (and deserialize) the given encrypted data using the given passphrase and salt
      Parameters:
      content - The data that is decrypted.
      salt - A salt that is used with the given passphrase.
      passphrase - The secret that is used to decrypt the given data.
      Returns:
      decrypted and deserialized content
      Throws:
      CryptoException - If an issue occurs with decryption.
      SerializationException - If an issue occurs with deserializing the given data.
    • generateSalt

      public static byte[] generateSalt() throws CryptoException
      generate a random salt
      Returns:
      a random salt for later use
      Throws:
      CryptoException - If the selected salt algorithm does not exist.
    • decryptAsymmetric

      public static Serializable decryptAsymmetric(byte[] data, PrivateKey key) throws SerializationException, CryptoException
      decrypt the given content with the given private key and try to deserialize the resulting byte array
      Parameters:
      data - The encrypted data that is decrypted.
      key - The key that is used to decrypt the given data.
      Returns:
      decrypted and deserialized content as java object
      Throws:
      SerializationException - If an issue occurs with deserializing the given data.
      CryptoException - If an decryption issue occurs.
    • decryptSymmetric

      public static byte[] decryptSymmetric(byte[] baCipherData, SecretKey key) throws CryptoException
      decrypt a symmetrically encrypted byte block using the given key
      Parameters:
      baCipherData - The encrypted data that is decrypted.
      key - The key that is used to decrypt the given data.
      Returns:
      decrypted content as byte array
      Throws:
      CryptoException - If an issue occurs with decryption.
    • encryptAsymmetric

      public static byte[] encryptAsymmetric(Serializable content, PublicKey key) throws CryptoException, SerializationException
      encrypt the given data after serialization using the given public key
      Parameters:
      content - The object that is encrypted.
      key - The key that is used to encrypt the given object.
      Returns:
      encrypted content as byte array
      Throws:
      CryptoException - If an issue occurs with encryption.
      SerializationException - If an issue occurs with deserializing the given data.
    • encryptAsymmetric

      public static byte[] encryptAsymmetric(byte[] content, PublicKey key) throws CryptoException
      encrypt the given data asymmetrically using the given public key
      Parameters:
      content - The object that is encrypted.
      key - The key that is used to encrypt the given object.
      Returns:
      encrypted content as byte array
      Throws:
      CryptoException - If an issue occurs with encryption.
    • signContent

      public static byte[] signContent(byte[] content, PrivateKey key) throws CryptoException
      sign the given content with the given private key
      Parameters:
      content - The content that is signed with the given key.
      key - The key that is used to sign the given content.
      Returns:
      signature as byte array
      Throws:
      CryptoException - If an issue occurs with the given key or selected algorithm.
    • verifySignature

      public static boolean verifySignature(byte[] signature, byte[] content, PublicKey key) throws VerificationFailedException
      tries to verify the given signature of the given content with the given public key
      Parameters:
      signature - The (possibly malicious) signature that is attached to the content.
      content - The (possibly malicious) content that is verified.
      key - The key that is verfied as the trusted signer.
      Returns:
      true, if verification is successful
      Throws:
      VerificationFailedException - If an issue occurs with the given key or selected algorithm.
    • generateSymmetricKey

      public static SecretKey generateSymmetricKey()
      generate a new key for the symmetric crypto operations of this class
      Returns:
      new symmetric key
    • generateKeyPair

      public static KeyPair generateKeyPair()
      generate a new asymmetric key pair
      Returns:
      new key pair
    • encryptSymmetric

      public static byte[] encryptSymmetric(byte[] baPlainData, SecretKey symmetricKey) throws CryptoException
      encrypt the given data symmetrically with the given key
      Parameters:
      baPlainData - The data that is encrypted.
      symmetricKey - The key that is used to encrypt the given data.
      Returns:
      encrypted content as byte array
      Throws:
      CryptoException - If an issue occurs with encryption.
    • encryptSymmetric

      public static byte[] encryptSymmetric(Serializable plainData, SecretKey key) throws CryptoException, SerializationException
      encrypt the given object after serialization with the givne key
      Parameters:
      plainData - The data that is encrypted.
      key - The key that is used to encrypt the given data.
      Returns:
      encrypted content as byte array
      Throws:
      CryptoException - If an issue occurs with encryption.
      SerializationException - If an issue occurs with deserializing the given data.
    • stringToPrivateKey

      public static PrivateKey stringToPrivateKey(String base64) throws CryptoException
      Throws:
      CryptoException
    • stringToPublicKey

      public static PublicKey stringToPublicKey(String base64) throws CryptoException
      Throws:
      CryptoException
    • privateKeyToBase64String

      public static String privateKeyToBase64String(PrivateKey priv) throws CryptoException
      Throws:
      CryptoException
    • publicKeyToBase64String

      public static String publicKeyToBase64String(PublicKey publ) throws CryptoException
      Throws:
      CryptoException
    • publicKeyToSHA512

      public static String publicKeyToSHA512(PublicKey publicKey)
    • getSecureHash

      public static byte[] getSecureHash(byte[] data) throws CryptoException
      Throws:
      CryptoException
    • isAgentID

      public static boolean isAgentID(String idString)
    • main

      public static void main(String[] argv)
      main (command line) method: create a key pair in the given file name prefix
      Parameters:
      argv - See usage output for details.