33
44import javax .crypto .Cipher ;
55import java .io .*;
6+ import java .security .GeneralSecurityException ;
67import java .security .KeyFactory ;
78import java .security .PrivateKey ;
89import java .security .PublicKey ;
@@ -34,9 +35,9 @@ public class RSA{
3435 */
3536 public static String sign (String content , String privateKey , String signAlgorithms , String characterEncoding ) {
3637 try {
37- PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec ( Base64 .decode (privateKey ));
38- KeyFactory keyf = KeyFactory .getInstance (ALGORITHM );
39- PrivateKey priKey = keyf .generatePrivate (priPKCS8 );
38+ PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec (Base64 .decode (privateKey ));
39+ KeyFactory keyf = KeyFactory .getInstance (ALGORITHM );
40+ PrivateKey priKey = keyf .generatePrivate (priPKCS8 );
4041
4142 java .security .Signature signature = java .security .Signature .getInstance (signAlgorithms );
4243
@@ -111,11 +112,11 @@ public static String sign(String content, PrivateKey privateKey ,String characte
111112 */
112113 public static boolean verify (String content , String sign , String publicKey , String signAlgorithms , String characterEncoding ){
113114 try {
114- PublicKey pubKey = getPublicKey (publicKey , ALGORITHM );
115+ PublicKey pubKey = getPublicKey (publicKey , ALGORITHM );
115116 java .security .Signature signature = java .security .Signature .getInstance (signAlgorithms );
116117 signature .initVerify (pubKey );
117- signature .update ( content .getBytes (characterEncoding ) );
118- return signature .verify ( Base64 .decode (sign ) );
118+ signature .update (content .getBytes (characterEncoding ) );
119+ return signature .verify (Base64 .decode (sign ) );
119120 } catch (Exception e ) {
120121 e .printStackTrace ();
121122 }
@@ -135,8 +136,8 @@ public static boolean verify(String content, String sign, PublicKey publicKey, S
135136 try {
136137 java .security .Signature signature = java .security .Signature .getInstance (signAlgorithms );
137138 signature .initVerify (publicKey );
138- signature .update ( content .getBytes (characterEncoding ) );
139- return signature .verify ( Base64 .decode (sign ) );
139+ signature .update (content .getBytes (characterEncoding ) );
140+ return signature .verify (Base64 .decode (sign ) );
140141 } catch (Exception e ) {
141142 e .printStackTrace ();
142143 }
@@ -176,9 +177,9 @@ public static boolean verify(String content, String sign, PublicKey publicKey, S
176177 * @return 解密后的字符串
177178 * @throws Exception 解密异常
178179 */
179- public static String decrypt (String content , String privateKey , String characterEncoding ) throws Exception {
180- PrivateKey prikey = getPrivateKey (privateKey );
181- Cipher cipher = Cipher .getInstance (ALGORITHM );
180+ public static String decrypt (String content , String privateKey , String characterEncoding ) throws GeneralSecurityException , IOException {
181+ PrivateKey prikey = getPrivateKey (privateKey );
182+ Cipher cipher = Cipher .getInstance (ALGORITHM );
182183 cipher .init (Cipher .DECRYPT_MODE , prikey );
183184 try (InputStream ins = new ByteArrayInputStream (Base64 .decode (content )); ByteArrayOutputStream writer = new ByteArrayOutputStream ();) {
184185
@@ -204,14 +205,14 @@ public static String decrypt(String content, String privateKey, String character
204205 }
205206 }
206207
207-
208+
208209 /**
209210 * 得到私钥
210211 * @param key 密钥字符串(经过base64编码)
211- * @throws Exception 加密异常
212+ * @throws GeneralSecurityException 加密异常
212213 * @return 私钥
213214 */
214- public static PrivateKey getPrivateKey (String key ) throws Exception {
215+ public static PrivateKey getPrivateKey (String key ) throws GeneralSecurityException {
215216
216217 byte [] keyBytes ;
217218 keyBytes = Base64 .decode (key );
@@ -225,26 +226,30 @@ public static PrivateKey getPrivateKey(String key) throws Exception {
225226 * 得到公钥
226227 * @param key 密钥字符串(经过base64编码)
227228 * @param signAlgorithms 密钥类型
228- * @throws Exception 加密异常
229+ * @throws GeneralSecurityException 加密异常
230+ * @throws IOException 加密异常
229231 * @return 公钥
230232 */
231- public static PublicKey getPublicKey (String key , String signAlgorithms ) throws Exception {
232- return getPublicKey (new ByteArrayInputStream (key .getBytes ("ISO8859-1" )), signAlgorithms );
233+ public static PublicKey getPublicKey (String key , String signAlgorithms ) throws GeneralSecurityException , IOException {
234+ try (ByteArrayInputStream is = new ByteArrayInputStream (key .getBytes ("ISO8859-1" ))){
235+ return getPublicKey (is , signAlgorithms );
236+ }
233237 }
234238
235239
236240 /**
237241 * 得到公钥
238242 * @param key 密钥字符串(经过base64编码)
239- * @throws Exception 加密异常
243+ * @throws GeneralSecurityException 加密异常
244+ * @throws IOException 加密异常
240245 * @return 公钥
241246 */
242- public static PublicKey getPublicKey (String key ) throws Exception {
247+ public static PublicKey getPublicKey (String key ) throws GeneralSecurityException , IOException {
243248
244249 return getPublicKey (key , ALGORITHM );
245250 }
246251
247- public static PublicKey getPublicKey (InputStream inputStream , String keyAlgorithm ) throws Exception {
252+ public static PublicKey getPublicKey (InputStream inputStream , String keyAlgorithm ) throws IOException , GeneralSecurityException {
248253 try (BufferedReader br = new BufferedReader (new InputStreamReader (inputStream ));) {
249254 StringBuilder sb = new StringBuilder ();
250255 String readLine = null ;
@@ -262,14 +267,14 @@ public static PublicKey getPublicKey(InputStream inputStream, String keyAlgorith
262267 }
263268 }
264269
265- public static byte [] encrypt (byte [] plainBytes , PublicKey publicKey , int keyLength , int reserveSize , String cipherAlgorithm ) throws Exception {
270+ public static byte [] encrypt (byte [] plainBytes , PublicKey publicKey , int keyLength , int reserveSize , String cipherAlgorithm ) throws IOException , GeneralSecurityException {
266271 int keyByteSize = keyLength / 8 ;
267272 int encryptBlockSize = keyByteSize - reserveSize ;
268273 int nBlock = plainBytes .length / encryptBlockSize ;
269274 if ((plainBytes .length % encryptBlockSize ) != 0 ) {
270275 nBlock += 1 ;
271276 }
272- try (ByteArrayOutputStream outbuf = new ByteArrayOutputStream (nBlock * keyByteSize ); ) {
277+ try (ByteArrayOutputStream outbuf = new ByteArrayOutputStream (nBlock * keyByteSize )) {
273278 Cipher cipher = Cipher .getInstance (cipherAlgorithm );
274279 cipher .init (Cipher .ENCRYPT_MODE , publicKey );
275280 for (int offset = 0 ; offset < plainBytes .length ; offset += encryptBlockSize ) {
@@ -284,7 +289,7 @@ public static byte[] encrypt(byte[] plainBytes, PublicKey publicKey, int keyLeng
284289 return outbuf .toByteArray ();
285290 }
286291 }
287- public static String encrypt (String content , String publicKey , String cipherAlgorithm , String characterEncoding ) throws Exception {
292+ public static String encrypt (String content , String publicKey , String cipherAlgorithm , String characterEncoding ) throws IOException , GeneralSecurityException {
288293 return Base64 .encode (RSA .encrypt (content .getBytes (characterEncoding ), RSA .getPublicKey (publicKey ),1024 , 11 , cipherAlgorithm ));
289294 }
290295
0 commit comments