Skip to content

Commit cd37def

Browse files
committed
turned cli applications into JUnit tests
1 parent 297113a commit cd37def

7 files changed

Lines changed: 234 additions & 211 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package de.dominikschadow.javasecurity;
2+
3+
import de.dominikschadow.javasecurity.asymmetric.DSA;
4+
5+
import javax.crypto.spec.SecretKeySpec;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.security.*;
9+
import java.security.cert.CertificateException;
10+
11+
public class Keystore {
12+
private static final String KEYSTORE_PATH = "/samples.ks";
13+
14+
public static KeyStore loadKeystore(char[] keystorePassword) throws KeyStoreException,
15+
CertificateException, NoSuchAlgorithmException, IOException {
16+
try (InputStream keystoreStream = DSA.class.getResourceAsStream(KEYSTORE_PATH)) {
17+
KeyStore ks = KeyStore.getInstance("JCEKS");
18+
ks.load(keystoreStream, keystorePassword);
19+
return ks;
20+
}
21+
}
22+
23+
public static PrivateKey loadPrivateKey(KeyStore ks, String keyAlias, char[] keyPassword) throws KeyStoreException,
24+
UnrecoverableKeyException, NoSuchAlgorithmException {
25+
if (!ks.containsAlias(keyAlias)) {
26+
throw new UnrecoverableKeyException("Private key " + keyAlias + " not found in keystore");
27+
}
28+
29+
return (PrivateKey) ks.getKey(keyAlias, keyPassword);
30+
}
31+
32+
public static PublicKey loadPublicKey(KeyStore ks, String keyAlias) throws KeyStoreException, UnrecoverableKeyException {
33+
if (!ks.containsAlias(keyAlias)) {
34+
throw new UnrecoverableKeyException("Public key " + keyAlias + " not found in keystore");
35+
}
36+
37+
return ks.getCertificate(keyAlias).getPublicKey();
38+
}
39+
40+
public static Key loadKey(KeyStore ks, String keyAlias, char[] keyPassword) throws KeyStoreException,
41+
UnrecoverableKeyException, NoSuchAlgorithmException {
42+
if (!ks.containsAlias(keyAlias)) {
43+
throw new UnrecoverableKeyException("Secret key " + keyAlias + " not found in keystore");
44+
}
45+
46+
return ks.getKey(keyAlias, keyPassword);
47+
}
48+
49+
public static SecretKeySpec createSecretKeySpec(byte[] key, String algorithm) {
50+
return new SecretKeySpec(key, algorithm);
51+
}
52+
}
Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 Dominik Schadow, dominikschadow@gmail.com
2+
* Copyright (C) 2022 Dominik Schadow, dominikschadow@gmail.com
33
*
44
* This file is part of the Java Security project.
55
*
@@ -17,13 +17,8 @@
1717
*/
1818
package de.dominikschadow.javasecurity.asymmetric;
1919

20-
import com.google.common.io.BaseEncoding;
21-
22-
import java.io.IOException;
23-
import java.io.InputStream;
2420
import java.nio.charset.StandardCharsets;
2521
import java.security.*;
26-
import java.security.cert.CertificateException;
2722

