@@ -55,14 +55,12 @@ public static String encryptString(String data, String base64PublicKey, String t
5555 throws CryptoException , IOException {
5656 String provider = "SUN" ;
5757
58- try (InputStream bais = new ByteArrayInputStream (data .getBytes (UTF_8 ))) {
59- return encrypt (bais , base64PublicKey , transformationName , provider );
60- }
58+ return encryptString (data , base64PublicKey , transformationName , provider );
6159 }
6260
6361 public static String encryptString (String data , String base64PublicKey , String transformationName , String provider )
6462 throws CryptoException , IOException {
65- InputStream dataIs = new ByteArrayInputStream ( data .getBytes (UTF_8 ) );
63+ byte [] dataBytes = data .getBytes (UTF_8 );
6664 String algorithm = transformationName .split ("/" )[0 ];
6765 byte [] resultBytes = null ;
6866
@@ -71,13 +69,7 @@ public static String encryptString(String data, String base64PublicKey, String t
7169 cipher = Cipher .getInstance (transformationName );
7270 PublicKey publicKey = loadPublicKey (base64PublicKey , algorithm , provider );
7371 cipher .init (Cipher .ENCRYPT_MODE , publicKey );
74-
75- byte [] buf = new byte [Buffer .TRANSFER_SIZE ];
76- int read = -1 ;
77- while ((read = dataIs .read (buf )) > -1 ) {
78- cipher .update (buf , 0 , read );
79- }
80- resultBytes = cipher .doFinal ();
72+ resultBytes = cipher .doFinal (dataBytes );
8173 } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException
8274 | InvalidKeyException e ) {
8375 throw new CryptoException (e );
@@ -88,8 +80,36 @@ public static String encryptString(String data, String base64PublicKey, String t
8880 return Base64 .getEncoder ().encodeToString (resultBytes );
8981 }
9082
91- public static String encrypt ( InputStream data , String base64PublicKey , String transformationName , String provider )
83+ public static String decryptString ( String encryptedData , String base64PrivateKey , String transformationName )
9284 throws CryptoException , IOException {
85+ String provider = "SUN" ;
86+
87+ return decryptString (encryptedData , base64PrivateKey , transformationName , provider );
88+ }
89+
90+ public static String decryptString (String encryptedData , String base64PrivateKey , String transformationName ,
91+ String provider ) throws CryptoException , IOException {
92+ byte [] dataBytes = Base64 .getDecoder ().decode (encryptedData );
93+ String algorithm = transformationName .split ("/" )[0 ];
94+ byte [] resultBytes = null ;
95+
96+ Cipher cipher ;
97+ try {
98+ cipher = Cipher .getInstance (transformationName );
99+ cipher .init (Cipher .DECRYPT_MODE , loadPrivateKey (base64PrivateKey , algorithm , provider ));
100+ resultBytes = cipher .doFinal (dataBytes );
101+ } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException
102+ | InvalidKeyException e ) {
103+ throw new CryptoException (e );
104+ } catch (Exception e ) {
105+ e .printStackTrace ();
106+ }
107+
108+ return new String (resultBytes , UTF_8 );
109+ }
110+
111+ public static String encryptBinary (InputStream data , String base64PublicKey , String transformationName ,
112+ String provider ) throws CryptoException , IOException {
93113 String algorithm = transformationName .split ("/" )[0 ];
94114 byte [] resultBytes = null ;
95115
@@ -115,32 +135,16 @@ public static String encrypt(InputStream data, String base64PublicKey, String tr
115135 return getString (resultBytes );
116136 }
117137
118- public static String decryptString (String encryptedInput , String base64PrivateKey , String transformationName )
119- throws CryptoException , IOException {
120- String provider = "SUN" ;
121-
122- try (InputStream bais = new ByteArrayInputStream (getBytes (encryptedInput ))) {
123- return decrypt (bais , base64PrivateKey , transformationName , provider );
124- }
125- }
126-
127- public static String decryptString (String data , String base64PrivateKey , String transformationName , String provider )
128- throws CryptoException , IOException {
129- try (InputStream bais = new ByteArrayInputStream (getBytes (data ))) {
130- return decrypt (bais , base64PrivateKey , transformationName , provider );
131- }
132- }
133-
134- public static String decrypt (InputStream data , String base64PrivateKey , String transformationName , String provider )
135- throws CryptoException , IOException {
138+ public static String decryptBinary (InputStream data , String base64PrivateKey , String transformationName ,
139+ String provider ) throws CryptoException , IOException {
136140 String algorithm = transformationName .split ("/" )[0 ];
137141 byte [] resultBytes = null ;
138142
139143 Cipher cipher ;
140144 try {
141145 cipher = Cipher .getInstance (transformationName );
142- PrivateKey publicKey = loadPrivateKey (base64PrivateKey , algorithm , provider );
143- cipher .init (Cipher .DECRYPT_MODE , publicKey );
146+ PrivateKey privateKey = loadPrivateKey (base64PrivateKey , algorithm , provider );
147+ cipher .init (Cipher .DECRYPT_MODE , privateKey );
144148
145149 byte [] buf = new byte [Buffer .TRANSFER_SIZE ];
146150 int read = -1 ;
@@ -199,7 +203,8 @@ private static PrivateKey loadPrivateKey(String base64PrivateKey, String algorit
199203 // !str.isEmpty()).orElse("SunRsaSign");
200204 provider = "SunRsaSign" ;
201205
202- PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec (Base64 .getDecoder ().decode (base64PrivateKey .getBytes (UTF_8 )));
206+ PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec (
207+ Base64 .getDecoder ().decode (base64PrivateKey .getBytes (UTF_8 )));
203208
204209 KeyFactory kf = KeyFactory .getInstance (algorithm , provider );
205210
0 commit comments