3434
3535import javax .crypto .Cipher ;
3636
37+ import lombok .experimental .UtilityClass ;
38+
39+ @ UtilityClass
3740public class RSAUtils {
38- public static final String KEY_ALGORITHM = "RSA" ;
39- public static final String SIGNATURE_ALGORITHM = "MD5withRSA" ;
40- private static final String PUBLIC_KEY = "RSAPublicKey" ;
41- private static final String PRIVATE_KEY = "RSAPrivateKey" ;
42- private static final int MAX_ENCRYPT_BLOCK = 117 ;
43- private static final int MAX_DECRYPT_BLOCK = 128 ;
44-
45- public RSAUtils () {
46- }
41+ public final String KEY_ALGORITHM = "RSA" ;
42+ public final String SIGNATURE_ALGORITHM = "MD5withRSA" ;
43+ private final String PUBLIC_KEY = "RSAPublicKey" ;
44+ private final String PRIVATE_KEY = "RSAPrivateKey" ;
45+ private final int MAX_ENCRYPT_BLOCK = 117 ;
46+ private final int MAX_DECRYPT_BLOCK = 128 ;
47+
4748
48- public static Map <String , Object > genKeyPair () throws Exception {
49+ public Map <String , Object > genKeyPair () throws Exception {
4950 KeyPairGenerator keyPairGen = KeyPairGenerator .getInstance ("RSA" );
5051 keyPairGen .initialize (1024 );
5152 KeyPair keyPair = keyPairGen .generateKeyPair ();
@@ -57,7 +58,7 @@ public static Map<String, Object> genKeyPair() throws Exception {
5758 return keyMap ;
5859 }
5960
60- public static String sign (byte [] data , String privateKey ) throws Exception {
61+ public String sign (byte [] data , String privateKey ) throws Exception {
6162 byte [] keyBytes = Base64Utils .decode (privateKey );
6263 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec (keyBytes );
6364 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
@@ -68,7 +69,7 @@ public static String sign(byte[] data, String privateKey) throws Exception {
6869 return Base64Utils .encode (signature .sign ());
6970 }
7071
71- public static boolean verify (byte [] data , String publicKey , String sign ) throws Exception {
72+ public boolean verify (byte [] data , String publicKey , String sign ) throws Exception {
7273 byte [] keyBytes = Base64Utils .decode (publicKey );
7374 X509EncodedKeySpec keySpec = new X509EncodedKeySpec (keyBytes );
7475 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
@@ -79,7 +80,7 @@ public static boolean verify(byte[] data, String publicKey, String sign) throws
7980 return signature .verify (Base64Utils .decode (sign ));
8081 }
8182
82- public static byte [] decryptByPrivateKey (byte [] encryptedData , String privateKey ) throws Exception {
83+ public byte [] decryptByPrivateKey (byte [] encryptedData , String privateKey ) throws Exception {
8384 byte [] keyBytes = Base64Utils .decode (privateKey );
8485 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec (keyBytes );
8586 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
@@ -107,7 +108,7 @@ public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey
107108 return decryptedData ;
108109 }
109110
110- public static byte [] decryptByPrivateKeyBlock (byte [] encryptedData , String privateKey ) throws Exception {
111+ public byte [] decryptByPrivateKeyBlock (byte [] encryptedData , String privateKey ) throws Exception {
111112 byte [] keyBytes = Base64Utils .decode (privateKey );
112113 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec (keyBytes );
113114 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
@@ -120,7 +121,7 @@ public static byte[] decryptByPrivateKeyBlock(byte[] encryptedData, String priva
120121 return cache ;
121122 }
122123
123- public static byte [] decryptByPublicKey (byte [] encryptedData , String publicKey ) throws Exception {
124+ public byte [] decryptByPublicKey (byte [] encryptedData , String publicKey ) throws Exception {
124125 byte [] keyBytes = Base64Utils .decode (publicKey );
125126 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (keyBytes );
126127 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
@@ -148,7 +149,7 @@ public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey)
148149 return decryptedData ;
149150 }
150151
151- public static byte [] decryptByPublicKeyBlock (byte [] encryptedData , String publicKey ) throws Exception {
152+ public byte [] decryptByPublicKeyBlock (byte [] encryptedData , String publicKey ) throws Exception {
152153 byte [] keyBytes = Base64Utils .decode (publicKey );
153154 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (keyBytes );
154155 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
@@ -161,7 +162,7 @@ public static byte[] decryptByPublicKeyBlock(byte[] encryptedData, String public
161162 return cache ;
162163 }
163164
164- public static byte [] encryptByPublicKey (byte [] data , String publicKey ) throws Exception {
165+ public byte [] encryptByPublicKey (byte [] data , String publicKey ) throws Exception {
165166 byte [] keyBytes = Base64Utils .decode (publicKey );
166167 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (keyBytes );
167168 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
@@ -189,7 +190,7 @@ public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Ex
189190 return encryptedData ;
190191 }
191192
192- public static byte [] encryptByPublicKeyBlock (byte [] data , String publicKey ) throws Exception {
193+ public byte [] encryptByPublicKeyBlock (byte [] data , String publicKey ) throws Exception {
193194 byte [] keyBytes = Base64Utils .decode (publicKey );
194195 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (keyBytes );
195196 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
@@ -202,7 +203,7 @@ public static byte[] encryptByPublicKeyBlock(byte[] data, String publicKey) thro
202203 return cache ;
203204 }
204205
205- public static byte [] encryptByPrivateKey (byte [] data , String privateKey ) throws Exception {
206+ public byte [] encryptByPrivateKey (byte [] data , String privateKey ) throws Exception {
206207 byte [] keyBytes = Base64Utils .decode (privateKey );
207208 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec (keyBytes );
208209 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
@@ -230,7 +231,7 @@ public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws
230231 return encryptedData ;
231232 }
232233
233- public static byte [] encryptByPrivateKeyBlock (byte [] data , String privateKey ) throws Exception {
234+ public byte [] encryptByPrivateKeyBlock (byte [] data , String privateKey ) throws Exception {
234235 byte [] keyBytes = Base64Utils .decode (privateKey );
235236 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec (keyBytes );
236237 KeyFactory keyFactory = KeyFactory .getInstance ("RSA" );
@@ -243,12 +244,12 @@ public static byte[] encryptByPrivateKeyBlock(byte[] data, String privateKey) th
243244 return cache ;
244245 }
245246
246- public static String getPrivateKey (Map <String , Object > keyMap ) throws Exception {
247+ public String getPrivateKey (Map <String , Object > keyMap ) throws Exception {
247248 Key key = (Key ) keyMap .get ("RSAPrivateKey" );
248249 return Base64Utils .encode (key .getEncoded ());
249250 }
250251
251- public static String getPublicKey (Map <String , Object > keyMap ) throws Exception {
252+ public String getPublicKey (Map <String , Object > keyMap ) throws Exception {
252253 Key key = (Key ) keyMap .get ("RSAPublicKey" );
253254 return Base64Utils .encode (key .getEncoded ());
254255 }
0 commit comments