Skip to content

Commit 0aa41a8

Browse files
committed
refactors example code (variable names) and base64 usage
1 parent bde196a commit 0aa41a8

12 files changed

Lines changed: 127 additions & 55 deletions

java_landing_page.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ is_landingpage: 1
99

1010
# Available Crypto Scenarios and Use Cases
1111

12-
| | All in One | Easy Object oriented |
13-
|-------------------------------------------------|----------------------------------------|--------------------------------------|
14-
| Symmetric Encryption | [String Encryption (password based) ✔](java_string_encryption_password_based_symmetric.html) <br />[String Encryption (key based) ✔](java_string_encryption_key_based_symmetric.html) <br />[File Encryption ✔](java_file_encryption_symmetric_password_based.html) | [String Encryption with separate class ✔](java_easy_AES_string_encryption.html) <br />[File Encryption with separate class ](java_easy_AES_file_encryption.html) |
15-
| Asymmetric Encryption / Public Key Cryptography | [String Encryption ✔](java_asymmetric_string_encryption.html) | |
16-
| Key Storage | | |
17-
| Hashing | [String Hash ✔](java_string_hash.html) | N/A |
18-
| Crypto Provider Setup | | |
19-
| Digital Signatures | [String Signing ✔](java_string_sign.html) | |
12+
| | All in One |
13+
|-------------------------------------------------|----------------------------------------|
14+
| Symmetric Encryption | [Symmetric String Encryption (password based) ✔](java_string_encryption_password_based_symmetric.html) <br />[Symmetric String Encryption (key based) ✔](java_string_encryption_key_based_symmetric.html) <br />[Symmetric File Encryption ✔](java_file_encryption_symmetric_password_based.html) |
15+
| Asymmetric Encryption / Public Key Cryptography | [Asymmetric String Encryption ✔](java_asymmetric_string_encryption.html) | |
16+
| Key Storage | |
17+
| Hashing | [String Hash ✔](java_string_hash.html) |
18+
| Crypto Provider Setup | |
19+
| Digital Signatures | [String Signing ✔](java_string_sign.html) |

java_string_encryption_asymmetric.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ current_reviews: [
2424

2525
]
2626
# Indicates when this example was last updated/created. Reviews don't change this.
27-
last_updated: "2018-04-28"
27+
last_updated: "2018-05-13"
2828
tags: [Java, RSA, Asymmetric, String, Encryption]
2929
---
3030

java_string_encryption_key_based_symmetric.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ keywords: sample
44
summary: "String encryption in Java with key generation"
55
permalink: java_string_encryption_key_based_symmetric.html
66
folder: Java JDK
7+
references: [
8+
# Place a list of references used to create and/or understand this example.
9+
{
10+
url: "https://docs.oracle.com/javase/10/docs/api/javax/crypto/Cipher.html",
11+
description: "Oracle JDK Cipher Documentation"
12+
}
13+
]
14+
authors: [
15+
{
16+
name: "Kai Mindermann",
17+
url: "https://github.com/kmindi"
18+
}
19+
]
20+
# List all reviewers that reviewed this version of the example. When the example is updated all old reviews
21+
# must be removed from the list below and the code has to be reviewed again. The complete review process
22+
# is documented in the main repository of CryptoExamples
23+
current_reviews: [
24+
25+
]
26+
# Indicates when this example was last updated/created. Reviews don't change this.
27+
last_updated: "2018-05-13"
728
tags: [Java, AES, GCM, Salt, AEAD]
829
---
930

java_string_encryption_password_based_symmetric.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ keywords: sample
44
summary: "Password based string encryption in Java"
55
permalink: java_string_encryption_password_based_symmetric.html
66
folder: Java JDK
7+
references: [
8+
# Place a list of references used to create and/or understand this example.
9+
{
10+
url: "https://docs.oracle.com/javase/10/docs/api/javax/crypto/Cipher.html",
11+
description: "Oracle JDK Cipher Documentation"
12+
}
13+
]
14+
authors: [
15+
{
16+
name: "Kai Mindermann",
17+
url: "https://github.com/kmindi"
18+
}
19+
]
20+
# List all reviewers that reviewed this version of the example. When the example is updated all old reviews
21+
# must be removed from the list below and the code has to be reviewed again. The complete review process
22+
# is documented in the main repository of CryptoExamples
23+
current_reviews: [
24+
25+
]
26+
# Indicates when this example was last updated/created. Reviews don't change this.
27+
last_updated: "2018-05-13"
728
tags: [Java, AES, GCM, PBKDF2, Salt, AEAD]
829
---
930

