This AES library is very simple and works only on Android.
Java-AES-Crypto is Tozny's simple Android class for encrypting & decrypting strings, aiming to avoid serious cryptographic errors that most such classes suffer from. Show me the code
The original library can be found at https://github.com/tozny/java-aes-crypto.
This library was modified by Dr. Jarmo Kukkola, in order to increase flexibility and security.
- Migrated from Java to Kotlin.
- Made it possible to change PBKDF2 iteration count and algorithm type.
- Algorithm types can be seen in https://developer.android.com/guide/topics/security/cryptography under SecretKeyFactory - PBKDF2...
- Increased default PBKDF2 iteration count from 10000 to 310000.
- Changed from AES-128 to AES-256.
- Implemented double encryption, once with AES-256-CBC and another time with AES-256-GCM. Both times have unique key and IV. The purpose is to reduce attack surfaces.
- Increased minimum android API version to 16.
- Added more robust tests, including measurements of key derivation time with various PBKDF2 iterations counts and algorithms.
Here are the features of this class. We believe that these properties are consistent with what a lot of people are looking for when encrypting Strings in Android.
- Works for strings: It should encrypt arbitrary strings or byte arrays. This means it needs to effectively handle multiple blocks (CBC) and partial blocks (padding). It consistently serializes and deserializes ciphertext, IVs, and key material using base64 to make it easy to store.
- Algorithm & Mode: Double enryption: AES 256, CBC, and PKCS5 padding and then the result is encrypted again with AES 256, GCM, and No padding. Each encryption round has unique key and IV.
- IV Handling: We securely generate a random IV before each encryption and provide a simple class to keep the IV and ciphertext together so they're easy to keep track of and store. We set the IV and then request it back from the Cipher class for compatibility across various Android versions.
- Key generation: Random key generation with the updated generation code recommended for Android. If you want password-based keys, we provide functions to salt and generate them.
- Integrity: Lots of people think AES has integrity checking built in. The thinking goes, "if it decrypts correctly, it was generated by the person with the private key". Actually, AES CBC allows an attacker to modify the messages. Therefore, we've also added integrity checking in the form of a SHA 256 hash.
- Older Phones: It's designed for backward compatibility with older phones, including ciphers that are available for most versions of Android as well as entropy fixes for old Android bugs.
It's a single very simple java class, AesCbcWithIntegrity.java that works across most or all versions of Android. The class should be easy to paste into an existing codebase.
The library is in Android library project format so you can clone this project and add as a library module/project.
We've also published the library AAR file via Jitpack for simple gradle dependency management:
Add the Jitpack repository to your root build.gradle:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency to your project's build.gradle:
dependencies {
implementation 'com.github.JarmoKukkola:java-aes-crypto:*.*.*' // where *.*.* is latest version number.
}
AesCbcWithIntegrity.SecretKeys keys = AesCbcWithIntegrity.generateKey();
EXAMPLE_PASSWORD = // Get password from user input
String salt = saltString(generateSalt());
// You can store the salt, it's not secret. Don't store the key. Derive from password every time
Log.i(TAG, "Salt: " + salt);
key = generateKeyFromPassword(EXAMPLE_PASSWORD, salt);
// alternatively generateKeyFromPassword(EXAMPLE_PASSWORD, salt, iterationCount, pbeAlgorithm);
AesCbcWithIntegrity.CipherTextIvMac cipherTextIvMac = AesCbcWithIntegrity.encrypt("some test", keys);
//store or send to server
String ciphertextString = cipherTextIvMac.toString();
//Use the constructor to re-create the CipherTextIvMac class from the string:
CipherTextIvMac cipherTextIvMac = new CipherTextIvMac (cipherTextString);
String plainText = AesCbcWithIntegrity.decryptString(cipherTextIvMac, keys);
Once you've generated a random key, you naturally might want to store it. This may work for some use cases, but please be aware that if you store the key in the same place that you store the encrypted data, your solution is not cryptographically sound since the attacker can just get both the key and the encrypted text. Instead, you should use either the Keystore infrastructure or consider generating the key from a passphrase and using that to encrypt the user data.
If despite the above you still want to store the key, you can convert the keys to a string using the included functions and store them in preferences or SQLite.
Note that if you hard-code keys or passphrases, or generate them from a static value, you will likely get an error message from the Android security scanner.
The included MIT license is compatible with open source or commercial products.