2823
/**
2924
* Digital signature sample with plain Java. Loads the DSA key from the sample keystore, signs and verifies sample text
@@ -34,86 +29,21 @@
3429
* @author Dominik Schadow
3530
*/
3631
public class DSA {
37-
private static final System.Logger LOG = System.getLogger(DSA.class.getName());
3832
private static final String ALGORITHM = "SHA1withDSA";
39-
private static final String KEYSTORE_PATH = "/samples.ks";
40-
41-
/**
42-
* Private constructor.
43-
*/
44-
private DSA() {
45-
}
46-
47-
public static void main(String[] args) {
48-
sign();
49-
}
50-
51-
private static void sign() {
52-
final String initialText = "DSA signature sample text";
53-
final char[] keystorePassword = "samples".toCharArray();
54-
final String keyAlias = "asymmetric-sample-dsa";
55-
final char[] keyPassword = "asymmetric-sample-dsa".toCharArray();
56-
57-
try {
58-
KeyStore ks = loadKeystore(keystorePassword);
59-
PrivateKey privateKey = loadPrivateKey(ks, keyAlias, keyPassword);
60-
PublicKey publicKey = loadPublicKey(ks, keyAlias);
61-
62-
byte[] signature = sign(privateKey, initialText);
63-
boolean valid = verify(publicKey, signature, initialText);
64-
65-
printReadableMessages(initialText, signature, valid);
66-
} catch (NoSuchAlgorithmException | SignatureException | KeyStoreException | CertificateException |
67-
UnrecoverableKeyException | InvalidKeyException | IOException ex) {
68-
LOG.log(System.Logger.Level.ERROR, ex.getMessage(), ex);
69-
}
70-
}
7133

72-
private static KeyStore loadKeystore(char[] keystorePassword) throws KeyStoreException,
73-
CertificateException, NoSuchAlgorithmException, IOException {
74-
try (InputStream keystoreStream = DSA.class.getResourceAsStream(KEYSTORE_PATH)) {
75-
KeyStore ks = KeyStore.getInstance("JCEKS");
76-
ks.load(keystoreStream, keystorePassword);
77-
return ks;
78-
}
79-
}
80-
81-
private static PrivateKey loadPrivateKey(KeyStore ks, String keyAlias, char[] keyPassword) throws KeyStoreException,
82-
UnrecoverableKeyException, NoSuchAlgorithmException {
83-
if (!ks.containsAlias(keyAlias)) {
84-
throw new UnrecoverableKeyException("Private key " + keyAlias + " not found in keystore");
85-
}
86-
87-
return (PrivateKey) ks.getKey(keyAlias, keyPassword);
88-
}
89-
90-
private static PublicKey loadPublicKey(KeyStore ks, String keyAlias) throws KeyStoreException, UnrecoverableKeyException {
91-
if (!ks.containsAlias(keyAlias)) {
92-
throw new UnrecoverableKeyException("Public key " + keyAlias + " not found in keystore");
93-
}
94-
95-
return ks.getCertificate(keyAlias).getPublicKey();
96-
}
97-
98-
private static byte[] sign(PrivateKey privateKey, String initialText) throws NoSuchAlgorithmException,
34+
public byte[] sign(PrivateKey privateKey, String initialText) throws NoSuchAlgorithmException,
9935
InvalidKeyException, SignatureException {
10036
Signature dsa = Signature.getInstance(ALGORITHM);
10137
dsa.initSign(privateKey);
10238
dsa.update(initialText.getBytes(StandardCharsets.UTF_8));
10339
return dsa.sign();
10440
}
10541

106-
private static boolean verify(PublicKey publicKey, byte[] signature, String initialText) throws
42+
public boolean verify(PublicKey publicKey, byte[] signature, String initialText) throws
10743
NoSuchAlgorithmException, InvalidKeyException, SignatureException {
10844
Signature dsa = Signature.getInstance(ALGORITHM);
10945
dsa.initVerify(publicKey);
11046
dsa.update(initialText.getBytes(StandardCharsets.UTF_8));
11147
return dsa.verify(signature);
11248
}
113-
114-
private static void printReadableMessages(String initialText, byte[] signature, boolean valid) {
115-
LOG.log(System.Logger.Level.INFO, "initial text: {0}", initialText);
116-
LOG.log(System.Logger.Level.INFO, "signature: {0}", BaseEncoding.base16().encode(signature));
117-
LOG.log(System.Logger.Level.INFO, "signature valid: {0}", valid);
118-
}
11949
}
Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 Dominik Schadow, dominikschadow@gmail.com
2+
* Copyright (C) 2022 Dominik Schadow, dominikschadow@gmail.com
33
*
44
* This file is part of the Java Security project.
55
*
@@ -17,17 +17,15 @@
1717
*/
1818
package de.dominikschadow.javasecurity.asymmetric;
1919

20-
import com.google.common.io.BaseEncoding;
21-
2220
import javax.crypto.BadPaddingException;
2321
import javax.crypto.Cipher;
2422
import javax.crypto.IllegalBlockSizeException;
2523
import javax.crypto.NoSuchPaddingException;
26-
import java.io.IOException;
27-
import java.io.InputStream;
2824
import java.nio.charset.StandardCharsets;
29-
import java.security.*;
30-
import java.security.cert.CertificateException;
25+
import java.security.InvalidKeyException;
26+
import java.security.NoSuchAlgorithmException;
27+
import java.security.PrivateKey;
28+
import java.security.PublicKey;
3129

3230
/**
3331
* Asymmetric encryption sample with plain Java. Loads the RSA key from the sample keystore, encrypts and decrypts
@@ -38,85 +36,19 @@
3836
* @author Dominik Schadow
3937
*/
4038
public class RSA {
41-
private static final System.Logger LOG = System.getLogger(RSA.class.getName());
4239
private static final String ALGORITHM = "RSA";
43-
private static final String KEYSTORE_PATH = "/samples.ks";
44-
45-
/**
46-
* Private constructor.
47-
*/
48-
private RSA() {
49-
}
50-
51-
public static void main(String[] args) {
52-
encrypt();
53-
}
54-
55-
private static void encrypt() {
56-
final String initialText = "RSA encryption sample text";
57-
final char[] keystorePassword = "samples".toCharArray();
58-
final String keyAlias = "asymmetric-sample-rsa";
59-
final char[] keyPassword = "asymmetric-sample-rsa".toCharArray();
60-
61-
try {
62-
KeyStore ks = loadKeystore(keystorePassword);
63-
PrivateKey privateKey = loadPrivateKey(ks, keyAlias, keyPassword);
64-
PublicKey publicKey = loadPublicKey(ks, keyAlias);
65-
66-
byte[] ciphertext = encrypt(publicKey, initialText);
67-
byte[] plaintext = decrypt(privateKey, ciphertext);
68-
69-
printReadableMessages(initialText, ciphertext, plaintext);
70-
} catch (NoSuchPaddingException | NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException |
71-
KeyStoreException | CertificateException | UnrecoverableKeyException | InvalidKeyException |
72-
IOException ex) {
73-
LOG.log(System.Logger.Level.ERROR, ex.getMessage(), ex);
74-
}
75-
}
7640

77-
private static KeyStore loadKeystore(char[] keystorePassword) throws KeyStoreException,
78-
CertificateException, NoSuchAlgorithmException, IOException {
79-
try (InputStream keystoreStream = RSA.class.getResourceAsStream(KEYSTORE_PATH)) {
80-
KeyStore ks = KeyStore.getInstance("JCEKS");
81-
ks.load(keystoreStream, keystorePassword);
82-
return ks;
83-
}
84-
}
85-
86-
private static PrivateKey loadPrivateKey(KeyStore ks, String keyAlias, char[] keyPassword) throws KeyStoreException,
87-
UnrecoverableKeyException, NoSuchAlgorithmException {
88-
if (!ks.containsAlias(keyAlias)) {
89-
throw new UnrecoverableKeyException("Private key " + keyAlias + " not found in keystore");
90-
}
91-
92-
return (PrivateKey) ks.getKey(keyAlias, keyPassword);
93-
}
94-
95-
private static PublicKey loadPublicKey(KeyStore ks, String keyAlias) throws KeyStoreException, UnrecoverableKeyException {
96-
if (!ks.containsAlias(keyAlias)) {
97-
throw new UnrecoverableKeyException("Public key " + keyAlias + " not found in keystore");
98-
}
99-
100-
return ks.getCertificate(keyAlias).getPublicKey();
101-
}
102-
103-
private static byte[] encrypt(PublicKey publicKey, String initialText) throws NoSuchPaddingException,
41+
public byte[] encrypt(PublicKey publicKey, String initialText) throws NoSuchPaddingException,
10442
NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
10543
Cipher cipher = Cipher.getInstance(ALGORITHM);
10644
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
10745
return cipher.doFinal(initialText.getBytes(StandardCharsets.UTF_8));
10846
}
10947

110-
private static byte[] decrypt(PrivateKey privateKey, byte[] ciphertext) throws NoSuchPaddingException,
48+
public byte[] decrypt(PrivateKey privateKey, byte[] ciphertext) throws NoSuchPaddingException,
11149
NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
11250
Cipher cipher = Cipher.getInstance(ALGORITHM);
11351
cipher.init(Cipher.DECRYPT_MODE, privateKey);
11452
return cipher.doFinal(ciphertext);
11553
}
116-
117-
private static void printReadableMessages(String initialText, byte[] ciphertext, byte[] plaintext) {
118-
LOG.log(System.Logger.Level.INFO, "initial text: {0}", initialText);
119-
LOG.log(System.Logger.Level.INFO, "cipher text: {0}", BaseEncoding.base16().encode(ciphertext));
120-
LOG.log(System.Logger.Level.INFO, "plain text: {0}", new String(plaintext, StandardCharsets.UTF_8));
121-
}
12254
}
Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 Dominik Schadow, dominikschadow@gmail.com
2+
* Copyright (C) 2022 Dominik Schadow, dominikschadow@gmail.com
33
*
44
* This file is part of the Java Security project.
55
*
@@ -17,19 +17,16 @@
1717
*/
1818
package de.dominikschadow.javasecurity.symmetric;
1919

20-
import com.google.common.io.BaseEncoding;
21-
2220
import javax.crypto.BadPaddingException;
2321
import javax.crypto.Cipher;
2422
import javax.crypto.IllegalBlockSizeException;
2523
import javax.crypto.NoSuchPaddingException;
2624
import javax.crypto.spec.IvParameterSpec;
2725
import javax.crypto.spec.SecretKeySpec;
28-
import java.io.IOException;
29-
import java.io.InputStream;
3026
import java.nio.charset.StandardCharsets;
31-
import java.security.*;
32-
import java.security.cert.CertificateException;
27+
import java.security.InvalidAlgorithmParameterException;
28+
import java.security.InvalidKeyException;
29+
import java.security.NoSuchAlgorithmException;
3330

3431
/**
3532
* Symmetric encryption sample with plain Java. Loads the AES key from the sample keystore, encrypts and decrypts sample
@@ -44,72 +41,25 @@
4441
* @author Dominik Schadow
4542
*/
4643
public class AES {
47-
private static final System.Logger LOG = System.getLogger(AES.class.getName());
48-
private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
49-
private static final String KEYSTORE_PATH = "/samples.ks";
50-
private Cipher cipher;
51-
52-
public static void main(String[] args) {
53-
AES aes = new AES();
54-
aes.encrypt();
55-
}
56-
57-
private void encrypt() {
58-
final String initialText = "AES encryption sample text";
59-
final char[] keystorePassword = "samples".toCharArray();
60-
final String keyAlias = "symmetric-sample";
61-
final char[] keyPassword = "symmetric-sample".toCharArray();
62-
63-
try {
64-
cipher = Cipher.getInstance(ALGORITHM);
65-
KeyStore ks = loadKeystore(keystorePassword);
66-
Key key = loadKey(ks, keyAlias, keyPassword);
67-
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getEncoded(), "AES");
68-
byte[] ciphertext = encrypt(secretKeySpec, initialText);
69-
byte[] plaintext = decrypt(secretKeySpec, ciphertext);
70-
71-
printReadableMessages(initialText, ciphertext, plaintext);
72-
} catch (NoSuchPaddingException | NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException |
73-
KeyStoreException | CertificateException | UnrecoverableKeyException |
74-
InvalidAlgorithmParameterException | InvalidKeyException | IOException ex) {
75-
LOG.log(System.Logger.Level.ERROR, ex.getMessage(), ex);
76-
}
77-
}
44+
private final SecretKeySpec secretKeySpec;
45+
private final Cipher cipher;
7846

79-
private KeyStore loadKeystore(char[] keystorePassword) throws KeyStoreException,
80-
CertificateException, NoSuchAlgorithmException, IOException {
81-
try (InputStream keystoreStream = getClass().getResourceAsStream(KEYSTORE_PATH)) {
82-
KeyStore ks = KeyStore.getInstance("JCEKS");
83-
ks.load(keystoreStream, keystorePassword);
47+
public AES(SecretKeySpec secretKeySpec, String algorithm) throws NoSuchPaddingException, NoSuchAlgorithmException {
48+
cipher = Cipher.getInstance(algorithm);
8449

85-
return ks;
86-
}
50+
this.secretKeySpec = secretKeySpec;
8751
}
8852

89-
private static Key loadKey(KeyStore ks, String keyAlias, char[] keyPassword) throws KeyStoreException,
90-
UnrecoverableKeyException, NoSuchAlgorithmException {
91-
if (!ks.containsAlias(keyAlias)) {
92-
throw new UnrecoverableKeyException("Secret key " + keyAlias + " not found in keystore");
93-
}
94-
95-
return ks.getKey(keyAlias, keyPassword);
96-
}
97-
98-
private byte[] encrypt(SecretKeySpec secretKeySpec, String initialText) throws
99-
BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
53+
public byte[] encrypt(String initialText) throws
54+
BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException {
10055
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
56+
10157
return cipher.doFinal(initialText.getBytes(StandardCharsets.UTF_8));
10258
}
10359

104-
private byte[] decrypt(SecretKeySpec secretKeySpec, byte[] ciphertext) throws
60+
public byte[] decrypt(byte[] ciphertext) throws
10561
BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException {
10662
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(cipher.getIV()));
10763
return cipher.doFinal(ciphertext);
10864
}
109-
110-
private static void printReadableMessages(String initialText, byte[] ciphertext, byte[] plaintext) {
111-
LOG.log(System.Logger.Level.INFO, "initial text: {0}", initialText);
112-
LOG.log(System.Logger.Level.INFO, "cipher text: {0}", BaseEncoding.base16().encode(ciphertext));
113-
LOG.log(System.Logger.Level.INFO, "plain text: {0}", new String(plaintext, StandardCharsets.UTF_8));
114-
}
11565
}

0 commit comments

Comments
 (0)