1.2.3 RSA 加密(签名)修复

This commit is contained in:
maxf 2018-11-23 16:46:48 +08:00
parent 3ba0e22e65
commit 047a7e24b6
3 changed files with 127 additions and 12 deletions

View File

@ -1,6 +1,18 @@
yexuejc-base 更新记录 yexuejc-base 更新记录
------------------ ------------------
#### version 1.2.3
**time2018-11-23 16:45:42** <br/>
**branch** master <br/>
**update** <br/>
>1. 修复RSA加密签名时选择的Base64encodeBase64URLSafeString、encodeBase64String区分
#
#### version 1.2.1
**time2018-11-9 15:05:06** <br/>
**branch** master <br/>
**update** <br/>
>1. 优化resps
#
#### version 1.2.2 #### version 1.2.2
**time2018-11-20 20:20:12** <br/> **time2018-11-20 20:20:12** <br/>
**branch** master <br/> **branch** master <br/>

View File

@ -6,7 +6,7 @@
<groupId>com.yexuejc.base</groupId> <groupId>com.yexuejc.base</groupId>
<artifactId>yexuejc-base</artifactId> <artifactId>yexuejc-base</artifactId>
<version>1.2.2</version> <version>1.2.3</version>
<name>${project.artifactId}</name> <name>${project.artifactId}</name>

View File

@ -18,6 +18,8 @@ import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
//import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
/** /**
* RSA加解密 配置模式 * RSA加解密 配置模式
* *
@ -44,12 +46,51 @@ public class RSA {
* 只针对于RSA_ALGORITHM_ECB = "RSA"有效 * 只针对于RSA_ALGORITHM_ECB = "RSA"有效
*/ */
public static boolean isChangeSign = true; public static boolean isChangeSign = true;
/**
* 是否使用 Base64URL 方式加密 默认正常加密
* <pre>
* 关于 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', '-', '_'
* };
* </pre>
*/
public static boolean encodeBase64URLSafe = false;
/** /**
* 签名算法 * 签名算法
*/ */
public static SignAlgorithm signAlgorithm = SignAlgorithm.SHA1withRSA; public static SignAlgorithm signAlgorithm = SignAlgorithm.SHA1withRSA;
/**
* 生成密钥对
*
* @param keySize 生成长度
* @param base64URLSafe 是否生成 base64URL 格式的密钥默认false
* @return
*/
public static Map<String, String> initKeys(int keySize, boolean base64URLSafe) {
encodeBase64URLSafe = base64URLSafe;
return initKeys(keySize);
}
/**
* 生成密钥对
*
* @param keySize 生成长度
* @return
*/
public static Map<String, String> initKeys(int keySize) { public static Map<String, String> initKeys(int keySize) {
//为RSA算法创建一个KeyPairGenerator对象 //为RSA算法创建一个KeyPairGenerator对象
KeyPairGenerator kpg; KeyPairGenerator kpg;
@ -65,10 +106,17 @@ public class RSA {
KeyPair keyPair = kpg.generateKeyPair(); KeyPair keyPair = kpg.generateKeyPair();
//得到公钥 //得到公钥
Key publicKey = keyPair.getPublic(); Key publicKey = keyPair.getPublic();
String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
//得到私钥 //得到私钥
Key privateKey = keyPair.getPrivate(); 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<String, String> keyPairMap = new HashMap<String, String>(); Map<String, String> keyPairMap = new HashMap<String, String>();
keyPairMap.put("publicKey", publicKeyStr); keyPairMap.put("publicKey", publicKeyStr);
keyPairMap.put("privateKey", privateKeyStr); keyPairMap.put("privateKey", privateKeyStr);
@ -108,15 +156,32 @@ public class RSA {
/** /**
* 公钥加密 * 公钥加密
* *
* @param data * @param data 加密原串数据
* @param publicKey * @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 * @return
*/ */
public static String publicEncrypt(String data, RSAPublicKey publicKey) { public static String publicEncrypt(String data, RSAPublicKey publicKey) {
try { try {
Cipher cipher = getCipher(); Cipher cipher = getCipher();
cipher.init(Cipher.ENCRYPT_MODE, publicKey); cipher.init(Cipher.ENCRYPT_MODE, publicKey);
if (encodeBase64URLSafe) {
return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength())); 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) { } catch (Exception e) {
throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e); throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
} }
@ -143,16 +208,32 @@ public class RSA {
/** /**
* 私钥加密 * 私钥加密
* *
* @param data * @param data 加密原串数据
* @param privateKey * @param privateKey 公钥
* @param base64URLSafe 是否生成 base64URL 格式的密钥默认false
* @return * @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) { public static String privateEncrypt(String data, RSAPrivateKey privateKey) {
try { try {
Cipher cipher = getCipher(); Cipher cipher = getCipher();
cipher.init(Cipher.ENCRYPT_MODE, privateKey); cipher.init(Cipher.ENCRYPT_MODE, privateKey);
if (encodeBase64URLSafe) {
return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), privateKey.getModulus().bitLength())); 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) { } catch (Exception e) {
throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e); throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
} }
@ -227,6 +308,25 @@ public class RSA {
private static Signature signature; private static Signature signature;
/**
* /**
* 私钥签名默认算法SHA1withRSA
* <p>
* 签名算法 {@link SignAlgorithm}
* </p>
*
* @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 * 私钥签名默认算法SHA1withRSA
* <p> * <p>
@ -250,8 +350,11 @@ public class RSA {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException("签名字符串[" + plaintext + "]的数据时发生异常", e); throw new RuntimeException("签名字符串[" + plaintext + "]的数据时发生异常", e);
} }
if (encodeBase64URLSafe) {
signBase64Str = Base64.encodeBase64URLSafeString(signature.sign());
} else {
signBase64Str = Base64.encodeBase64String(signature.sign()); signBase64Str = Base64.encodeBase64String(signature.sign());
}
return signBase64Str; return signBase64Str;
} catch (InvalidKeyException var6) { } catch (InvalidKeyException var6) {
var6.printStackTrace(); var6.printStackTrace();