Skip to content

Commit e03cc18

Browse files
committed
feat: load RSA public key via modulus and exponent
1 parent 6ac9f1a commit e03cc18

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

key-pair-loader/src/main/java/com/onixbyte/security/KeyLoader.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717

1818
package com.onixbyte.security;
1919

20+
import com.onixbyte.security.exception.KeyLoadingException;
21+
2022
import java.security.PrivateKey;
2123
import java.security.PublicKey;
24+
import java.security.interfaces.RSAPublicKey;
2225

2326
/**
2427
* The {@code KeyLoader} class provides utility methods for loading keys pairs from PEM-formatted
@@ -49,6 +52,10 @@ public interface KeyLoader {
4952
*/
5053
PublicKey loadPublicKey(String pemKeyText);
5154

55+
default RSAPublicKey loadPublicKey(String modulus, String exponent) {
56+
throw new KeyLoadingException("This key loader does not support RSA Public key loading.");
57+
}
58+
5259
/**
5360
* Retrieves the raw content of a PEM formatted key by removing unnecessary headers, footers,
5461
* and new line characters.

key-pair-loader/src/main/java/com/onixbyte/security/impl/RSAKeyLoader.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import com.onixbyte.security.KeyLoader;
2121
import com.onixbyte.security.exception.KeyLoadingException;
2222

23+
import java.math.BigInteger;
2324
import java.security.KeyFactory;
2425
import java.security.NoSuchAlgorithmException;
2526
import java.security.interfaces.RSAPrivateKey;
2627
import java.security.interfaces.RSAPublicKey;
2728
import java.security.spec.InvalidKeySpecException;
2829
import java.security.spec.PKCS8EncodedKeySpec;
30+
import java.security.spec.RSAPublicKeySpec;
2931
import java.security.spec.X509EncodedKeySpec;
3032
import java.util.Base64;
3133

@@ -47,6 +49,9 @@
4749
public class RSAKeyLoader implements KeyLoader {
4850

4951
private final Base64.Decoder decoder;
52+
53+
private final Base64.Decoder urlDecoder;
54+
5055
private final KeyFactory keyFactory;
5156

5257
/**
@@ -58,6 +63,7 @@ public class RSAKeyLoader implements KeyLoader {
5863
public RSAKeyLoader() {
5964
try {
6065
this.decoder = Base64.getDecoder();
66+
this.urlDecoder = Base64.getUrlDecoder();
6167
this.keyFactory = KeyFactory.getInstance("RSA");
6268
} catch (NoSuchAlgorithmException e) {
6369
throw new KeyLoadingException(e);
@@ -133,4 +139,22 @@ public RSAPublicKey loadPublicKey(String pemKeyText) {
133139
throw new KeyLoadingException("Key spec is invalid.", e);
134140
}
135141
}
142+
143+
@Override
144+
public RSAPublicKey loadPublicKey(String modulus, String exponent) {
145+
try {
146+
var _modulus = new BigInteger(1, urlDecoder.decode(modulus));
147+
var _exponent = new BigInteger(1, urlDecoder.decode(exponent));
148+
149+
var keySpec = new RSAPublicKeySpec(_modulus, _exponent);
150+
var kf = KeyFactory.getInstance("RSA");
151+
if (kf.generatePublic(keySpec) instanceof RSAPublicKey rsaPublicKey) {
152+
return rsaPublicKey;
153+
} else {
154+
throw new KeyLoadingException("Cannot generate RSA public key with given modulus and exponent.");
155+
}
156+
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
157+
throw new KeyLoadingException("Cannot generate RSA public key with given modulus and exponent.", e);
158+
}
159+
}
136160
}

0 commit comments

Comments
 (0)