Class PublicPrivateKeyEncryption

java.lang.Object
com.illumon.util.tools.PublicPrivateKeyEncryption

public class PublicPrivateKeyEncryption extends Object
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.
  • Constructor Details

    • PublicPrivateKeyEncryption

      public PublicPrivateKeyEncryption(String keyfile)
      Initialize the key pair based on the provided key filename
      Parameters:
      keyfile - the filename (including path) for the public/private keypair
    • PublicPrivateKeyEncryption

      public PublicPrivateKeyEncryption(String keyfile, PublicPrivateKeyEncryption.Source source)
      Initialize the key pair based on the provided key filename
      Parameters:
      keyfile - the filename (including path) for the public/private keypair
      source - the PublicPrivateKeyEncryption.Source for 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 key
      publicKeyAlgorithm - the algorithm for the public key (e.g., "RSA")
      privateKeyEncoded - the encoded private key
      privateKeyAlgorithm - the algorithm for the private key (e.g., "RSA")
      Throws:
      NoSuchAlgorithmException - if the specified algorithm is not available
      InvalidKeySpecException - if the provided key specifications are invalid
    • PublicPrivateKeyEncryption

      public PublicPrivateKeyEncryption(PublicKey publicKey)
      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

      public String getPrivateKeyAlgorithm()
    • getPublicKeyEncoded

      public byte[] getPublicKeyEncoded()
    • getPublicKeyAlgorithm

      public String getPublicKeyAlgorithm()
    • getPublicKey

      public PublicKey getPublicKey()
      Return the public key that can be used for encryption
      Returns:
      the PublicKey
    • encrypt

      public String encrypt(String stringToEncrypt)
      Encrypt and re-encode as base-64 the supplied String
      Parameters:
      stringToEncrypt - the String to encrypt
      Returns:
      a base-64 encoded, encrypted String
    • decrypt

      public String decrypt(String encryptedString)
      Decrypt the supplied base-64 encoded, encrypted String
      Parameters:
      encryptedString - the base-64 encoded, encrypted String
      Returns:
      the decrypted String