Package i5.las2peer.tools
Class CryptoTools
java.lang.Object
i5.las2peer.tools.CryptoTools
Simple static class collecting useful cryptographic methods end encapsulating the access to the underlying
cryptografic library.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Serializable
decryptAsymmetric
(byte[] data, PrivateKey key) decrypt the given content with the given private key and try to deserialize the resulting byte arraystatic byte[]
decryptSymmetric
(byte[] baCipherData, SecretKey key) decrypt a symmetrically encrypted byte block using the given keystatic Serializable
depryptPassphaseObject
(byte[] content, byte[] salt, String passphrase) descrypt (and deserialize) the given encrypted data using the given passphrase and saltstatic byte[]
encryptAsymmetric
(byte[] content, PublicKey key) encrypt the given data asymmetrically using the given public keystatic byte[]
encryptAsymmetric
(Serializable content, PublicKey key) encrypt the given data after serialization using the given public keystatic byte[]
encryptSymmetric
(byte[] baPlainData, SecretKey symmetricKey) encrypt the given data symmetrically with the given keystatic byte[]
encryptSymmetric
(Serializable plainData, SecretKey key) encrypt the given object after serialization with the givne keystatic byte[]
encryptWithPassphrase
(Serializable object, String passphrase, byte[] salt) encrypt a serializable object using the given passphrase an salt make sure to use real random salts e.g.static SecretKey
generateKeyForPassphrase
(String passphrase, byte[] salt) generate a symmetric key for the given passphrase using the given salt make sure to use real random salts e.g.static KeyPair
generate a new asymmetric key pairstatic byte[]
generate a random saltstatic SecretKey
generate a new key for the symmetric crypto operations of this classstatic String
get the asymmetric encryption algorithm in usestatic int
static String
used hash methodstatic byte[]
getSecureHash
(byte[] data) static String
get the signature method in usestatic String
get the symmetric algorithm in usestatic String
get the factory method for symmetric keysstatic boolean
static void
main (command line) method: create a key pair in the given file name prefixstatic String
static String
static String
publicKeyToSHA512
(PublicKey publicKey) static void
setAsymmetricKeySize
(int size) set the preferred size for asymmetric keysstatic void
setSymmetricKeySize
(int size) set the preferred size for symmetric keysstatic byte[]
signContent
(byte[] content, PrivateKey key) sign the given content with the given private keystatic PrivateKey
stringToPrivateKey
(String base64) static PublicKey
stringToPublicKey
(String base64) static boolean
verifySignature
(byte[] signature, byte[] content, PublicKey key) tries to verify the given signature of the given content with the given public key
-
Constructor Details
-
CryptoTools
public CryptoTools()
-
-
Method Details
-
getHashMethod
used hash method- Returns:
- used hash method
-
getAsymmetricAlgorithm
get the asymmetric encryption algorithm in use- Returns:
- asymetric algorithm
-
getSymmetricAlgorithm
get the symmetric algorithm in use- Returns:
- symetric algorithm
-
getSignatureMethod
get the signature method in use- Returns:
- signature method
-
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 thegenerateSalt()
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 thegenerateSalt()
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
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
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
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
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
generate a new key for the symmetric crypto operations of this class- Returns:
- new symmetric key
-
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
- Throws:
CryptoException
-
stringToPublicKey
- Throws:
CryptoException
-
privateKeyToBase64String
- Throws:
CryptoException
-
publicKeyToBase64String
- Throws:
CryptoException
-
publicKeyToSHA512
-
getSecureHash
- Throws:
CryptoException
-
isAgentID
-
main
main (command line) method: create a key pair in the given file name prefix- Parameters:
argv
- See usage output for details.
-