- java.lang.Object
-
- io.helidon.common.crypto.SymmetricCipher
-
- All Implemented Interfaces:
CommonCipher
public class SymmetricCipher extends Object implements CommonCipher
This class provides simple and stateless way to encrypt and decrypt messages using selected symmetric cipher.
It requires to have base password provided. Unique cryptography key is generated by this implementation every time it encrypts and decrypts. The key uniqueness is ensured by randomly generated salt which is added to the password.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SymmetricCipher.Builder
Builder of theSymmetricCipher
.
-
Field Summary
Fields Modifier and Type Field Description static String
ALGORITHM_AES_CBC
AES algorithm with CBC method and PKCS5 padding.static String
ALGORITHM_AES_CTR
AES algorithm with CTR method and no padding.static String
ALGORITHM_AES_GCM
AES algorithm with GCM method and no padding.static String
ALGORITHM_CHA_CHA
ChaCha20 encryption algorithm.static String
ALGORITHM_CHA_CHA_POLY1305
ChaCha20 encryption algorithm with Poly1305 authentication code.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SymmetricCipher.Builder
builder()
Create a new builder.static SymmetricCipher
create(char[] password)
Create a new instance based on the password.Base64Value
decrypt(Base64Value encrypted)
Decrypt encrypted message.static Base64Value
decrypt(String algorithm, byte[] key, byte[] iv, Base64Value encrypted)
Decrypt the message with the usage of provided parameters.static Base64Value
decrypt(String algorithm, String provider, byte[] key, byte[] iv, Base64Value encrypted)
Decrypt the message with the usage of provided parameters.static Base64Value
decrypt(String algorithm, String provider, byte[] key, AlgorithmParameterSpec params, Base64Value encrypted)
Decrypt the message with the usage of provided parameters.Base64Value
encrypt(Base64Value message)
Encrypt message.static Base64Value
encrypt(String algorithm, byte[] key, byte[] iv, Base64Value plain)
Encrypt the message with the usage of provided parameters.static Base64Value
encrypt(String algorithm, String provider, byte[] key, byte[] iv, Base64Value plain)
Encrypt the message with the usage of provided parameters.static Base64Value
encrypt(String algorithm, String provider, byte[] key, AlgorithmParameterSpec params, Base64Value plain)
Encrypt the message with the usage of provided parameters.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.helidon.common.crypto.CommonCipher
decryptFromString, encryptToString
-
-
-
-
Field Detail
-
ALGORITHM_AES_CBC
public static final String ALGORITHM_AES_CBC
AES algorithm with CBC method and PKCS5 padding.
It is strongly advised to be used together with HMAC or other authenticated message digest.
Value is: "AES/CBC/PKCS5Padding".- See Also:
- Constant Field Values
-
ALGORITHM_AES_CTR
public static final String ALGORITHM_AES_CTR
AES algorithm with CTR method and no padding.
It is strongly advised to be used together with HMAC or other authenticated message digest.
Value is: "AES/CTR/NoPadding".- See Also:
- Constant Field Values
-
ALGORITHM_AES_GCM
public static final String ALGORITHM_AES_GCM
AES algorithm with GCM method and no padding.
Does not need to be used with any authenticated message digest since GCM is making its signature by the design.
Value is: "AES/GCM/NoPadding".- See Also:
- Constant Field Values
-
ALGORITHM_CHA_CHA
public static final String ALGORITHM_CHA_CHA
ChaCha20 encryption algorithm.
Value is: "ChaCha20".- See Also:
- Constant Field Values
-
ALGORITHM_CHA_CHA_POLY1305
public static final String ALGORITHM_CHA_CHA_POLY1305
ChaCha20 encryption algorithm with Poly1305 authentication code.
Value is: "ChaCha20-Poly1305".- See Also:
- Constant Field Values
-
-
Method Detail
-
builder
public static SymmetricCipher.Builder builder()
Create a new builder.- Returns:
- new builder
-
create
public static SymmetricCipher create(char[] password)
Create a new instance based on the password. Default used algorithm isALGORITHM_AES_GCM
.- Parameters:
password
- password- Returns:
- new instance
-
encrypt
public static Base64Value encrypt(String algorithm, byte[] key, byte[] iv, Base64Value plain)
Encrypt the message with the usage of provided parameters.- Parameters:
algorithm
- algorithm to be usedkey
- key to encrypt messageiv
- initialization vectorplain
- message- Returns:
- encrypted message
-
encrypt
public static Base64Value encrypt(String algorithm, String provider, byte[] key, byte[] iv, Base64Value plain)
Encrypt the message with the usage of provided parameters.- Parameters:
algorithm
- algorithm to be usedprovider
- provider of the algorithmkey
- key to encrypt messageiv
- initialization vectorplain
- message- Returns:
- encrypted message
-
encrypt
public static Base64Value encrypt(String algorithm, String provider, byte[] key, AlgorithmParameterSpec params, Base64Value plain)
Encrypt the message with the usage of provided parameters.- Parameters:
algorithm
- algorithm to be usedprovider
- provider of the algorithmkey
- key to encrypt messageparams
- cipher parameter objectplain
- message- Returns:
- encrypted message
-
decrypt
public static Base64Value decrypt(String algorithm, byte[] key, byte[] iv, Base64Value encrypted)
Decrypt the message with the usage of provided parameters.- Parameters:
algorithm
- algorithm to be usedkey
- key to decrypt messageiv
- encrypted message initialization vectorencrypted
- encrypted message- Returns:
- decrypted message
-
decrypt
public static Base64Value decrypt(String algorithm, String provider, byte[] key, byte[] iv, Base64Value encrypted)
Decrypt the message with the usage of provided parameters.- Parameters:
algorithm
- algorithm to be usedprovider
- algorithm providerkey
- key to decrypt messageiv
- encrypted message initialization vectorencrypted
- encrypted message- Returns:
- decrypted message
-
decrypt
public static Base64Value decrypt(String algorithm, String provider, byte[] key, AlgorithmParameterSpec params, Base64Value encrypted)
Decrypt the message with the usage of provided parameters.- Parameters:
algorithm
- algorithm to be usedprovider
- algorithm providerkey
- key to decrypt messageparams
- cipher parameter objectencrypted
- encrypted message- Returns:
- decrypted message
-
encrypt
public Base64Value encrypt(Base64Value message)
Description copied from interface:CommonCipher
Encrypt message.- Specified by:
encrypt
in interfaceCommonCipher
- Parameters:
message
- message- Returns:
- encrypted message
-
decrypt
public Base64Value decrypt(Base64Value encrypted)
Description copied from interface:CommonCipher
Decrypt encrypted message.- Specified by:
decrypt
in interfaceCommonCipher
- Parameters:
encrypted
- encrypted message- Returns:
- decrypted message
-
-