From 047a7e24b68a0b93398d696fdbd85be59991bb8d Mon Sep 17 00:00:00 2001 From: maxf <1107047387@qq.com> Date: Fri, 23 Nov 2018 16:46:48 +0800 Subject: [PATCH] =?UTF-8?q?1.2.3=20RSA=20=E5=8A=A0=E5=AF=86=EF=BC=88?= =?UTF-8?q?=E7=AD=BE=E5=90=8D=EF=BC=89=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UPDATE.md | 12 ++ pom.xml | 2 +- .../java/com/yexuejc/base/encrypt/RSA.java | 125 ++++++++++++++++-- 3 files changed, 127 insertions(+), 12 deletions(-) diff --git a/UPDATE.md b/UPDATE.md index fbc09a5..dd5e827 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -1,6 +1,18 @@ yexuejc-base 更新记录 ------------------ +#### version :1.2.3 +**time:2018-11-23 16:45:42**
+**branch:** master
+**update:**
+>1. 修复RSA加密(签名)时选择的Base64(encodeBase64URLSafeString、encodeBase64String)区分 +# +#### version :1.2.1 +**time:2018-11-9 15:05:06**
+**branch:** master
+**update:**
+>1. 优化resps +# #### version :1.2.2 **time:2018-11-20 20:20:12**
**branch:** master
diff --git a/pom.xml b/pom.xml index 9115823..122678c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.yexuejc.base yexuejc-base - 1.2.2 + 1.2.3 ${project.artifactId} diff --git a/src/main/java/com/yexuejc/base/encrypt/RSA.java b/src/main/java/com/yexuejc/base/encrypt/RSA.java index 968d0fa..8df2e9a 100644 --- a/src/main/java/com/yexuejc/base/encrypt/RSA.java +++ b/src/main/java/com/yexuejc/base/encrypt/RSA.java @@ -18,6 +18,8 @@ import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map; +//import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; + /** * RSA加解密 配置模式 * @@ -44,12 +46,51 @@ public class RSA { * 只针对于RSA_ALGORITHM_ECB = "RSA"有效 */ public static boolean isChangeSign = true; + /** + * 是否使用 Base64URL 方式加密 默认正常加密 + *
+     *     关于 Base64URL 和正常加密的区别:Base64URL会把 '+', '/' 转换成 '-', '_' 来防止请求时url上的转义
+     *     private static final byte[] STANDARD_ENCODE_TABLE = {
+     *             'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+     *             'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+     *             'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+     *             'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+     *             '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
+     *     };
+     *      private static final byte[] URL_SAFE_ENCODE_TABLE = {
+     *             'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+     *             'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+     *             'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+     *             'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+     *             '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
+     *     };
+     * 
+ */ + public static boolean encodeBase64URLSafe = false; /** * 签名算法 */ public static SignAlgorithm signAlgorithm = SignAlgorithm.SHA1withRSA; + /** + * 生成密钥对 + * + * @param keySize 生成长度 + * @param base64URLSafe 是否生成 base64URL 格式的密钥:默认false + * @return + */ + public static Map initKeys(int keySize, boolean base64URLSafe) { + encodeBase64URLSafe = base64URLSafe; + return initKeys(keySize); + } + + /** + * 生成密钥对 + * + * @param keySize 生成长度 + * @return + */ public static Map initKeys(int keySize) { //为RSA算法创建一个KeyPairGenerator对象 KeyPairGenerator kpg; @@ -65,10 +106,17 @@ public class RSA { KeyPair keyPair = kpg.generateKeyPair(); //得到公钥 Key publicKey = keyPair.getPublic(); - String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded()); //得到私钥 Key privateKey = keyPair.getPrivate(); - String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded()); + String privateKeyStr = null; + String publicKeyStr = null; + if (encodeBase64URLSafe) { + publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded()); + privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded()); + } else { + publicKeyStr = Base64.encodeBase64String(publicKey.getEncoded()); + privateKeyStr = Base64.encodeBase64String(privateKey.getEncoded()); + } Map keyPairMap = new HashMap(); keyPairMap.put("publicKey", publicKeyStr); keyPairMap.put("privateKey", privateKeyStr); @@ -83,7 +131,7 @@ public class RSA { * @param publicKey 密钥字符串(经过base64编码) * @throws Exception */ - public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException{ + public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException { //通过X509编码的Key指令获得公钥对象 KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey)); @@ -108,15 +156,32 @@ public class RSA { /** * 公钥加密 * - * @param data - * @param publicKey + * @param data 加密原串数据 + * @param publicKey 公钥 + * @param base64URLSafe 是否生成 base64URL 格式的密钥:默认false + * @return + */ + public static String publicEncrypt(String data, RSAPublicKey publicKey, boolean base64URLSafe) { + encodeBase64URLSafe = base64URLSafe; + return publicEncrypt(data, publicKey); + } + + /** + * 公钥加密 + * + * @param data 加密原串数据 + * @param publicKey 公钥 * @return */ public static String publicEncrypt(String data, RSAPublicKey publicKey) { try { Cipher cipher = getCipher(); cipher.init(Cipher.ENCRYPT_MODE, publicKey); - return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength())); + if (encodeBase64URLSafe) { + return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength())); + } else { + return Base64.encodeBase64String(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength())); + } } catch (Exception e) { throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e); } @@ -143,16 +208,32 @@ public class RSA { /** * 私钥加密 * - * @param data - * @param privateKey + * @param data 加密原串数据 + * @param privateKey 公钥 + * @param base64URLSafe 是否生成 base64URL 格式的密钥:默认false * @return */ + public static String privateEncrypt(String data, RSAPrivateKey privateKey, boolean base64URLSafe) { + encodeBase64URLSafe = base64URLSafe; + return privateEncrypt(data, privateKey); + } + /** + * 私钥加密 + * + * @param data 加密原串数据 + * @param privateKey 公钥 + * @return + */ public static String privateEncrypt(String data, RSAPrivateKey privateKey) { try { Cipher cipher = getCipher(); cipher.init(Cipher.ENCRYPT_MODE, privateKey); - return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), privateKey.getModulus().bitLength())); + if (encodeBase64URLSafe) { + return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), privateKey.getModulus().bitLength())); + } else { + return Base64.encodeBase64String(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), privateKey.getModulus().bitLength())); + } } catch (Exception e) { throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e); } @@ -227,6 +308,25 @@ public class RSA { private static Signature signature; + + /** + * /** + * 私钥签名:默认算法SHA1withRSA + *

+ * 签名算法 {@link SignAlgorithm} + *

+ * + * @param plaintext 签名字符串 + * @param privateKey 签名私钥 + * @param base64URLSafe 是否生成 base64URL 格式的密钥:默认false + * @return + * @throws NoSuchAlgorithmException + */ + public static String sign(String plaintext, RSAPrivateKey privateKey, boolean base64URLSafe) throws NoSuchAlgorithmException { + encodeBase64URLSafe = base64URLSafe; + return sign(plaintext, privateKey); + } + /** * 私钥签名:默认算法SHA1withRSA *

@@ -250,8 +350,11 @@ public class RSA { e.printStackTrace(); throw new RuntimeException("签名字符串[" + plaintext + "]的数据时发生异常", e); } - - signBase64Str = Base64.encodeBase64String(signature.sign()); + if (encodeBase64URLSafe) { + signBase64Str = Base64.encodeBase64URLSafeString(signature.sign()); + } else { + signBase64Str = Base64.encodeBase64String(signature.sign()); + } return signBase64Str; } catch (InvalidKeyException var6) { var6.printStackTrace();