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
The constant 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 & 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 & HMAC keys.
|
static AesCbcWithIntegrity.SecretKeys |
generateKeyFromPassword(java.lang.String password,
java.lang.String salt)
A function that generates password-based AES & 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.InvalidKeyException
- the invalid key exceptionpublic 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, 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.GeneralSecurityException
- the general security exceptionpublic static AesCbcWithIntegrity.SecretKeys generateKeyFromPassword(java.lang.String password, byte[] salt) throws java.security.GeneralSecurityException
password
- The password to derive the keys from.salt
- the saltjava.security.GeneralSecurityException
- if AES is not implemented on this system, or a suitable RNG is not availablepublic static byte[] generateSalt() throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
- the general security exceptionpublic static java.lang.String saltString(byte[] salt)
salt
- the saltpublic 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 & HMAC keys with which to encryptjava.io.UnsupportedEncodingException
- if UTF-8 is not supported in this systemjava.security.GeneralSecurityException
- if AES is not implemented on 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 & HMAC keys with which to encryptencoding
- the encodingjava.io.UnsupportedEncodingException
- if the specified encoding is invalidjava.security.GeneralSecurityException
- if AES is not implemented on this systempublic static AesCbcWithIntegrity.CipherTextIvMac encrypt(byte[] plaintext, AesCbcWithIntegrity.SecretKeys secretKeys) throws java.security.GeneralSecurityException
plaintext
- The text that will be encryptedsecretKeys
- The combined AES & HMAC keys with which to encryptjava.security.GeneralSecurityException
- if AES is not implemented on this systempublic static byte[] generateIv() throws java.security.GeneralSecurityException
java.security.GeneralSecurityException
- if a suitable RNG is not availablepublic static byte[] generateMac(byte[] byteCipherText, javax.crypto.SecretKey integrityKey) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException
byteCipherText
- the cipher textintegrityKey
- The key used for hmacjava.security.NoSuchAlgorithmException
- the no such algorithm exceptionjava.security.InvalidKeyException
- the invalid key exceptionpublic 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 & HMAC keysjava.io.UnsupportedEncodingException
- if UTF-8 is not supportedjava.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 & HMAC keysencoding
- The string encoding to use to decode the bytes after decryptionjava.io.UnsupportedEncodingException
- if the encoding is unsupportedjava.security.GeneralSecurityException
- if AES is not implemented on this systempublic static byte[] decrypt(AesCbcWithIntegrity.CipherTextIvMac civ, AesCbcWithIntegrity.SecretKeys secretKeys) throws java.security.GeneralSecurityException
civ
- the cipher text, iv, and macsecretKeys
- the AES & HMAC keysjava.security.GeneralSecurityException
- if MACs don't match or AES is not implementedpublic static boolean constantTimeEq(byte[] a, byte[] b)
a
- the ab
- the b