public class KeyUtil extends Object
Refer to the Java Cryptography Extension Reference Guide at to determine which parameters are best for key algorithm, key size and cipher; although "AES", 128 and "AES" should be good enough in most cases.
Constructor and Description |
---|
KeyUtil() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
decrypt(SecretKey key,
String cipherParams,
byte[] message)
Decrypt a message using a symmetric key
|
static byte[] |
encrypt(SecretKey key,
String cipherParams,
byte[] message)
Encrypt a message using a symmetric key
|
static SecretKey |
generateKey(String algorithm,
int size)
Generates a secret symmetric key
|
static SecureRandom |
getSecureRandom() |
public static SecureRandom getSecureRandom()
public static SecretKey generateKey(String algorithm, int size) throws KeyException
algorithm
- algorithm used for key generation, ie AESsize
- size of the generated key, must be one of 128, 192, 256. Use 128 when unsure,
default configurations and providers should refuse to use longer keys.KeyException
- key generation or saving failedpublic static byte[] encrypt(SecretKey key, String cipherParams, byte[] message) throws KeyException
key
- secret key used for encryptioncipherParams
- cipher parameters: transformations, ie AESmessage
- the message to encryptKeyException
- encryption failed, public key recovery failedpublic static byte[] decrypt(SecretKey key, String cipherParams, byte[] message) throws KeyException
key
- secret key used for decryptioncipherParams
- cipher parameters: transformations, ie AESmessage
- the encrypted messageKeyException
- private key recovery failed, decryption failed