Package io.mangoo.crypto
Class Crypto
- java.lang.Object
-
- io.mangoo.crypto.Crypto
-
public class Crypto extends Object
Convenient class for encryption and decryption- Author:
- svenkubiak
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]decrypt(byte[] text, PrivateKey key)Decrypt text using private keyStringdecrypt(String encrytedText)Decrypts an given encrypted text using the application secret property (application.secret) as keyStringdecrypt(String encrytedText, String key)Decrypts an given encrypted text using the given keyStringdecrypt(String text, PrivateKey key)Decrypt Base64 encoded text using private keybyte[]encrypt(byte[] text, PublicKey key)Encrypt a text using public keyStringencrypt(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 bitStringencrypt(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 bitStringencrypt(String text, PublicKey key)Encrypt a text using public key.KeyPairgenerateKeyPair()Generate key which contains a pair of private and public key using 4096 bytesStringgetKeyAsString(Key key)Convert a Key to string encoded as Base64PrivateKeygetPrivateKeyFromString(String key)Generates Private Key from Base64 encoded stringPublicKeygetPublicKeyFromString(String key)Generates Public Key from Base64 encoded stringStringgetSizedSecret(String secret)
-
-
-
Constructor Detail
-
Crypto
@Inject public Crypto(Config config)
-
-
Method Detail
-
decrypt
public String decrypt(String encrytedText)
Decrypts an 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 an given encrypted text using the given key- Parameters:
encrytedText- The encrypted textkey- 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 encryptkey- The key to use for encryption- Returns:
- The encrypted text or null if encryption fails
-
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 MangooEncryptionExceptionEncrypt a text using public key- Parameters:
text- The plain textkey- 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 textkey- The public key- Returns:
- Encrypted string as base64
- Throws:
MangooEncryptionException- if encryption fails
-
decrypt
public byte[] decrypt(byte[] text, PrivateKey key) throws MangooEncryptionExceptionDecrypt text using private key- Parameters:
text- The encrypted textkey- 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 Base64key- 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
-
-