4242import javax .crypto .NoSuchPaddingException ;
4343
4444import ro .kuberam .libs .java .crypto .CryptoException ;
45+ import ro .kuberam .libs .java .crypto .keyManagement .LoadPrivateKey ;
46+ import ro .kuberam .libs .java .crypto .keyManagement .LoadPublicKey ;
4547import 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
0 commit comments