mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-07 22:34:04 +08:00
1.2.3 RSA 加密(签名)修复
This commit is contained in:
parent
3ba0e22e65
commit
047a7e24b6
12
UPDATE.md
12
UPDATE.md
@ -1,6 +1,18 @@
|
||||
yexuejc-base 更新记录
|
||||
------------------
|
||||
|
||||
#### version :1.2.3
|
||||
**time:2018-11-23 16:45:42** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1. 修复RSA加密(签名)时选择的Base64(encodeBase64URLSafeString、encodeBase64String)区分
|
||||
#
|
||||
#### version :1.2.1
|
||||
**time:2018-11-9 15:05:06** <br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1. 优化resps
|
||||
#
|
||||
#### version :1.2.2
|
||||
**time:2018-11-20 20:20:12** <br/>
|
||||
**branch:** master <br/>
|
||||
|
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.yexuejc.base</groupId>
|
||||
<artifactId>yexuejc-base</artifactId>
|
||||
<version>1.2.2</version>
|
||||
<version>1.2.3</version>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
|
||||
|
@ -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 方式加密 默认正常加密
|
||||
* <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;
|
||||
|
||||
|
||||
/**
|
||||
* 生成密钥对
|
||||
*
|
||||
* @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) {
|
||||
//为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<String, String> keyPairMap = new HashMap<String, String>();
|
||||
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
|
||||
* <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
|
||||
* <p>
|
||||
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user