public class CryptoUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CIPHER_CHARSET |
static java.lang.String |
CIPHER_NAME |
protected static java.lang.String |
DEFAULT_PASSWORD |
protected static javax.crypto.spec.SecretKeySpec |
keySpec |
protected static java.security.MessageDigest |
md5 |
protected static java.security.SecureRandom |
random |
protected static java.security.MessageDigest |
sha |
Constructor and Description |
---|
CryptoUtils() |
Modifier and Type | Method and Description |
---|---|
static javax.crypto.spec.SecretKeySpec |
createSecret(byte[] password,
int length)
Create secret from password.
|
static byte[] |
decryptFromAes128EcbWithBase64(java.lang.String source)
Decode with Base64 and Aes128Ecb.
|
static byte[] |
decryptFromAes128EcbWithBase64(java.lang.String source,
javax.crypto.spec.SecretKeySpec secretKeySpec)
Decode with Base64 and Aes128Ecb.
|
static byte[] |
decryptFromAes128EcbWithBase64(java.lang.String source,
java.lang.String password)
Decode with Base64 and Aes128Ecb.
|
static byte[] |
decryptFromAesGcm(byte[] source,
byte[] password,
byte[] nonce)
Decrypt to aes with gcm.
|
static java.lang.String |
encryptToAes128EcbWithBase64(byte[] message,
javax.crypto.spec.SecretKeySpec secretKeySpec)
Encoding with Base64 and Aes128Ecb.
|
static java.lang.String |
encryptToAes128EcbWithBase64(java.lang.String source)
Encoding with Base64 and Aes128Ecb.
|
static java.lang.String |
encryptToAes128EcbWithBase64(java.lang.String source,
java.lang.String password)
Encoding with Base64 and Aes128Ecb.
|
static byte[] |
encryptToAesGcm(byte[] message,
byte[] password,
byte[] nonce)
Encrypt to aes with gcm.
|
public static final java.lang.String CIPHER_CHARSET
public static final java.lang.String CIPHER_NAME
protected static final java.lang.String DEFAULT_PASSWORD
protected static java.security.SecureRandom random
protected static java.security.MessageDigest sha
protected static java.security.MessageDigest md5
protected static javax.crypto.spec.SecretKeySpec keySpec
public static javax.crypto.spec.SecretKeySpec createSecret(byte[] password, int length)
password
- string to create secret fromlength
- secret lengthpublic static java.lang.String encryptToAes128EcbWithBase64(java.lang.String source) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
source
- string to encodejava.security.NoSuchAlgorithmException
- if no algorithmjavax.crypto.NoSuchPaddingException
- if no padding existjava.security.InvalidKeyException
- if invalid key usedjavax.crypto.IllegalBlockSizeException
- if wrong block sizejavax.crypto.BadPaddingException
- if padding violate rulepublic static java.lang.String encryptToAes128EcbWithBase64(java.lang.String source, java.lang.String password) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
source
- string to encodepassword
- password to encode withjava.security.NoSuchAlgorithmException
- if no algorithmjavax.crypto.NoSuchPaddingException
- if no padding existjava.security.InvalidKeyException
- if invalid key usedjavax.crypto.IllegalBlockSizeException
- if wrong block sizejavax.crypto.BadPaddingException
- if padding violate rulepublic static java.lang.String encryptToAes128EcbWithBase64(byte[] message, javax.crypto.spec.SecretKeySpec secretKeySpec) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
message
- byte array to encodesecretKeySpec
- secret key informationjava.security.NoSuchAlgorithmException
- if no algorithmjavax.crypto.NoSuchPaddingException
- if no padding existjava.security.InvalidKeyException
- if invalid key usedjavax.crypto.IllegalBlockSizeException
- if wrong block sizejavax.crypto.BadPaddingException
- if padding violate rulepublic static byte[] encryptToAesGcm(byte[] message, byte[] password, byte[] nonce) throws java.lang.IllegalStateException, org.bouncycastle.crypto.InvalidCipherTextException
message
- message to encryptpassword
- encrypt keynonce
- an encrypt noncejava.lang.IllegalStateException
- if the cipher is in an inappropriate stateorg.bouncycastle.crypto.InvalidCipherTextException
- if the MAC fails to matchpublic static byte[] decryptFromAes128EcbWithBase64(java.lang.String source) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
source
- string to decodejava.security.NoSuchAlgorithmException
- if no algorithmjavax.crypto.NoSuchPaddingException
- if no padding existjava.security.InvalidKeyException
- if invalid key usedjavax.crypto.IllegalBlockSizeException
- if wrong block sizejavax.crypto.BadPaddingException
- if padding violate rulepublic static byte[] decryptFromAes128EcbWithBase64(java.lang.String source, java.lang.String password) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
source
- string to decodepassword
- spring to decode withjava.security.NoSuchAlgorithmException
- if no algorithmjavax.crypto.NoSuchPaddingException
- if no padding existjava.security.InvalidKeyException
- if invalid key usedjavax.crypto.IllegalBlockSizeException
- if wrong block sizejavax.crypto.BadPaddingException
- if padding violate rulepublic static byte[] decryptFromAes128EcbWithBase64(java.lang.String source, javax.crypto.spec.SecretKeySpec secretKeySpec) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException
source
- string to decodesecretKeySpec
- secret to decode withjava.security.NoSuchAlgorithmException
- if no algorithmjavax.crypto.NoSuchPaddingException
- if no padding existjava.security.InvalidKeyException
- if invalid key usedjavax.crypto.IllegalBlockSizeException
- if wrong block sizejavax.crypto.BadPaddingException
- if padding violate rulepublic static byte[] decryptFromAesGcm(byte[] source, byte[] password, byte[] nonce) throws java.lang.IllegalStateException, org.bouncycastle.crypto.InvalidCipherTextException
source
- source to decryptpassword
- encrypt keynonce
- an encrypt noncejava.lang.IllegalStateException
- if the cipher is in an inappropriate stateorg.bouncycastle.crypto.InvalidCipherTextException
- if the MAC fails to match