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​(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

    • 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