public class AesCbcWithIntegrity extends 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.
|
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 String |
decryptString(AesCbcWithIntegrity.CipherTextIvMac civ,
AesCbcWithIntegrity.SecretKeys secretKeys)
AES CBC decrypt.
|
static String |
decryptString(AesCbcWithIntegrity.CipherTextIvMac civ,
AesCbcWithIntegrity.SecretKeys secretKeys,
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(String plaintext,
AesCbcWithIntegrity.SecretKeys secretKeys)
Generates a random IV and encrypts this plain text with the given key.
|
static AesCbcWithIntegrity.CipherTextIvMac |
encrypt(String plaintext,
AesCbcWithIntegrity.SecretKeys secretKeys,
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(String password,
byte[] salt)
A function that generates password-based AES & HMAC keys.
|
static AesCbcWithIntegrity.SecretKeys |
generateKeyFromPassword(String password,
String salt)
A function that generates password-based AES & HMAC keys.
|
static byte[] |
generateMac(byte[] byteCipherText,
SecretKey integrityKey)
Generate the mac based on HMAC_ALGORITHM
|
static byte[] |
generateSalt()
Generates a random salt.
|
static AesCbcWithIntegrity.SecretKeys |
keys(String keysStr)
An aes key derived from a base64 encoded key.
|
static String |
keyString(AesCbcWithIntegrity.SecretKeys keys)
Converts the given AES/HMAC keys into a base64 encoded string suitable for
storage.
|
static String |
saltString(byte[] salt)
Converts the given salt into a base64 encoded string suitable for
storage.
|
static boolean |
validateKey(AesCbcWithIntegrity.SecretKeys key)
return true is the supplied key is a valid aes key
|
public static boolean validateKey(AesCbcWithIntegrity.SecretKeys key)
key
- public static String keyString(AesCbcWithIntegrity.SecretKeys keys)
keys
- The combined aes and hmac keyspublic static AesCbcWithIntegrity.SecretKeys keys(String keysStr) throws InvalidKeyException
keysStr
- a base64 encoded AES key / hmac key as base64(aesKey) : base64(hmacKey).InvalidKeyException
public static AesCbcWithIntegrity.SecretKeys generateKey() throws GeneralSecurityException
GeneralSecurityException
- if AES is not implemented on this system,
or a suitable RNG is not availablepublic static AesCbcWithIntegrity.SecretKeys generateKeyFromPassword(String password, byte[] salt) throws GeneralSecurityException
password
- The password to derive the keys from.GeneralSecurityException
- if AES is not implemented on this system,
or a suitable RNG is not availablepublic static AesCbcWithIntegrity.SecretKeys generateKeyFromPassword(String password, String salt) throws GeneralSecurityException
password
- The password to derive the AES/HMAC keys fromsalt
- A string version of the salt; base64 encoded.GeneralSecurityException
public static byte[] generateSalt() throws GeneralSecurityException
GeneralSecurityException
public static String saltString(byte[] salt)
salt
- public static byte[] generateIv() throws GeneralSecurityException
GeneralSecurityException
- if a suitable RNG is not availablepublic static AesCbcWithIntegrity.CipherTextIvMac encrypt(String plaintext, AesCbcWithIntegrity.SecretKeys secretKeys) throws UnsupportedEncodingException, GeneralSecurityException
plaintext
- The text that will be encrypted, which
will be serialized with UTF-8secretKeys
- The AES & HMAC keys with which to encryptGeneralSecurityException
- if AES is not implemented on this systemUnsupportedEncodingException
- if UTF-8 is not supported in this systempublic static AesCbcWithIntegrity.CipherTextIvMac encrypt(String plaintext, AesCbcWithIntegrity.SecretKeys secretKeys, String encoding) throws UnsupportedEncodingException, GeneralSecurityException
plaintext
- The bytes that will be encryptedsecretKeys
- The AES & HMAC keys with which to encryptGeneralSecurityException
- if AES is not implemented on this systemUnsupportedEncodingException
- if the specified encoding is invalidpublic static AesCbcWithIntegrity.CipherTextIvMac encrypt(byte[] plaintext, AesCbcWithIntegrity.SecretKeys secretKeys) throws GeneralSecurityException
plaintext
- The text that will be encryptedsecretKeys
- The combined AES & HMAC keys with which to encryptGeneralSecurityException
- if AES is not implemented on this systempublic static String decryptString(AesCbcWithIntegrity.CipherTextIvMac civ, AesCbcWithIntegrity.SecretKeys secretKeys, String encoding) throws UnsupportedEncodingException, GeneralSecurityException
civ
- The cipher text, IV, and macsecretKeys
- The AES & HMAC keysencoding
- The string encoding to use to decode the bytes after decryptionGeneralSecurityException
- if AES is not implemented on this systemUnsupportedEncodingException
- if the encoding is unsupportedpublic static String decryptString(AesCbcWithIntegrity.CipherTextIvMac civ, AesCbcWithIntegrity.SecretKeys secretKeys) throws UnsupportedEncodingException, GeneralSecurityException
civ
- The cipher text, IV, and macsecretKeys
- The AES & HMAC keysGeneralSecurityException
- if AES is not implemented on this systemUnsupportedEncodingException
- if UTF-8 is not supportedpublic static byte[] decrypt(AesCbcWithIntegrity.CipherTextIvMac civ, AesCbcWithIntegrity.SecretKeys secretKeys) throws GeneralSecurityException
civ
- the cipher text, iv, and macsecretKeys
- the AES & HMAC keysGeneralSecurityException
- if MACs don't match or AES is not implementedpublic static byte[] generateMac(byte[] byteCipherText, SecretKey integrityKey) throws NoSuchAlgorithmException, InvalidKeyException
integrityKey
- The key used for hmacbyteCipherText
- the cipher textNoSuchAlgorithmException
InvalidKeyException
public static boolean constantTimeEq(byte[] a, byte[] b)
a
- b
- Copyright © 2008–2017 MIL-OSS. All rights reserved.