Skip to content

Commit 37ceacc

Browse files
committed
Refactors main method to call a demonstrate...-method
1 parent 2c2e9d7 commit 37ceacc

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@
3535
public class ExampleStringEncryptionPasswordBasedInOneMethod {
3636
private static final Logger LOGGER = Logger.getLogger(ExampleStringEncryptionPasswordBasedInOneMethod.class.getName());
3737

38-
public static void main(String[] args) {
39-
String plainText = "Text that is going to be sent over an insecure channel and must be encrypted at all costs!";
38+
/**
39+
* Demonstrational method that encrypts the plainText using a password (that is used to derive the required key).
40+
* @param plainText
41+
* @param password
42+
* @return true if encryption and decryption is working, false otherwise
43+
*/
44+
public static boolean demonstratePasswordBasedSymmetricEncryption(String plainText, String password) {
4045
try {
41-
String password = null;
4246
// GENERATE password (not needed if you have a password already)
4347
if(password == null || password.isEmpty()) {
4448
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
@@ -76,9 +80,15 @@ public static void main(String[] args) {
7680
String decryptedCipherText = new String(decryptedCipherTextBytes, StandardCharsets.UTF_8);
7781

7882
LOGGER.log(Level.INFO, () -> String.format("Decrypted and original plain text are the same: %b", decryptedCipherText.compareTo(plainText) == 0));
83+
return decryptedCipherText.compareTo(plainText) == 0;
7984
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidParameterException | InvalidAlgorithmParameterException | InvalidKeySpecException e) {
8085
LOGGER.log(Level.SEVERE, e.getLocalizedMessage());
86+
return false;
8187
}
8288
}
8389

90+
public static void main(String[] args) {
91+
demonstratePasswordBasedSymmetricEncryption("Text that is going to be sent over an insecure channel and must be encrypted at all costs!",null);
92+
}
93+
8494
}

src/test/java/com/cryptoexamples/java/EncryptionInOneMethodTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import org.junit.*;
44

55
import java.io.ByteArrayOutputStream;
6+
import java.io.IOException;
67
import java.io.PrintStream;
78

89
import static org.hamcrest.CoreMatchers.containsString;
910
import static org.junit.Assert.assertThat;
11+
import static org.junit.Assert.assertTrue;
1012

1113

1214
/**
@@ -35,9 +37,12 @@ public void resetOut() {
3537
}
3638

3739
@Test
38-
public void testStringEncryptionPasswordBasedMain() {
40+
public void testStringEncryptionPasswordBasedMain() throws IOException {
3941
ExampleStringEncryptionPasswordBasedInOneMethod.main(new String[1]);
4042
assertThat(errContent.toString(), containsString("Decrypted and original plain text are the same: true"));
43+
errContent.flush();
44+
assertTrue(ExampleStringEncryptionPasswordBasedInOneMethod.demonstratePasswordBasedSymmetricEncryption("plaintext",null));
45+
assertThat(errContent.toString(), containsString("Decrypted and original plain text are the same: true"));
4146
}
4247

4348
@Test

0 commit comments

Comments
 (0)