public class KeyPairUtil extends Object
Refer to the Java Cryptography Extension Reference Guide to determine which parameters are best for key algorithm, key size and cipher; although "RSA", 1024 and "RSA/ECB/PKCS1Padding" should be good enough in most cases.
Constructor and Description |
---|
KeyPairUtil() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
decrypt(PrivateKey privKey,
String cipherParams,
byte[] message)
Decrypt a message using asymmetric keys
|
static byte[] |
encrypt(PublicKey pubKey,
String cipherParams,
byte[] message)
Encrypt a message using asymmetric keys
|
static KeyPair |
generateKeyPair(String algorithm,
int size) |
static void |
generateKeyPair(String algorithm,
int size,
String privPath,
String pubPath)
Generates a pair of public and private keys
|
public static void generateKeyPair(String algorithm, int size, String privPath, String pubPath) throws KeyException
algorithm
- algorithm used for key generation, ie RSAsize
- size of the generated key, must be power of 2 and greater than 512privPath
- path to file to which the generated private key will be savedpubPath
- path to file to which the generated public key will be savedKeyException
- key generation or saving failedpublic static KeyPair generateKeyPair(String algorithm, int size) throws KeyException
KeyException
public static byte[] encrypt(PublicKey pubKey, String cipherParams, byte[] message) throws KeyException
pubKey
- public key used for encryptioncipherParams
- cipher parameters: transformations (ie RSA/ECB/NoPadding)message
- the message to encryptKeyException
- encryption failed, public key recovery failedpublic static byte[] decrypt(PrivateKey privKey, String cipherParams, byte[] message) throws KeyException
privKey
- Private key used for decryptioncipherParams
- cipher parameters: transformations (ie RSA/ECB/NoPadding)message
- the encrypted messageKeyException
- private key recovery failed, decryption failed