Package com.illumon.util.tools
Class PublicPrivateKeyEncryption
java.lang.Object
com.illumon.util.tools.PublicPrivateKeyEncryption
Immutable class to provide simple public/private-key encryption/decryption based on a provided keypair, or encryption based on a provided PublicKey.
Results are provided as a base-64-encoded String. Note that PublicKey instances are serializable and can be provided to a client for encryption.
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionPublicPrivateKeyEncryption(byte[] publicKeyEncoded, String publicKeyAlgorithm, byte[] privateKeyEncoded, String privateKeyAlgorithm) Initialize the key pair based on the provided encoded keys.PublicPrivateKeyEncryption(String keyfile) Initialize the key pair based on the provided key filenamePublicPrivateKeyEncryption(String keyfile, PublicPrivateKeyEncryption.Source source) Initialize the key pair based on the provided key filenamePublicPrivateKeyEncryption(PublicKey publicKey) Initialize the public key based on the provided PublicKey, and ensure decryption will fail -
Method Summary
Modifier and TypeMethodDescriptionDecrypt the supplied base-64 encoded, encrypted StringEncrypt and re-encode as base-64 the supplied Stringbyte[]Return the public key that can be used for encryptionbyte[]
-
Constructor Details
-
PublicPrivateKeyEncryption
Initialize the key pair based on the provided key filename- Parameters:
keyfile- the filename (including path) for the public/private keypair
-
PublicPrivateKeyEncryption
Initialize the key pair based on the provided key filename- Parameters:
keyfile- the filename (including path) for the public/private keypairsource- thePublicPrivateKeyEncryption.Sourcefor the keyfile (classpath or filesystem)
-
PublicPrivateKeyEncryption
public PublicPrivateKeyEncryption(byte[] publicKeyEncoded, String publicKeyAlgorithm, byte[] privateKeyEncoded, String privateKeyAlgorithm) throws NoSuchAlgorithmException, InvalidKeySpecException Initialize the key pair based on the provided encoded keys.- Parameters:
publicKeyEncoded- the encoded public keypublicKeyAlgorithm- the algorithm for the public key (e.g., "RSA")privateKeyEncoded- the encoded private keyprivateKeyAlgorithm- the algorithm for the private key (e.g., "RSA")- Throws:
NoSuchAlgorithmException- if the specified algorithm is not availableInvalidKeySpecException- if the provided key specifications are invalid
-
PublicPrivateKeyEncryption
Initialize the public key based on the provided PublicKey, and ensure decryption will fail- Parameters:
publicKey- the public key to be used for the encryption
-
-
Method Details
-
getPrivateKeyEncoded
public byte[] getPrivateKeyEncoded() -
getPrivateKeyAlgorithm
-
getPublicKeyEncoded
public byte[] getPublicKeyEncoded() -
getPublicKeyAlgorithm
-
getPublicKey
Return the public key that can be used for encryption- Returns:
- the PublicKey
-
encrypt
Encrypt and re-encode as base-64 the supplied String- Parameters:
stringToEncrypt- the String to encrypt- Returns:
- a base-64 encoded, encrypted String
-
decrypt
Decrypt the supplied base-64 encoded, encrypted String- Parameters:
encryptedString- the base-64 encoded, encrypted String- Returns:
- the decrypted String
-