Skip to content

Commit 2c2e9d7

Browse files
committed
refactors variable names and fixes comments and makes password generation conditional
1 parent 8f6c193 commit 2c2e9d7

3 files changed

Lines changed: 24 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
- Changed PBKDF2 hash function to SHA-512
1212
- Increased PBKDF2 salt size to 64 bytes (equal to SHA-512 size)
1313
- Increased PBKDF2 iterations to 10000
14+
- Refactored variable names
1415

1516
### Fixed
1617
- Exceptions were logged including the stack trace, now only the localized message is logged

src/main/java/com/cryptoexamples/java/ExampleFileEncryptionInOneMethod.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Including
1919
* - Random password generation using strong secure random number generator
2020
* - Random salt generation
21-
* - Key derivation using PBKDF2 HMAC SHA-256,
21+
* - Key derivation using PBKDF2 HMAC SHA-512,
2222
* - AES-256 authenticated encryption using GCM
2323
* - BASE64-encoding as representation for the byte-arrays
2424
* - Exception handling
@@ -29,23 +29,24 @@ public class ExampleFileEncryptionInOneMethod {
2929
public static void main(String[] args) {
3030
String plainText = "Multiline text:";
3131
try {
32-
// GENERATE password
33-
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
34-
// Needs unlimited strength policy files http://www.oracle.com/technetwork/java/javase/downloads
35-
keyGen.init(256);
36-
String password = Base64.getEncoder().encodeToString(keyGen.generateKey().getEncoded());
32+
String password = null;
33+
// GENERATE password (not needed if you have a password already)
34+
if(password == null || password.isEmpty()) {
35+
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
36+
keyGen.init(256);
37+
password = Base64.getEncoder().encodeToString(keyGen.generateKey().getEncoded());
38+
}
3739

3840
// GENERATE random salt
3941
final byte[] salt = new byte[64];
4042
SecureRandom random = SecureRandom.getInstanceStrong();
4143
random.nextBytes(salt);
4244

4345
// DERIVE key (from password and salt)
44-
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
45-
// Needs unlimited strength policy files http://www.oracle.com/technetwork/java/javase/downloads
46-
KeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 10000, 256);
47-
SecretKey tmp = factory.generateSecret(keyspec);
48-
SecretKey key = new SecretKeySpec(tmp.getEncoded(), "AES");
46+
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
47+
KeySpec passwordBasedEncryptionKeySpec = new PBEKeySpec(password.toCharArray(), salt, 10000, 256);
48+
SecretKey secretKeyFromPBKDF2 = secretKeyFactory.generateSecret(passwordBasedEncryptionKeySpec);
49+
SecretKey key = new SecretKeySpec(secretKeyFromPBKDF2.getEncoded(), "AES");
4950

5051
// GENERATE random nonce (number used once)
5152
final byte[] nonce = new byte[32];

src/main/java/com/cryptoexamples/java/ExampleStringEncryptionPasswordBasedInOneMethod.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* All in one example for encryption and decryption of a string in one method.
2727
* - Random password generation using strong secure random number generator
2828
* - Random salt generation
29-
* - Key derivation using PBKDF2 HMAC SHA-256,
29+
* - Key derivation using PBKDF2 HMAC SHA-512,
3030
* - AES-256 authenticated encryption using GCM
3131
* - BASE64 encoding as representation for the byte-arrays
3232
* - UTF-8 encoding of Strings
@@ -38,21 +38,24 @@ public class ExampleStringEncryptionPasswordBasedInOneMethod {
3838
public static void main(String[] args) {
3939
String plainText = "Text that is going to be sent over an insecure channel and must be encrypted at all costs!";
4040
try {
41+
String password = null;
4142
// GENERATE password (not needed if you have a password already)
42-
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
43-
keyGen.init(256);
44-
String password = Base64.getEncoder().encodeToString(keyGen.generateKey().getEncoded());
43+
if(password == null || password.isEmpty()) {
44+
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
45+
keyGen.init(256);
46+
password = Base64.getEncoder().encodeToString(keyGen.generateKey().getEncoded());
47+
}
4548

4649
// GENERATE random salt (needed for PBKDF2)
4750
final byte[] salt = new byte[64];
4851
SecureRandom random = SecureRandom.getInstanceStrong();
4952
random.nextBytes(salt);
5053

5154
// DERIVE key (from password and salt)
52-
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
53-
KeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 10000, 256);
54-
SecretKey tmp = factory.generateSecret(keyspec);
55-
SecretKey key = new SecretKeySpec(tmp.getEncoded(), "AES");
55+
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
56+
KeySpec passwordBasedEncryptionKeySpec = new PBEKeySpec(password.toCharArray(), salt, 10000, 256);
57+
SecretKey secretKeyFromPBKDF2 = secretKeyFactory.generateSecret(passwordBasedEncryptionKeySpec);
58+
SecretKey key = new SecretKeySpec(secretKeyFromPBKDF2.getEncoded(), "AES");
5659

5760
// GENERATE random nonce (number used once)
5861
final byte[] nonce = new byte[32];

0 commit comments

Comments
 (0)