11package com .fengwenyi .javalib .encryption ;
22
3- import com .fengwenyi .javalib .constant .CharsetConstant ;
4- import com .fengwenyi .javalib .third .Base64 ;
53import com .fengwenyi .javalib .convert .HexUtils ;
64import com .fengwenyi .javalib .util .StringUtils ;
75
108import javax .crypto .IllegalBlockSizeException ;
119import javax .crypto .NoSuchPaddingException ;
1210import java .io .UnsupportedEncodingException ;
11+ import java .nio .charset .StandardCharsets ;
1312import java .security .*;
1413import java .security .interfaces .RSAPrivateKey ;
1514import java .security .interfaces .RSAPublicKey ;
@@ -79,9 +78,9 @@ public static String privateKeyEncrypt(String key, String plainText) throws NoSu
7978 PrivateKey privateKey = commonGetPrivatekeyByText (key );
8079 Cipher cipher = Cipher .getInstance (RSA );
8180 cipher .init (Cipher .ENCRYPT_MODE , privateKey );
82- byte [] result = cipher .doFinal (plainText .getBytes (CharsetConstant .UTF_8 ));
81+ byte [] result = cipher .doFinal (plainText .getBytes (StandardCharsets .UTF_8 ));
8382
84- return Base64 . byteArrayToBase64 (result );
83+ return Base64Utils . encrypt (result );
8584 }
8685
8786 /**
@@ -102,7 +101,7 @@ public static String publicKeyDecrypt(String key, String cipherText) throws NoSu
102101 PublicKey publicKey = commonGetPublickeyByText (key );
103102 Cipher cipher = Cipher .getInstance (RSA );
104103 cipher .init (Cipher .DECRYPT_MODE , publicKey );
105- byte [] result = cipher .doFinal (Base64 . base64ToByteArray (cipherText ));
104+ byte [] result = cipher .doFinal (Base64Utils . decode (cipherText ));
106105
107106 return new String (result );
108107 }
@@ -127,9 +126,9 @@ public static String publicKeyEncrypt(String key, String plainText) throws NoSuc
127126 Cipher cipher = Cipher .getInstance (RSA );
128127 cipher .init (Cipher .ENCRYPT_MODE , publicKey );
129128
130- byte [] result = cipher .doFinal (plainText .getBytes (CharsetConstant .UTF_8 ));
129+ byte [] result = cipher .doFinal (plainText .getBytes (StandardCharsets .UTF_8 ));
131130
132- return Base64 . byteArrayToBase64 (result );
131+ return Base64Utils . encrypt (result );
133132 }
134133
135134 /**
@@ -149,7 +148,7 @@ public static String privateKeyDecrypt(String key, String cipherText) throws NoS
149148 PrivateKey privateKey = commonGetPrivatekeyByText (key );
150149 Cipher cipher = Cipher .getInstance (RSA );
151150 cipher .init (Cipher .DECRYPT_MODE , privateKey );
152- byte [] result = cipher .doFinal (Base64 . base64ToByteArray (cipherText ));
151+ byte [] result = cipher .doFinal (Base64Utils . decode (cipherText ));
153152
154153 return new String (result );
155154 }
@@ -165,17 +164,17 @@ private static String[] commonKey(int size) throws NoSuchAlgorithmException {
165164 RSAPublicKey rsaPublicKey = (RSAPublicKey ) keyPair .getPublic ();
166165
167166 // 私钥
168- keys [0 ] = Base64 . byteArrayToBase64 (rsaPrivateKey .getEncoded ());
167+ keys [0 ] = Base64Utils . encrypt (rsaPrivateKey .getEncoded ());
169168 // 公钥
170- keys [1 ] = Base64 . byteArrayToBase64 (rsaPublicKey .getEncoded ());
169+ keys [1 ] = Base64Utils . encrypt (rsaPublicKey .getEncoded ());
171170
172171 return keys ;
173172 }
174173
175174 // (common)通过公钥文本获取公钥
176175 private static PublicKey commonGetPublickeyByText (String keyText )
177176 throws NoSuchAlgorithmException , InvalidKeySpecException {
178- byte [] keyBytes = Base64 . base64ToByteArray (keyText );
177+ byte [] keyBytes = Base64Utils . decode (keyText );
179178 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (keyBytes );
180179 KeyFactory keyFactory = KeyFactory .getInstance (RSA );
181180 return keyFactory .generatePublic (x509KeySpec );
@@ -184,7 +183,7 @@ private static PublicKey commonGetPublickeyByText(String keyText)
184183 // (common)通过私钥文本获取私钥
185184 private static PrivateKey commonGetPrivatekeyByText (String keyText )
186185 throws NoSuchAlgorithmException , InvalidKeySpecException {
187- byte [] keyBytes = Base64 . base64ToByteArray (keyText );
186+ byte [] keyBytes = Base64Utils . decode (keyText );
188187 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec (keyBytes );
189188 KeyFactory keyFactory = KeyFactory .getInstance (RSA );
190189 return keyFactory .generatePrivate (pkcs8EncodedKeySpec );
0 commit comments