44import javax .crypto .Cipher ;
55import javax .crypto .IllegalBlockSizeException ;
66import javax .crypto .NoSuchPaddingException ;
7+
8+ import java .io .ByteArrayInputStream ;
79import java .security .*;
10+ import java .security .cert .CertificateException ;
11+ import java .security .cert .CertificateFactory ;
12+ import java .security .cert .X509Certificate ;
813import java .security .spec .InvalidKeySpecException ;
914import java .security .spec .X509EncodedKeySpec ;
1015import java .util .Base64 ;
@@ -14,6 +19,8 @@ public class AsymEncryption {
1419 private final PublicKey publicKey ;
1520 private static final String PUBLIC_KEY_HEADER = "-----BEGIN PUBLIC KEY-----" ;
1621 private static final String PUBLIC_KEY_FOOTER = "-----END PUBLIC KEY-----" ;
22+ private static final String PEM_HEADER = "-----BEGIN (.*)-----" ;
23+ private static final String PEM_FOOTER = "-----END (.*)-----" ;
1724 private static final String CIPHER_TRANSFORM = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding" ;
1825
1926 /**
@@ -22,24 +29,51 @@ public class AsymEncryption {
2229 * @param publicKeyInPem a Public Key in PEM format
2330 */
2431 public AsymEncryption (String publicKeyInPem ) {
25- publicKeyInPem = publicKeyInPem
26- .replace (PUBLIC_KEY_HEADER , "" )
27- .replace (PUBLIC_KEY_FOOTER , "" )
28- .replaceAll ("\\ s" , "" );
2932
30- byte [] decoded = Base64 .getDecoder ().decode (publicKeyInPem );
31- X509EncodedKeySpec spec = new X509EncodedKeySpec (decoded );
32- KeyFactory kf ;
33- try {
34- kf = KeyFactory .getInstance ("RSA" );
35- } catch (NoSuchAlgorithmException e ) {
36- throw new SDKException ("RSA is not a valid algorithm!!!???!!!" , e );
33+ PublicKey pubKey = null ;
34+
35+ String base64EncodedPem = publicKeyInPem
36+ .replaceAll (PEM_HEADER , "" )
37+ .replaceAll (PEM_FOOTER , "" )
38+ .replaceAll ("\\ s" , "" )
39+ .replaceAll ("\r \n " , "" )
40+ .replaceAll ("\n " , "" )
41+ .trim ();
42+
43+
44+ byte [] decoded = Base64 .getDecoder ().decode (base64EncodedPem );
45+
46+ // Check if the PEM contains a certificate
47+ if (publicKeyInPem .contains ("BEGIN CERTIFICATE" )) {
48+ try {
49+ // Parse the certificate
50+ CertificateFactory certFactory = CertificateFactory .getInstance ("X.509" );
51+ X509Certificate cert = (X509Certificate ) certFactory .generateCertificate (new ByteArrayInputStream (decoded ));
52+ pubKey = cert .getPublicKey ();
53+ } catch (CertificateException e ) {
54+ throw new SDKException ("x509.ParseCertificate failed: " + e .getMessage (), e );
55+ }
56+ } else {
57+ // Otherwise, treat it as a PKIX public key
58+ X509EncodedKeySpec spec = new X509EncodedKeySpec (decoded );
59+ KeyFactory keyFactory ;
60+ try {
61+ keyFactory = KeyFactory .getInstance ("RSA" );
62+ } catch (NoSuchAlgorithmException e ) {
63+ throw new SDKException ("RSA is not a valid algorithm!!!???!!!" , e );
64+ }
65+ try {
66+ pubKey = keyFactory .generatePublic (spec );
67+ } catch (InvalidKeySpecException e ) {
68+ throw new SDKException ("error creating asymmetric encryption" , e );
69+ }
3770 }
3871
39- try {
40- this .publicKey = kf .generatePublic (spec );
41- } catch (InvalidKeySpecException e ) {
42- throw new SDKException ("error creating asymmetric encryption" , e );
72+ // Check if the public key is RSA
73+ if (pubKey instanceof java .security .interfaces .RSAPublicKey ) {
74+ this .publicKey = pubKey ;
75+ } else {
76+ throw new SDKException ("Not an RSA PEM formatted public key" );
4377 }
4478 }
4579
0 commit comments