4747 */
4848public class AsymmetricEncryption {
4949
50- public static String encryptString (String data , String base64PublicKey , String transformationName )
50+ public static String encryptString (String data , PublicKey publicKey , String transformationName )
5151 throws CryptoException , IOException {
5252 String provider = "SUN" ;
5353
54- return encryptString (data , base64PublicKey , transformationName , provider );
54+ return encryptString (data , publicKey , transformationName , provider );
5555 }
5656
57- public static String encryptString (String data , String base64PublicKey , String transformationName , String provider )
57+ public static String encryptString (String data , PublicKey publicKey , String transformationName , String provider )
5858 throws CryptoException , IOException {
5959 byte [] dataBytes = data .getBytes (UTF_8 );
60- String algorithm = transformationName .split ("/" )[0 ];
6160 byte [] resultBytes = null ;
6261
6362 Cipher cipher ;
6463 try {
6564 cipher = Cipher .getInstance (transformationName );
66- PublicKey publicKey = LoadPublicKey .run (base64PublicKey , algorithm , provider );
6765 cipher .init (Cipher .ENCRYPT_MODE , publicKey );
6866 resultBytes = cipher .doFinal (dataBytes );
6967 } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException
@@ -76,23 +74,22 @@ public static String encryptString(String data, String base64PublicKey, String t
7674 return Base64 .getEncoder ().encodeToString (resultBytes );
7775 }
7876
79- public static String decryptString (String encryptedData , String base64PrivateKey , String transformationName )
77+ public static String decryptString (String encryptedData , PrivateKey privateKey , String transformationName )
8078 throws CryptoException , IOException {
8179 String provider = "SUN" ;
8280
83- return decryptString (encryptedData , base64PrivateKey , transformationName , provider );
81+ return decryptString (encryptedData , privateKey , transformationName , provider );
8482 }
8583
86- public static String decryptString (String encryptedData , String base64PrivateKey , String transformationName ,
84+ public static String decryptString (String encryptedData , PrivateKey privateKey , String transformationName ,
8785 String provider ) throws CryptoException , IOException {
8886 byte [] dataBytes = Base64 .getDecoder ().decode (encryptedData );
89- String algorithm = transformationName .split ("/" )[0 ];
9087 byte [] resultBytes = null ;
9188
9289 Cipher cipher ;
9390 try {
9491 cipher = Cipher .getInstance (transformationName );
95- cipher .init (Cipher .DECRYPT_MODE , LoadPrivateKey . run ( base64PrivateKey , algorithm , provider ) );
92+ cipher .init (Cipher .DECRYPT_MODE , privateKey );
9693 resultBytes = cipher .doFinal (dataBytes );
9794 } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException
9895 | InvalidKeyException e ) {
0 commit comments