public class AesCbcWithIntegrity
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static class |
AesCbcWithIntegrity.CipherTextIvMac
Holder class that allows us to bundle ciphertext and IV together.
|
static class |
AesCbcWithIntegrity.PrngFixes
Fixes for the RNG as per
http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html
This software is provided 'as-is', without any express or implied
warranty.
|
static class |
AesCbcWithIntegrity.SecretKeys
Holder class that has both the secret AES key for encryption (confidentiality)
and the secret HMAC key for integrity.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
BASE64_FLAGS |
| Constructor and Description |
|---|
AesCbcWithIntegrity() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
constantTimeEq(byte[] a,
byte[] b)
Simple constant-time equality of two byte arrays.
|
static byte[] |
decrypt(AesCbcWithIntegrity.CipherTextIvMac civ,
AesCbcWithIntegrity.SecretKeys secretKeys)
AES CBC decrypt.
|
static java.lang.String |
decryptString(AesCbcWithIntegrity.CipherTextIvMac civ,
AesCbcWithIntegrity.SecretKeys secretKeys)
AES CBC decrypt.
|
static java.lang.String |
decryptString(AesCbcWithIntegrity.CipherTextIvMac civ,
AesCbcWithIntegrity.SecretKeys secretKeys,
java.lang.String encoding)
AES CBC decrypt.
|
static AesCbcWithIntegrity.CipherTextIvMac |
encrypt(byte[] plaintext,
AesCbcWithIntegrity.SecretKeys secretKeys)
Generates a random IV and encrypts this plain text with the given key.
|
static AesCbcWithIntegrity.CipherTextIvMac |
encrypt(java.lang.String plaintext,
AesCbcWithIntegrity.SecretKeys secretKeys)
Generates a random IV and encrypts this plain text with the given key.
|
static AesCbcWithIntegrity.CipherTextIvMac |
encrypt(java.lang.String plaintext,
AesCbcWithIntegrity.SecretKeys secretKeys,
java.lang.String encoding)
Generates a random IV and encrypts this plain text with the given key.
|
static byte[] |
generateIv()
Creates a random Initialization Vector (IV) of IV_LENGTH_BYTES.
|
static AesCbcWithIntegrity.SecretKeys |
generateKey()
A function that generates random AES and HMAC keys and prints out exceptions but
doesn't throw them since none should be encountered.
|
static AesCbcWithIntegrity.SecretKeys |
generateKeyFromPassword(java.lang.String password,
byte[] salt)
A function that generates password-based AES and HMAC keys.
|
static AesCbcWithIntegrity.SecretKeys |
generateKeyFromPassword(java.lang.String password,
byte[] salt,
int iterationCount)
A function that generates password-based AES and HMAC keys.
|
static AesCbcWithIntegrity.SecretKeys |
generateKeyFromPassword(java.lang.String password,
java.lang.String salt)
A function that generates password-based AES and HMAC keys.
|
static AesCbcWithIntegrity.SecretKeys |
generateKeyFromPassword(java.lang.String password,
java.lang.String salt,
int iterationCount)
A function that generates password-based AES and HMAC keys.
|
static byte[] |
generateMac(byte[] byteCipherText,
javax.crypto.SecretKey integrityKey)
Generate the mac based on HMAC_ALGORITHM
|
static byte[] |
generateSalt()
Generates a random salt.
|
static AesCbcWithIntegrity.SecretKeys |
keys(java.lang.String keysStr)
An aes key derived from a base64 encoded key.
|
static java.lang.String |
keyString(AesCbcWithIntegrity.SecretKeys keys)
Converts the given AES/HMAC keys into a base64 encoded string suitable for
storage.
|
static java.lang.String |
saltString(byte[] salt)
Converts the given salt into a base64 encoded string suitable for
storage.
|
public static final int BASE64_FLAGS
public static java.lang.String keyString(AesCbcWithIntegrity.SecretKeys keys)
keys - The combined aes and hmac keyspublic static AesCbcWithIntegrity.SecretKeys keys(java.lang.String keysStr) throws java.security.InvalidKeyException
keysStr - a base64 encoded AES key / hmac key as base64(aesKey) : base64(hmacKey).java.security.InvalidKeyExceptionpublic static AesCbcWithIntegrity.SecretKeys generateKey() throws java.security.GeneralSecurityException
java.security.GeneralSecurityException - if AES is not implemented on this system,
or a suitable RNG is not availablepublic static AesCbcWithIntegrity.SecretKeys generateKeyFromPassword(java.lang.String password, byte[] salt) throws java.security.GeneralSecurityException
password - The password to derive the keys from.salt - The salt for the keys derived from the password.java.security.GeneralSecurityException - if AES is not implemented on this system,
or a suitable RNG is not availablepublic static AesCbcWithIntegrity.SecretKeys generateKeyFromPassword(java.lang.String password, byte[] salt, int iterationCount) throws java.security.GeneralSecurityException
password - The password to derive the keys from.salt - The salt for the keys derived from the password.iterationCount - The iteration count for the keys generation.java.security.GeneralSecurityException - if AES is not implemented on this system,
or a suitable RNG is not availablepublic static AesCbcWithIntegrity.SecretKeys generateKeyFromPassword(java.lang.String password, java.lang.String salt) throws java.security.GeneralSecurityException
password - The password to derive the AES/HMAC keys fromsalt - A string version of the salt; base64 encoded.java.security.GeneralSecurityExceptionpublic static AesCbcWithIntegrity.SecretKeys generateKeyFromPassword(java.lang.String password, java.lang.String salt, int iterationCount) throws java.security.GeneralSecurityException
password - The password to derive the AES/HMAC keys fromsalt - A string version of the salt; base64 encoded.iterationCount - The iteration count for the key generation.java.security.GeneralSecurityExceptionpublic static byte[] generateSalt()
throws java.security.GeneralSecurityException
java.security.GeneralSecurityExceptionpublic static java.lang.String saltString(byte[] salt)
salt - public static byte[] generateIv()
throws java.security.GeneralSecurityException
java.security.GeneralSecurityException - if a suitable RNG is not availablepublic static AesCbcWithIntegrity.CipherTextIvMac encrypt(java.lang.String plaintext, AesCbcWithIntegrity.SecretKeys secretKeys) throws java.io.UnsupportedEncodingException, java.security.GeneralSecurityException
plaintext - The text that will be encrypted, which
will be serialized with UTF-8secretKeys - The AES and HMAC keys with which to encryptjava.security.GeneralSecurityException - if AES is not implemented on this systemjava.io.UnsupportedEncodingException - if UTF-8 is not supported in this systempublic static AesCbcWithIntegrity.CipherTextIvMac encrypt(java.lang.String plaintext, AesCbcWithIntegrity.SecretKeys secretKeys, java.lang.String encoding) throws java.io.UnsupportedEncodingException, java.security.GeneralSecurityException
plaintext - The bytes that will be encryptedsecretKeys - The AES and HMAC keys with which to encryptjava.security.GeneralSecurityException - if AES is not implemented on this systemjava.io.UnsupportedEncodingException - if the specified encoding is invalidpublic static AesCbcWithIntegrity.CipherTextIvMac encrypt(byte[] plaintext, AesCbcWithIntegrity.SecretKeys secretKeys) throws java.security.GeneralSecurityException
plaintext - The text that will be encryptedsecretKeys - The combined AES and HMAC keys with which to encryptjava.security.GeneralSecurityException - if AES is not implemented on this systempublic static java.lang.String decryptString(AesCbcWithIntegrity.CipherTextIvMac civ, AesCbcWithIntegrity.SecretKeys secretKeys, java.lang.String encoding) throws java.io.UnsupportedEncodingException, java.security.GeneralSecurityException
civ - The cipher text, IV, and macsecretKeys - The AES and HMAC keysencoding - The string encoding to use to decode the bytes after decryptionjava.security.GeneralSecurityException - if AES is not implemented on this systemjava.io.UnsupportedEncodingException - if the encoding is unsupportedpublic static java.lang.String decryptString(AesCbcWithIntegrity.CipherTextIvMac civ, AesCbcWithIntegrity.SecretKeys secretKeys) throws java.io.UnsupportedEncodingException, java.security.GeneralSecurityException
civ - The cipher text, IV, and macsecretKeys - The AES and HMAC keysjava.security.GeneralSecurityException - if AES is not implemented on this systemjava.io.UnsupportedEncodingException - if UTF-8 is not supportedpublic static byte[] decrypt(AesCbcWithIntegrity.CipherTextIvMac civ, AesCbcWithIntegrity.SecretKeys secretKeys) throws java.security.GeneralSecurityException
civ - the cipher text, iv, and macsecretKeys - the AES and HMAC keysjava.security.GeneralSecurityException - if MACs don't match or AES is not implementedpublic static byte[] generateMac(byte[] byteCipherText,
javax.crypto.SecretKey integrityKey)
throws java.security.NoSuchAlgorithmException,
java.security.InvalidKeyException
integrityKey - The key used for hmacbyteCipherText - the cipher textjava.security.NoSuchAlgorithmExceptionjava.security.InvalidKeyExceptionpublic static boolean constantTimeEq(byte[] a,
byte[] b)