Class Crypto

java.lang.Object
io.mangoo.crypto.Crypto

public class Crypto extends Object
Convenient class for encryption and decryption
Author:
svenkubiak
  • Constructor Details

    • Crypto

      @Inject public Crypto(Config config)
  • Method Details

    • decrypt

      public String decrypt(String encrytedText)
      Decrypts a given encrypted text using the application secret property (application.secret) as key
      Parameters:
      encrytedText - The encrypted text
      Returns:
      The clear text or null if decryption fails
    • decrypt

      public String decrypt(String encrytedText, String key)
      Decrypts a given encrypted text using the given key
      Parameters:
      encrytedText - The encrypted text
      key - The encryption key
      Returns:
      The clear text or null if decryption fails
    • encrypt

      public String encrypt(String plainText)
      Encrypts a given plain text using the application secret property (application.secret) as key Encryption is done by using AES and CBC Cipher and a key length of 256 bit
      Parameters:
      plainText - The plain text to encrypt
      Returns:
      The encrypted text or null if encryption fails
    • encrypt

      public String encrypt(String plainText, String key)
      Encrypts a given plain text using the given key Encryption is done by using AES and CBC Cipher and a key length of 256 bit
      Parameters:
      plainText - The plain text to encrypt
      key - The key to use for encryption
      Returns:
      The encrypted text or null if encryption fails
    • getSizedSecret

      public String getSizedSecret(String secret)
    • generateKeyPair

      public KeyPair generateKeyPair()
      Generate key which contains a pair of private and public key using 4096 bytes
      Returns:
      key pair
    • encrypt

      public byte[] encrypt(byte[] text, PublicKey key) throws MangooEncryptionException
      Encrypt a text using public key
      Parameters:
      text - The plain text
      key - The public key
      Returns:
      Encrypted text
      Throws:
      MangooEncryptionException - if encryption fails
    • encrypt

      public String encrypt(String text, PublicKey key) throws MangooEncryptionException
      Encrypt a text using public key. The result is encoded to Base64.
      Parameters:
      text - The plain text
      key - The public key
      Returns:
      Encrypted string as base64
      Throws:
      MangooEncryptionException - if encryption fails
    • decrypt

      public byte[] decrypt(byte[] text, PrivateKey key) throws MangooEncryptionException
      Decrypt text using private key
      Parameters:
      text - The encrypted text
      key - The private key
      Returns:
      The unencrypted text
      Throws:
      MangooEncryptionException - if decryption fails
    • decrypt

      public String decrypt(String text, PrivateKey key) throws MangooEncryptionException
      Decrypt Base64 encoded text using private key
      Parameters:
      text - The encrypted text, encoded as Base64
      key - The private key
      Returns:
      The plain text encoded as UTF8
      Throws:
      MangooEncryptionException - if decryption fails
    • getKeyAsString

      public String getKeyAsString(Key key)
      Convert a Key to string encoded as Base64
      Parameters:
      key - The key (private or public)
      Returns:
      A string representation of the key
    • getPrivateKeyFromString

      public PrivateKey getPrivateKeyFromString(String key) throws MangooEncryptionException
      Generates Private Key from Base64 encoded string
      Parameters:
      key - Base64 encoded string which represents the key
      Returns:
      The PrivateKey
      Throws:
      MangooEncryptionException - if getting private key from string fails
    • getPublicKeyFromString

      public PublicKey getPublicKeyFromString(String key) throws MangooEncryptionException
      Generates Public Key from Base64 encoded string
      Parameters:
      key - Base64 encoded string which represents the key
      Returns:
      The PublicKey
      Throws:
      MangooEncryptionException - if getting public key from string fails