Skip to content

Commit a5ef7d1

Browse files
committed
Added LoadPrivateKey and LoadPublicKey classes.
1 parent 425e5e8 commit a5ef7d1

File tree

4 files changed

+98
-31
lines changed

4 files changed

+98
-31
lines changed

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

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import javax.crypto.NoSuchPaddingException;
4343

4444
import ro.kuberam.libs.java.crypto.CryptoException;
45+
import ro.kuberam.libs.java.crypto.keyManagement.LoadPrivateKey;
46+
import ro.kuberam.libs.java.crypto.keyManagement.LoadPublicKey;
4547
import ro.kuberam.libs.java.crypto.utils.Buffer;
4648

4749
/**
@@ -66,7 +68,7 @@ public static String encryptString(String data, String base64PublicKey, String t
6668
Cipher cipher;
6769
try {
6870
cipher = Cipher.getInstance(transformationName);
69-
PublicKey publicKey = loadPublicKey(base64PublicKey, algorithm, provider);
71+
PublicKey publicKey = LoadPublicKey.run(base64PublicKey, algorithm, provider);
7072
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
7173
resultBytes = cipher.doFinal(dataBytes);
7274
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException
@@ -95,7 +97,7 @@ public static String decryptString(String encryptedData, String base64PrivateKey
9597
Cipher cipher;
9698
try {
9799
cipher = Cipher.getInstance(transformationName);
98-
cipher.init(Cipher.DECRYPT_MODE, loadPrivateKey(base64PrivateKey, algorithm, provider));
100+
cipher.init(Cipher.DECRYPT_MODE, LoadPrivateKey.run(base64PrivateKey, algorithm, provider));
99101
resultBytes = cipher.doFinal(dataBytes);
100102
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException
101103
| InvalidKeyException e) {
@@ -115,7 +117,7 @@ public static String encryptBinary(InputStream data, String base64PublicKey, Str
115117
Cipher cipher;
116118
try {
117119
cipher = Cipher.getInstance(transformationName);
118-
PublicKey publicKey = loadPublicKey(base64PublicKey, algorithm, provider);
120+
PublicKey publicKey = LoadPublicKey.run(base64PublicKey, algorithm, provider);
119121
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
120122

121123
byte[] buf = new byte[Buffer.TRANSFER_SIZE];
@@ -142,7 +144,7 @@ public static String decryptBinary(InputStream data, String base64PrivateKey, St
142144
Cipher cipher;
143145
try {
144146
cipher = Cipher.getInstance(transformationName);
145-
PrivateKey privateKey = loadPrivateKey(base64PrivateKey, algorithm, provider);
147+
PrivateKey privateKey = LoadPrivateKey.run(base64PrivateKey, algorithm, provider);
146148
cipher.init(Cipher.DECRYPT_MODE, privateKey);
147149

148150
byte[] buf = new byte[Buffer.TRANSFER_SIZE];
@@ -183,34 +185,8 @@ public static byte[] getBytes(String str) throws IOException {
183185
return bos.toByteArray();
184186
}
185187
}
186-
187-
private static PublicKey loadPublicKey(String base64PublicKey, String algorithm, String provider)
188-
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
189-
// provider = Optional.ofNullable(provider).filter(str ->
190-
// !str.isEmpty()).orElse("SunRsaSign");
191-
provider = "SunRsaSign";
192-
193-
X509EncodedKeySpec spec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes(UTF_8)));
194-
KeyFactory kf = KeyFactory.getInstance(algorithm, provider);
195-
196-
return kf.generatePublic(spec);
197-
}
198-
199-
private static PrivateKey loadPrivateKey(String base64PrivateKey, String algorithm, String provider)
200-
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
201-
// provider = Optional.ofNullable(provider).filter(str ->
202-
// !str.isEmpty()).orElse("SunRsaSign");
203-
provider = "SunRsaSign";
204-
205-
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
206-
Base64.getDecoder().decode(base64PrivateKey.getBytes(UTF_8)));
207-
208-
KeyFactory kf = KeyFactory.getInstance(algorithm, provider);
209-
210-
return kf.generatePrivate(keySpec);
211-
}
212188
}
213-
// move loadPublicKey() and loadPrivateKey() to Key management section
189+
// move loadPrivateKey() to Key management section
214190
// add providers to loadPublicKey() and loadPrivateKey()
215191
// test AsymmetricEncryption with ad-hoc generated keys
216192
// add AsymmetricEncryption for binaries
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ro.kuberam.libs.java.crypto.keyManagement;
2+
3+
import static java.nio.charset.StandardCharsets.UTF_8;
4+
5+
import java.security.KeyFactory;
6+
import java.security.NoSuchAlgorithmException;
7+
import java.security.NoSuchProviderException;
8+
import java.security.PrivateKey;
9+
import java.security.spec.InvalidKeySpecException;
10+
import java.security.spec.PKCS8EncodedKeySpec;
11+
import java.util.Base64;
12+
13+
public class LoadPrivateKey {
14+
public static PrivateKey run(String base64PrivateKey, String algorithm, String provider)
15+
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
16+
// provider = Optional.ofNullable(provider).filter(str ->
17+
// !str.isEmpty()).orElse("SunRsaSign");
18+
provider = "SunRsaSign";
19+
20+
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
21+
Base64.getDecoder().decode(base64PrivateKey.getBytes(UTF_8)));
22+
23+
KeyFactory kf = KeyFactory.getInstance(algorithm, provider);
24+
25+
return kf.generatePrivate(keySpec);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ro.kuberam.libs.java.crypto.keyManagement;
2+
3+
import static java.nio.charset.StandardCharsets.UTF_8;
4+
5+
import java.security.KeyFactory;
6+
import java.security.NoSuchAlgorithmException;
7+
import java.security.NoSuchProviderException;
8+
import java.security.PublicKey;
9+
import java.security.spec.InvalidKeySpecException;
10+
import java.security.spec.X509EncodedKeySpec;
11+
import java.util.Base64;
12+
13+
public class LoadPublicKey {
14+
15+
public static PublicKey run(String base64PublicKey, String algorithm, String provider)
16+
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
17+
// provider = Optional.ofNullable(provider).filter(str ->
18+
// !str.isEmpty()).orElse("SunRsaSign");
19+
provider = "SunRsaSign";
20+
21+
X509EncodedKeySpec spec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes(UTF_8)));
22+
KeyFactory kf = KeyFactory.getInstance(algorithm, provider);
23+
24+
return kf.generatePublic(spec);
25+
}
26+
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* EXPath Cryptographic Module
3+
* Java Library providing an EXPath Cryptographic Module
4+
* Copyright (C) 2015 Kuberam
5+
*
6+
* This library is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public License
8+
* as published by the Free Software Foundation; either version 2.1
9+
* of the License, or (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this library; if not, write to the Free Software Foundation,
18+
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19+
*/
20+
package ro.kuberam.libs.java.crypto.keyManagement;
21+
22+
import java.util.Map;
23+
24+
import org.junit.Test;
25+
26+
import ro.kuberam.tests.junit.BaseTest;
27+
28+
public class TranslateKeyTest extends BaseTest {
29+
30+
@Test
31+
public void rsaKeyPair() throws Exception {
32+
Map<String, String> keys = GenerateKeyPair.run("RSA");
33+
34+
System.out.println("Private key:\n" + keys.get("private-key"));
35+
System.out.println("Public key:\n" + keys.get("public-key"));
36+
}
37+
}

0 commit comments

Comments
 (0)