Skip to content

Commit 732178d

Browse files
committed
updates code (pbkdf2 iterations to 10000, salt size to 32 bytes, exception only log name)
1 parent 4208df0 commit 732178d

7 files changed

Lines changed: 16 additions & 9 deletions

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Changed
11+
- Increased PBKDF2 salt size to 32 bytes (equal to SHA-256 size)
12+
- Increased PBKDF2 iterations to 10000
13+
14+
### Fixed
15+
- Exceptions were logged including the stack trace, now only the localized message is logged
16+
1017
## [0.2.0] - 2018-05-13
1118

1219
### Changed

src/main/java/com/cryptoexamples/java/ExampleAsymmetricStringEncryptionInOneMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void main(String[] args) {
4949

5050
LOGGER.log(Level.INFO, () -> String.format("Decrypted and original plain text are the same: %b", decryptedCipherText.compareTo(plainText) == 0));
5151
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidParameterException e) {
52-
LOGGER.log(Level.SEVERE, e.getMessage(), e);
52+
LOGGER.log(Level.SEVERE, e.getLocalizedMessage());
5353
}
5454
}
5555
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public static void main(String[] args) {
3636
String password = Base64.getEncoder().encodeToString(keyGen.generateKey().getEncoded());
3737

3838
// GENERATE random salt
39-
final byte[] salt = new byte[12];
39+
final byte[] salt = new byte[32];
4040
SecureRandom random = SecureRandom.getInstanceStrong();
4141
random.nextBytes(salt);
4242

4343
// DERIVE key (from password and salt)
4444
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
4545
// Needs unlimited strength policy files http://www.oracle.com/technetwork/java/javase/downloads
46-
KeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
46+
KeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 10000, 256);
4747
SecretKey tmp = factory.generateSecret(keyspec);
4848
SecretKey key = new SecretKeySpec(tmp.getEncoded(), "AES");
4949

@@ -98,7 +98,7 @@ public static void main(String[] args) {
9898
InvalidAlgorithmParameterException |
9999
InvalidKeySpecException |
100100
IOException e) {
101-
LOGGER.log(Level.SEVERE, e.getMessage(), e);
101+
LOGGER.log(Level.SEVERE, e.getLocalizedMessage());
102102
}
103103
}
104104

src/main/java/com/cryptoexamples/java/ExampleHashInOneMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static void main(String[] args) {
3232

3333
LOGGER.log(Level.INFO, hashString);
3434
} catch (NoSuchAlgorithmException e) {
35-
LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
35+
LOGGER.log(Level.SEVERE, e.getLocalizedMessage());
3636
}
3737
}
3838
}

src/main/java/com/cryptoexamples/java/ExampleSignatureInOneMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void main(String[] args) {
4747
boolean isSignatureCorrect = signature.verify(Base64.getDecoder().decode(signatureForPlainTextString));
4848
LOGGER.log(Level.INFO, () -> String.format("Signature is correct: %b", isSignatureCorrect));
4949
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
50-
LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
50+
LOGGER.log(Level.SEVERE, e.getLocalizedMessage());
5151
}
5252
}
5353
}

src/main/java/com/cryptoexamples/java/ExampleStringEncryptionKeyBasedInOneMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void main(String[] args) {
5959

6060
LOGGER.log(Level.INFO, () -> String.format("Decrypted and original plain text are the same: %b", decryptedCipherText.compareTo(plainText) == 0));
6161
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidParameterException | InvalidAlgorithmParameterException e) {
62-
LOGGER.log(Level.SEVERE, e.getMessage(), e);
62+
LOGGER.log(Level.SEVERE, e.getLocalizedMessage());
6363
}
6464
}
6565
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void main(String[] args) {
5050

5151
// DERIVE key (from password and salt)
5252
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
53-
KeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
53+
KeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 10000, 256);
5454
SecretKey tmp = factory.generateSecret(keyspec);
5555
SecretKey key = new SecretKeySpec(tmp.getEncoded(), "AES");
5656

@@ -74,7 +74,7 @@ public static void main(String[] args) {
7474

7575
LOGGER.log(Level.INFO, () -> String.format("Decrypted and original plain text are the same: %b", decryptedCipherText.compareTo(plainText) == 0));
7676
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidParameterException | InvalidAlgorithmParameterException | InvalidKeySpecException e) {
77-
LOGGER.log(Level.SEVERE, e.getMessage(), e);
77+
LOGGER.log(Level.SEVERE, e.getLocalizedMessage());
7878
}
7979
}
8080

0 commit comments

Comments
 (0)