Skip to content

Instantly share code, notes, and snippets.

@beached
Last active June 28, 2017 01:40
Show Gist options
  • Save beached/5b623425ce2426bec97f50c2071bdedf to your computer and use it in GitHub Desktop.
Save beached/5b623425ce2426bec97f50c2071bdedf to your computer and use it in GitHub Desktop.
char[] message = "Secret message!".toCharArray();
char[] userPassword = input("Enter your password: ");
//16 bytes twice PBKDF2 minimum salt recommendation
byte[] salt = SecureRandom.getBytes(16);
//Why did I use SHA512? see note at the bottom
//produce 64 bytes of secretmaterial from user password
byte[] secretMaterial = PBKDF2(userPassword, salt, 5000, "SHA512", 64);
// 32 bytes
byte[] encKey = secretMaterial.splice(0, 32);
//32 bytes
byte[] authKey = secretMaterial.splice(32, 64);
byte[] iv = SecureRandom.getBytes(16);
//AES256_CBC (has no integrity built in so we need to add MAC)
byte[] cipherText = AES(message, iv, encKey, "CBC");
//array concatenation. "salt" and "iv" are always fixed length (16 and 16)
byte[] cipherPayload = salt + iv + cipherText;
//always 32 bytes
byte[] mac = HMAC_256(cipherPayload, authKey);
cipherPayload = mac + cipherPayload;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment