Skip to content

Commit ffe027f

Browse files
committed
Added a single class for loading keys.
1 parent 54cf118 commit ffe027f

File tree

6 files changed

+68
-143
lines changed

6 files changed

+68
-143
lines changed

src/main/java/ro/kuberam/libs/java/crypto/encrypt/AsymmetricEncryption.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
import javax.crypto.NoSuchPaddingException;
3838

3939
import ro.kuberam.libs.java.crypto.CryptoException;
40-
import ro.kuberam.libs.java.crypto.keyManagement.LoadPrivateKey;
41-
import ro.kuberam.libs.java.crypto.keyManagement.LoadPublicKey;
40+
import ro.kuberam.libs.java.crypto.keyManagement.Load;
4241
import ro.kuberam.libs.java.crypto.utils.Buffer;
4342

4443
/**
@@ -109,7 +108,7 @@ public static String encryptBinary(InputStream data, String base64PublicKey, Str
109108
Cipher cipher;
110109
try {
111110
cipher = Cipher.getInstance(transformationName);
112-
PublicKey publicKey = LoadPublicKey.run(base64PublicKey, algorithm, provider);
111+
PublicKey publicKey = Load.publicKey(base64PublicKey, algorithm, provider);
113112
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
114113

115114
byte[] buf = new byte[Buffer.TRANSFER_SIZE];
@@ -136,7 +135,7 @@ public static String decryptBinary(InputStream data, String base64PrivateKey, St
136135
Cipher cipher;
137136
try {
138137
cipher = Cipher.getInstance(transformationName);
139-
PrivateKey privateKey = LoadPrivateKey.run(base64PrivateKey, algorithm, provider);
138+
PrivateKey privateKey = Load.privateKey(base64PrivateKey, algorithm, provider);
140139
cipher.init(Cipher.DECRYPT_MODE, privateKey);
141140

142141
byte[] buf = new byte[Buffer.TRANSFER_SIZE];
@@ -179,7 +178,6 @@ public static byte[] getBytes(String str) throws IOException {
179178
}
180179
}
181180
// add providers to loadPublicKey() and loadPrivateKey()
182-
// test AsymmetricEncryption with ad-hoc generated keys
183181
// add AsymmetricEncryption for binaries
184182
// change the output of symmetric encryption to base64Binary string (and tests, too)
185183
// add / improve error message for AsymmetricEncryption when the text to be encrypted is larger that the key
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ro.kuberam.libs.java.crypto.keyManagement;
2+
3+
import java.security.KeyFactory;
4+
import java.security.NoSuchAlgorithmException;
5+
import java.security.NoSuchProviderException;
6+
import java.security.PrivateKey;
7+
import java.security.PublicKey;
8+
import java.security.spec.InvalidKeySpecException;
9+
import java.security.spec.PKCS8EncodedKeySpec;
10+
import java.security.spec.X509EncodedKeySpec;
11+
import java.util.Base64;
12+
import java.util.Optional;
13+
import java.util.regex.Pattern;
14+
15+
public class Load {
16+
private static Pattern pattern = Pattern.compile("(?m)(?s)^---*BEGIN.*---*$(.*)^---*END.*---*$.*");
17+
18+
public static PublicKey publicKey(String base64PublicKey, String algorithm, String provider)
19+
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
20+
provider = Optional.ofNullable(provider).filter(str -> !str.isEmpty()).orElse("SunRsaSign");
21+
String cleanedKey = pattern.matcher(base64PublicKey).replaceFirst("$1");
22+
23+
X509EncodedKeySpec spec = new X509EncodedKeySpec(Base64.getMimeDecoder().decode(cleanedKey));
24+
KeyFactory kf = KeyFactory.getInstance(algorithm, provider);
25+
26+
return kf.generatePublic(spec);
27+
}
28+
29+
public static PrivateKey privateKey(String base64PrivateKey, String algorithm, String provider)
30+
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
31+
provider = Optional.ofNullable(provider).filter(str -> !str.isEmpty()).orElse("SunRsaSign");
32+
String cleanedKey = pattern.matcher(base64PrivateKey).replaceFirst("$1");
33+
34+
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
35+
Base64.getMimeDecoder().decode(cleanedKey));
36+
37+
KeyFactory kf = KeyFactory.getInstance(algorithm, provider);
38+
39+
return kf.generatePrivate(keySpec);
40+
}
41+
42+
}

src/main/java/ro/kuberam/libs/java/crypto/keyManagement/LoadPrivateKey.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/main/java/ro/kuberam/libs/java/crypto/keyManagement/LoadPublicKey.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/test/java/ro/kuberam/libs/java/crypto/encrypt/AsymmetricEncryptionTest.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,43 @@
2626
import java.nio.file.Paths;
2727
import java.security.PrivateKey;
2828
import java.security.PublicKey;
29+
import java.util.Map;
2930

3031
import org.junit.Test;
3132

3233
import ro.kuberam.libs.java.crypto.CryptoModuleTests;
33-
import ro.kuberam.libs.java.crypto.keyManagement.LoadPrivateKey;
34-
import ro.kuberam.libs.java.crypto.keyManagement.LoadPublicKey;
34+
import ro.kuberam.libs.java.crypto.keyManagement.GenerateKeyPair;
35+
import ro.kuberam.libs.java.crypto.keyManagement.Load;
3536

3637
public class AsymmetricEncryptionTest extends CryptoModuleTests {
3738

3839
@Test
39-
public void encryptStringWithRsaAsymmetricKey() throws Exception {
40+
public void encryptAndDecryptStringWithRsaAsymmetricKey() throws Exception {
4041
String transformation = "RSA/ECB/PKCS1Padding";
4142
String algorithm = "RSA";
42-
43+
4344
String base64PublicKey = new String(
4445
Files.readAllBytes(Paths.get(getClass().getResource("../rsa-public-key.key").toURI())), UTF_8);
45-
PublicKey publicKey = LoadPublicKey.run(base64PublicKey, algorithm, null);
46-
46+
PublicKey publicKey = Load.publicKey(base64PublicKey, algorithm, null);
47+
4748
String base64PrivateKey = new String(
4849
Files.readAllBytes(Paths.get(getClass().getResource("../rsa-private-key.key").toURI())), UTF_8);
49-
PrivateKey privateKey = LoadPrivateKey.run(base64PrivateKey, algorithm, null);
50+
PrivateKey privateKey = Load.privateKey(base64PrivateKey, algorithm, null);
51+
52+
String encryptedText = AsymmetricEncryption.encryptString(longString, publicKey, transformation);
53+
String decryptedText = AsymmetricEncryption.decryptString(encryptedText, privateKey, transformation);
54+
55+
assertEquals(longString, decryptedText);
56+
}
57+
58+
@Test
59+
public void encryptAndDecryptStringWithAdHocGeneratedKey() throws Exception {
60+
String transformation = "RSA/ECB/PKCS1Padding";
61+
String algorithm = "RSA";
62+
Map<String, String> keys = GenerateKeyPair.run("RSA");
63+
64+
PublicKey publicKey = Load.publicKey(keys.get("public-key"), algorithm, null);
65+
PrivateKey privateKey = Load.privateKey(keys.get("private-key"), algorithm, null);
5066

5167
String encryptedText = AsymmetricEncryption.encryptString(longString, publicKey, transformation);
5268
String decryptedText = AsymmetricEncryption.decryptString(encryptedText, privateKey, transformation);

src/test/java/ro/kuberam/libs/java/crypto/encrypt/RSAUtil.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)