java_string_hash.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ keywords: sample
44
summary: "Java based string hashing"
55
permalink: java_string_hash.html
66
folder: Java JDK
7+
references: [
8+
# Place a list of references used to create and/or understand this example.
9+
{
10+
url: "https://docs.oracle.com/javase/10/docs/api/java/security/MessageDigest.html",
11+
description: "Oracle JDK MessageDigest Documentation"
12+
}
13+
]
14+
authors: [
15+
{
16+
name: "Kai Mindermann",
17+
url: "https://github.com/kmindi"
18+
}
19+
]
20+
# List all reviewers that reviewed this version of the example. When the example is updated all old reviews
21+
# must be removed from the list below and the code has to be reviewed again. The complete review process
22+
# is documented in the main repository of CryptoExamples
23+
current_reviews: [
24+
25+
]
26+
# Indicates when this example was last updated/created. Reviews don't change this.
27+
last_updated: "2018-05-13"
728
tags: [Java, hash, SHA, SHA-512]
829
---
930

java_string_sign.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ keywords: sample
44
summary: "Java based string signing"
55
permalink: java_string_sign.html
66
folder: Java JDK
7+
references: [
8+
# Place a list of references used to create and/or understand this example.
9+
{
10+
url: "https://docs.oracle.com/javase/10/docs/api/java/security/Signature.html",
11+
description: "Oracle JDK Signature Documentation"
12+
}
13+
]
14+
authors: [
15+
{
16+
name: "Kai Mindermann",
17+
url: "https://github.com/kmindi"
18+
}
19+
]
20+
# List all reviewers that reviewed this version of the example. When the example is updated all old reviews
21+
# must be removed from the list below and the code has to be reviewed again. The complete review process
22+
# is documented in the main repository of CryptoExamples
23+
current_reviews: [
24+
25+
]
26+
# Indicates when this example was last updated/created. Reviews don't change this.
27+
last_updated: "2018-05-13"
728
tags: [Java, hash, SHA, SHA-512]
829
---
930

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* All in one example for asymmetric encryption and decryption of a string in one method.
1818
* - Generation of public and private RSA 4096 bit keypair
1919
* - AES-256 authenticated encryption using GCM
20-
* - BASE64-encoding as representation for the byte-arrays
21-
* - UTF-8 encoding of String
20+
* - BASE64 encoding as representation for the byte-arrays
21+
* - UTF-8 encoding of Strings
2222
* - Exception handling
2323
*/
2424
public class ExampleAsymmetricStringEncryptionInOneMethod {
@@ -27,7 +27,6 @@ public class ExampleAsymmetricStringEncryptionInOneMethod {
2727
public static void main(String[] args) {
2828
String plainText = "Text that is going to be sent over an insecure channel and must be encrypted at all costs!";
2929
try {
30-
3130
// GENERATE NEW KEYPAIR
3231
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
3332
/* @see https://www.keylength.com/ */
@@ -38,15 +37,15 @@ public static void main(String[] args) {
3837
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
3938
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
4039

41-
byte[] byteCipher = cipher.doFinal(plainText.getBytes());
40+
byte[] cipherTextBytes = cipher.doFinal(plainText.getBytes());
4241

4342
// CONVERSION of raw bytes to BASE64 representation
44-
String cipherText = new String(Base64.getEncoder().encode(byteCipher));
43+
String cipherText = Base64.getEncoder().encodeToString(cipherTextBytes);
4544

4645
// DECRYPTION
4746
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
48-
byte[] decryptedCipher = cipher.doFinal(Base64.getDecoder().decode(cipherText));
49-
String decryptedCipherText = new String(decryptedCipher);
47+
byte[] decryptedCipherTextBytes = cipher.doFinal(Base64.getDecoder().decode(cipherText));
48+
String decryptedCipherText = new String(decryptedCipherTextBytes);
5049

5150
LOGGER.log(Level.INFO, () -> String.format("Decrypted and original plain text are the same: %b", decryptedCipherText.compareTo(plainText) == 0));
5251
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidParameterException e) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public static void main(String[] args) {
6363

6464
// SET UP OUTPUT STREAM and write content of String
6565
try (
66-
FileOutputStream fileOutputStream = new FileOutputStream("encryptedFile.enc");
67-
CipherOutputStream encryptedOutputStream = new CipherOutputStream(fileOutputStream, cipher);
68-
InputStream stringInputStream = new ByteArrayInputStream(plainText.getBytes(StandardCharsets.UTF_8));
66+
FileOutputStream fileOutputStream = new FileOutputStream("encryptedFile.enc");
67+
CipherOutputStream encryptedOutputStream = new CipherOutputStream(fileOutputStream, cipher);
68+
InputStream stringInputStream = new ByteArrayInputStream(plainText.getBytes(StandardCharsets.UTF_8))
6969
) {
7070
byte[] buffer = new byte[8192];
7171
while (stringInputStream.read(buffer) > 0) {
@@ -79,7 +79,7 @@ public static void main(String[] args) {
7979
//cipher.updateAAD(aad);
8080
try (
8181
FileInputStream fileInputStream = new FileInputStream("encryptedFile.enc");
82-
CipherInputStream cipherInputStream = new CipherInputStream(fileInputStream, cipher);
82+
CipherInputStream cipherInputStream = new CipherInputStream(fileInputStream, cipher)
8383
) {
8484
byte[] buffer = new byte[8192];
8585
while (cipherInputStream.read(buffer) > 0) {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
import java.util.logging.Logger;
1010

1111
/**
12-
* All in one example for hashing of a string in one method;
13-
* Including
12+
* All in one example for hashing of a string in one method.
1413
* - SHA-512
15-
* - BASE64-encoding as representation for the byte-arrays
14+
* - BASE64 encoding as representation for the byte-arrays
1615
* - UTF-8 encoding of String
1716
* - Exception handling
1817
*/
@@ -22,14 +21,14 @@ public class ExampleHashInOneMethod {
2221
public static void main(String[] args) {
2322
String plainText = "Text that should be authenticated by comparing the hash of it!";
2423
try {
25-
// https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#MessageDigest
24+
// Get MessageDigest Instance
2625
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
2726

2827
// CREATE HASH
29-
byte[] hash = messageDigest.digest(plainText.getBytes(StandardCharsets.UTF_8));
28+
byte[] hashBytes = messageDigest.digest(plainText.getBytes(StandardCharsets.UTF_8));
3029

3130
// CONVERT/ENCODE IN BASE64
32-
String hashString = new String(Base64.getEncoder().encode(hash),StandardCharsets.UTF_8);
31+
String hashString = Base64.getEncoder().encodeToString(hashBytes);
3332

3433
LOGGER.log(Level.INFO, hashString);
3534
} catch (NoSuchAlgorithmException e) {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
import java.util.logging.Logger;
1313

1414
/**
15-
* All in one example for cryptographic signing of a string in one method;
16-
* Including
15+
* All in one example for cryptographic signing of a string in one method.
1716
* - Generation of public and private RSA 4096 bit keypair
1817
* - SHA-512 with RSA
19-
* - BASE64-encoding as representation for the byte-arrays
18+
* - BASE64 encoding as representation for the byte-arrays
2019
* - UTF-8 encoding of String
2120
* - Exception handling
2221
*/
@@ -38,15 +37,14 @@ public static void main(String[] args) {
3837
signature.update(plainText.getBytes(StandardCharsets.UTF_8));
3938

4039
// SIGN DATA/STRING
41-
byte[] signatureForPlainText = signature.sign();
42-
String signatureForPlainTextString = new String(Base64.getEncoder().encode(signatureForPlainText),StandardCharsets.UTF_8);
40+
String signatureForPlainTextString = Base64.getEncoder().encodeToString(signature.sign());
4341
LOGGER.log(Level.INFO, () -> String.format("Signature: %s", signatureForPlainTextString));
4442

4543
// VERIFY JUST CREATED SIGNATURE USING PUBLIC KEY
4644
signature.initVerify(keyPair.getPublic());
4745
signature.update(plainText.getBytes(StandardCharsets.UTF_8));
4846

49-
boolean isSignatureCorrect = signature.verify(signatureForPlainText);
47+
boolean isSignatureCorrect = signature.verify(Base64.getDecoder().decode(signatureForPlainTextString));
5048
LOGGER.log(Level.INFO, () -> String.format("Signature is correct: %b", isSignatureCorrect));
5149
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
5250
LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);

0 commit comments

Comments
 (0)