2525import java .io .ByteArrayOutputStream ;
2626import java .io .IOException ;
2727import java .io .InputStream ;
28- import java .security .InvalidAlgorithmParameterException ;
2928import java .security .InvalidKeyException ;
3029import java .security .KeyFactory ;
3130import java .security .NoSuchAlgorithmException ;
3534import java .security .spec .InvalidKeySpecException ;
3635import java .security .spec .PKCS8EncodedKeySpec ;
3736import java .security .spec .X509EncodedKeySpec ;
38- import java .util .Optional ;
37+ import java .util .Base64 ;
3938import java .util .StringTokenizer ;
4039
41- import javax .annotation .Nullable ;
4240import javax .crypto .BadPaddingException ;
4341import javax .crypto .Cipher ;
4442import javax .crypto .IllegalBlockSizeException ;
4543import javax .crypto .NoSuchPaddingException ;
46- import javax .crypto .spec .IvParameterSpec ;
47- import javax .crypto .spec .SecretKeySpec ;
4844
4945import ro .kuberam .libs .java .crypto .CryptoException ;
5046import ro .kuberam .libs .java .crypto .utils .Buffer ;
5551 */
5652public class AsymmetricEncryption {
5753
58- public static String encryptString (String input , String publicKey , String transformationName )
54+ public static String encryptString (String data , String base64PublicKey , String transformationName )
5955 throws CryptoException , IOException {
60- try (InputStream bais = new ByteArrayInputStream (input .getBytes (UTF_8 ))) {
61- return encrypt (bais , publicKey , transformationName );
56+ String provider = "SUN" ;
57+
58+ try (InputStream bais = new ByteArrayInputStream (data .getBytes (UTF_8 ))) {
59+ return encrypt (bais , base64PublicKey , transformationName , provider );
6260 }
6361 }
6462
65- public static String encrypt ( InputStream input , String privateKey , String transformationName )
63+ public static String encryptString ( String data , String base64PublicKey , String transformationName , String provider )
6664 throws CryptoException , IOException {
65+ InputStream dataIs = new ByteArrayInputStream (data .getBytes (UTF_8 ));
6766 String algorithm = transformationName .split ("/" )[0 ];
68- String provider = "SUN" ;
67+ byte [] resultBytes = null ;
6968
7069 Cipher cipher ;
7170 try {
7271 cipher = Cipher .getInstance (transformationName );
73- } catch (NoSuchAlgorithmException | NoSuchPaddingException e ) {
74- throw new CryptoException (e );
75- }
72+ PublicKey publicKey = loadPublicKey (base64PublicKey , algorithm , provider );
73+ cipher .init (Cipher .ENCRYPT_MODE , publicKey );
7674
77- try {
78- PrivateKey privateKey1 = loadPrivateKey (privateKey , algorithm , provider );
79- cipher .init (Cipher .ENCRYPT_MODE , privateKey1 );
80- } catch (InvalidKeyException e ) {
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 ();
81+ } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException
82+ | InvalidKeyException e ) {
8183 throw new CryptoException (e );
8284 } catch (Exception e ) {
8385 e .printStackTrace ();
8486 }
8587
86- byte [] resultBytes ;
88+ return Base64 .getEncoder ().encodeToString (resultBytes );
89+ }
90+
91+ public static String encrypt (InputStream data , String base64PublicKey , String transformationName , String provider )
92+ throws CryptoException , IOException {
93+ String algorithm = transformationName .split ("/" )[0 ];
94+ byte [] resultBytes = null ;
95+
96+ Cipher cipher ;
8797 try {
98+ cipher = Cipher .getInstance (transformationName );
99+ PublicKey publicKey = loadPublicKey (base64PublicKey , algorithm , provider );
100+ cipher .init (Cipher .ENCRYPT_MODE , publicKey );
101+
88102 byte [] buf = new byte [Buffer .TRANSFER_SIZE ];
89103 int read = -1 ;
90- while ((read = input .read (buf )) > -1 ) {
104+ while ((read = data .read (buf )) > -1 ) {
91105 cipher .update (buf , 0 , read );
92106 }
93107 resultBytes = cipher .doFinal ();
94- } catch (IllegalBlockSizeException | BadPaddingException e ) {
108+ } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException
109+ | InvalidKeyException e ) {
95110 throw new CryptoException (e );
111+ } catch (Exception e ) {
112+ e .printStackTrace ();
96113 }
97114
98115 return getString (resultBytes );
99116 }
100117
101- public static String decryptString (String encryptedInput , String plainKey , String transformationName , String iv ,
102- @ Nullable String provider ) throws CryptoException , IOException {
118+ public static String decryptString (String encryptedInput , String base64PrivateKey , String transformationName )
119+ throws CryptoException , IOException {
120+ String provider = "SUN" ;
121+
103122 try (InputStream bais = new ByteArrayInputStream (getBytes (encryptedInput ))) {
104- return decrypt (bais , plainKey , transformationName , iv , provider );
123+ return decrypt (bais , base64PrivateKey , transformationName , provider );
105124 }
106125 }
107126
108- public static String decrypt (InputStream encryptedInput , String plainKey , String transformationName , String iv ,
109- @ Nullable String provider ) throws CryptoException , IOException {
110- String algorithm = transformationName .split ("/" )[0 ];
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+ }
111133
112- String actualProvider = Optional .ofNullable (provider ).filter (str -> !str .isEmpty ()).orElse ("SunJCE" );
134+ public static String decrypt (InputStream data , String base64PrivateKey , String transformationName , String provider )
135+ throws CryptoException , IOException {
136+ String algorithm = transformationName .split ("/" )[0 ];
137+ byte [] resultBytes = null ;
113138
114139 Cipher cipher ;
115140 try {
116- cipher = Cipher .getInstance (transformationName , actualProvider );
117- } catch (NoSuchProviderException | NoSuchAlgorithmException | NoSuchPaddingException e ) {
118- throw new CryptoException (e );
119- }
120-
121- SecretKeySpec skeySpec = new SecretKeySpec (plainKey .getBytes (UTF_8 ), algorithm );
122- if (transformationName .contains ("/" )) {
123- IvParameterSpec ivSpec = new IvParameterSpec (iv .getBytes (UTF_8 ), 0 , 16 );
124- try {
125- cipher .init (Cipher .DECRYPT_MODE , skeySpec , ivSpec );
126- } catch (InvalidAlgorithmParameterException | InvalidKeyException e ) {
127- throw new CryptoException (e );
128- }
129- } else {
130- try {
131- cipher .init (Cipher .DECRYPT_MODE , skeySpec );
132- } catch (InvalidKeyException e ) {
133- throw new CryptoException (e );
134- }
135- }
141+ cipher = Cipher .getInstance (transformationName );
142+ PrivateKey publicKey = loadPrivateKey (base64PrivateKey , algorithm , provider );
143+ cipher .init (Cipher .DECRYPT_MODE , publicKey );
136144
137- try {
138145 byte [] buf = new byte [Buffer .TRANSFER_SIZE ];
139146 int read = -1 ;
140- while ((read = encryptedInput .read (buf )) > -1 ) {
147+ while ((read = data .read (buf )) > -1 ) {
141148 cipher .update (buf , 0 , read );
142149 }
143-
144- byte [] resultBytes = cipher .doFinal ();
145- return new String (resultBytes , UTF_8 );
146- } catch (IllegalBlockSizeException | BadPaddingException e ) {
150+ resultBytes = cipher .doFinal ();
151+ } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException
152+ | InvalidKeyException e ) {
147153 throw new CryptoException (e );
154+ } catch (Exception e ) {
155+ e .printStackTrace ();
148156 }
157+
158+ return getString (resultBytes );
149159 }
150160
151161 public static String getString (byte [] bytes ) {
@@ -171,30 +181,28 @@ public static byte[] getBytes(String str) throws IOException {
171181 }
172182 }
173183
174- private static PublicKey loadPublicKey (String keyString , String algorithm , String provider )
184+ private static PublicKey loadPublicKey (String base64PublicKey , String algorithm , String provider )
175185 throws NoSuchAlgorithmException , NoSuchProviderException , InvalidKeySpecException {
176- byte [] keyBytes = keyString .getBytes (UTF_8 );
177186 // provider = Optional.ofNullable(provider).filter(str ->
178187 // !str.isEmpty()).orElse("SunRsaSign");
179188 provider = "SunRsaSign" ;
180189
181- X509EncodedKeySpec spec = new X509EncodedKeySpec (keyBytes );
190+ X509EncodedKeySpec spec = new X509EncodedKeySpec (Base64 . getDecoder (). decode ( base64PublicKey . getBytes ( UTF_8 )) );
182191 KeyFactory kf = KeyFactory .getInstance (algorithm , provider );
183192
184193 return kf .generatePublic (spec );
185194 }
186195
187- private static PrivateKey loadPrivateKey (String keyString , String algorithm , String provider )
196+ private static PrivateKey loadPrivateKey (String base64PrivateKey , String algorithm , String provider )
188197 throws NoSuchAlgorithmException , NoSuchProviderException , InvalidKeySpecException {
189- byte [] keyBytes = keyString .getBytes (UTF_8 );
190198 // provider = Optional.ofNullable(provider).filter(str ->
191199 // !str.isEmpty()).orElse("SunRsaSign");
192200 provider = "SunRsaSign" ;
193201
194- PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec (keyBytes );
202+ PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec (Base64 . getDecoder (). decode ( base64PrivateKey . getBytes ( UTF_8 )) );
195203
196204 KeyFactory kf = KeyFactory .getInstance (algorithm , provider );
197205
198- return kf .generatePrivate (spec );
206+ return kf .generatePrivate (keySpec );
199207 }
200208}
0 commit comments