Package net.aifusion.utils
Class EncryptionUtility
java.lang.Object
net.aifusion.utils.EncryptionUtility
Utility class to manage message encryption and signing
- Author:
- Sharad Singhal
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Algorithm use to compute message digestsstatic final String
Algorithm used to encrypt symmetric keysstatic final int
static final int
Key length (in bits) used for generating the wrapping keysstatic final String
Algorithm used for generating public keys for signingstatic final int
Iteration count for generating secret keystatic final String
Algorithm used to transform passwords+salts to secret keysstatic final int
Default length of seedsstatic final String
Algorithm used to generate message signaturesstatic final int
Key length (in bits) of the symmetric keysstatic final String
Algorithm used to encrypt messagesstatic final String
Encryption/Decryption mode used to encrypt messages with the symmetric key -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
decrypt
(byte[] cipherText, char[] password) Decrypt some cipherText that was encrypted using password-based encryption.static byte[]
decrypt
(byte[] cipherText, PrivateKey decryptionKey) Decrypt some ciphertext using a private decryption keystatic byte[]
Decrypt some cipherText that was encrypted using secret key-based encryptionstatic void
decrypt
(CipherInputStream input, OutputStream output, SecretKey key) static byte[]
decryptAndVerify
(PrivateKey decryptionKey, byte[] receivedCipherText) Decrypt a message, and verify the signature on it.static byte[]
encrypt
(byte[] plainText, char[] password) Encrypt some plain text using a password.static byte[]
Encrypt some plaintext using a public encryption keystatic byte[]
Encrypt some plain text using secret key-based encryptionstatic void
encrypt
(InputStream input, CipherOutputStream output, SecretKey key) static KeyPair
Generate a private/public key pair usable by this classstatic KeyPair
generatePublicKeyPair
(byte[] privateKeyData, byte[] publicKeyData) Generate the private/public keys from encoded key datastatic SecretKey
Generate a secret keystatic SecretKey
generateSecretKey
(byte[] salt, char[] password) Generate a password-based secret key.static byte[]
getHash
(byte[] passPhrase) Hash a pass phrase.static byte[]
sign
(PrivateKey signatureKey, byte[] plainText) Sign some plainText using a signature keystatic byte[]
signAndEncrypt
(KeyPair senderKeys, byte[] plainText, PublicKey receiverEncryptionKey) Sign and encrypt a message for transmission to a receiver.static SecretKey
unwrap
(byte[] wrappedKey, PrivateKey decryptionKey) Unwrap a secret key using the receiver's private decryption keystatic boolean
validateHash
(byte[] expectedHash, byte[] passPhrase) Validate if a passPhrase matches a hashstatic boolean
Verify the signature on some signed plainTextstatic byte[]
Wrap a secret key using a receiver's public encryption key
-
Field Details
-
MIN_BUF_SIZE
public static final int MIN_BUF_SIZE- See Also:
-
signatureAlgorithm
Algorithm used to generate message signatures- See Also:
-
symmetricKeyAlgorithm
Algorithm used to encrypt messages- See Also:
-
encryptionAlgorithm
Algorithm used to encrypt symmetric keys- See Also:
-
publicKeyAlgorithm
Algorithm used for generating public keys for signing- See Also:
-
symmetricKeyCryptoMode
Encryption/Decryption mode used to encrypt messages with the symmetric key- See Also:
-
SYMMETRIC_KEY_LENGTH
public static final int SYMMETRIC_KEY_LENGTHKey length (in bits) of the symmetric keys- See Also:
-
PUBLIC_KEY_LENGTH
public static final int PUBLIC_KEY_LENGTHKey length (in bits) used for generating the wrapping keys- See Also:
-
secretKeyGenerationAlgorithm
Algorithm used to transform passwords+salts to secret keys- See Also:
-
SECRET_KEY_ITERATION
public static final int SECRET_KEY_ITERATIONIteration count for generating secret key- See Also:
-
SEED_LENGTH
public static final int SEED_LENGTHDefault length of seeds- See Also:
-
digestAlgorithm
Algorithm use to compute message digests- See Also:
-
-
Method Details
-
getHash
public static byte[] getHash(byte[] passPhrase) Hash a pass phrase. A random seed and the passphrase are hashed. The seed is then prepended to the digest and returned as the hash- Parameters:
passPhrase
- - passphrase to hash- Returns:
- byte array containing the hash
- See Also:
-
validateHash
public static boolean validateHash(byte[] expectedHash, byte[] passPhrase) Validate if a passPhrase matches a hash- Parameters:
expectedHash
- - expected hashpassPhrase
- - passphrase to test- Returns:
- true if match, false otherwise
- See Also:
-
sign
Sign some plainText using a signature key- Parameters:
signatureKey
- - sender's private signature keyplainText
- - PlainText to sign- Returns:
- - bytes containing the signature
- See Also:
-
verify
Verify the signature on some signed plainText- Parameters:
validationKey
- - sender's public validation key corresponding to the signature keyplainText
- - signed plainTextsignature
- - signature received with the text- Returns:
- true - if the signature is successfully verified, false otherwise
- See Also:
-
wrap
Wrap a secret key using a receiver's public encryption key- Parameters:
key
- - key to wrapencryptionKey
- - receiver's public encryption key- Returns:
- - wrapped key
-
unwrap
Unwrap a secret key using the receiver's private decryption key- Parameters:
wrappedKey
- - wrapped keydecryptionKey
- - receiver's private decryption key- Returns:
- - unwrapped secret key
-
encrypt
Encrypt some plaintext using a public encryption key- Parameters:
plainText
- - plaintext to encryptencryptionKey
- - public encryption key- Returns:
- - encrypted text. Null in case of error
- See Also:
-
decrypt
Decrypt some ciphertext using a private decryption key- Parameters:
cipherText
- - ciphertext to decryptdecryptionKey
- - private decryption key- Returns:
- - plain text. Null if error
- See Also:
-
signAndEncrypt
public static byte[] signAndEncrypt(KeyPair senderKeys, byte[] plainText, PublicKey receiverEncryptionKey) Sign and encrypt a message for transmission to a receiver. The method uses the private signature key of the sender to sign the message, and the public encryption key of the receiver to encrypt the message contents.An AES secret key is first generated, and encrypted (wrapped) using the receiver's public encryption key. The Sender's verificationKey, the messageSignature, and the plain text are encrypted using the secret key and the wrapped key and the encrypted text is returned packed as {WLen,WrappedKey,E[{PkLen,PublicKey,SLen,Signature,PtLen,PlainText}]}, where {x}Len is two bytes containing the length of {x}. Currently assumes that RSA keys are used for the asymetric keys
- Parameters:
senderKeys
- - keypair containing the sender's signature keysplainText
- - message to be sent (Should be less than about 60000 bytes).receiverEncryptionKey
- - the public (encryption) key of the receiver- Returns:
- - encrypted message. Null in case of error
- See Also:
-
decryptAndVerify
Decrypt a message, and verify the signature on it. The message is unpacked, the received encryption key is decrypted using the receiver's private decryption key, and the signature on the message is verified using the sender's public key (included as part of the message). Currently assumes RSA key pairs- Parameters:
decryptionKey
- - decryption key to be usedreceivedCipherText
- - received message- Returns:
- - plainText. Null if some error occurs
- See Also:
-
generatePublicKeyPair
Generate a private/public key pair usable by this class- Returns:
- - a key pair containing public/private keys usable by this class
- Throws:
ModelException
- if the Java security policy does not permit keys of the appropriate length to be generated- See Also:
-
generatePublicKeyPair
Generate the private/public keys from encoded key data- Parameters:
privateKeyData
- - private key data (from key.getEncoded())publicKeyData
- - public key data (from key.getEncoded()))- Returns:
- - key pair containing the private/public keys
- See Also:
-
generateSecretKey
Generate a secret key- Returns:
- - a secret key usable by this class
-
generateSecretKey
Generate a password-based secret key. This method takes a salt and a password, and generates an AES key from them- Parameters:
salt
- - salt used in the algorithm (normally 16 bytes)password
- - password to be used to generate the secret key- Returns:
- - key to be used in secret key encryption
- See Also:
-
encrypt
Encrypt some plain text using secret key-based encryption- Parameters:
plainText
- - plainText to be encryptedkey
- - secret key for encryption- Returns:
- - encrypted cipher text
- See Also:
-
decrypt
Decrypt some cipherText that was encrypted using secret key-based encryption- Parameters:
cipherText
- - cipherText to be decryptedkey
- - secret key to be used for decryption- Returns:
- - decrypted plain text
- See Also:
-
encrypt
public static byte[] encrypt(byte[] plainText, char[] password) Encrypt some plain text using a password. An encryption key is generated using the password and a randomly generated salt, and used to encrypt the message. The salt concatenated with the encoded message is returned as the cipherText- Parameters:
plainText
- - plain text to be encryptedpassword
- - password to be used for encryption- Returns:
- - encrypted cipherText
- See Also:
-
decrypt
public static byte[] decrypt(byte[] cipherText, char[] password) Decrypt some cipherText that was encrypted using password-based encryption. The salt is extracted from the message, and the password is used to re-generate the encryption key, which is then used to decrypt the message.- Parameters:
cipherText
- - cipherText to be decryptedpassword
- - password used to encrypt the text- Returns:
- - decrypted plain text
- See Also:
-
encrypt
-
decrypt
-