getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM, DEFAULT_PROVIDER);
keyPairGenerator.initialize(KEY_SIZE);
KeyPair keyPair = keyPairGenerator.genKeyPair();
MapkeyPairBase64Map = new HashMap<>();
keyPairBase64Map.put("privateKey", Base64Util.encode(keyPair.getPrivate().getEncoded()));
keyPairBase64Map.put("publicKey", Base64Util.encode(keyPair.getPublic().getEncoded()));
return keyPairBase64Map;
}
/**
* 获取私钥
*
* @param base64PrivateKey
*
* @return
*/
public static RSAPrivateKey getPrivateKey(String base64PrivateKey) {
if (base64PrivateKey == null) return null;
try {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64Util.decode(base64PrivateKey));
RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8EncodedKeySpec);
return privateKey;
} catch (Exception e) {
LOG.error(ExceptionUtil.getAllExceptionMessages(e));
}
return null;
}
/**
* 获取公钥
*
* @param base64PublicKey
*
* @return
*/
public static RSAPublicKey getPublicKey(String base64PublicKey) {
if (base64PublicKey == null) return null;
try {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64Util.decode(base64PublicKey));
RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(x509EncodedKeySpec);
return publicKey;
} catch (Exception e) {
LOG.error(ExceptionUtil.getAllExceptionMessages(e));
}
return null;
}
/**
* 公钥加密
*
* @param publicKey
* @param text
*
* @return
*/
public static String encrypt(RSAPublicKey publicKey, String text) {
if (publicKey == null || text == null) return null;
ByteArrayOutputStream out = null;
try {
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] data = text.getBytes();
int inputLen = data.length;
out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
int maxEncryptBlock = getMaxEncryptBlock(publicKey);
// 分段加密
while ((inputLen - offSet) > 0) {
if ((inputLen - offSet) > maxEncryptBlock) {
cache = cipher.doFinal(data, offSet, maxEncryptBlock);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * maxEncryptBlock;
}
byte[] encryptedData = out.toByteArray();
return Base64Util.encode(encryptedData);
} catch (Exception e) {
LOG.error(ExceptionUtil.getAllExceptionMessages(e));
} finally {
try {
if (out != null) out.close();
} catch (IOException e) {
LOG.error(ExceptionUtil.getAllExceptionMessages(e));
}
}
return null;
}
/**
* 私钥解密
*
* @param privateKey
* @param encryptBase64Text
*
* @return
*/
public static String decrypt(RSAPrivateKey privateKey, String encryptBase64Text) {
if (privateKey == null || encryptBase64Text == null) return null;
ByteArrayOutputStream out = null;
try {
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] encryptedData = Base64Util.decode(encryptBase64Text);
int inputLen = encryptedData.length;
out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
int maxDecryptBlock = getMaxDecryptBlock(privateKey);
// 分段解密
while (inputLen - offSet > 0) {
if (inputLen - offSet > maxDecryptBlock) {
cache = cipher.doFinal(encryptedData, offSet, maxDecryptBlock);
} else {
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * maxDecryptBlock;
}
byte[] decryptedData = out.toByteArray();
return new String(decryptedData);
} catch (Exception e) {
LOG.error(ExceptionUtil.getAllExceptionMessages(e));
} finally {
try {
if (out != null) out.close();
} catch (IOException e) {
LOG.error(ExceptionUtil.getAllExceptionMessages(e));
}
}
return null;
}
/**
* RSA最大加密明文大小,公钥modules长度(若不是外来key,那么就是KEY_SIZE) / 8 - 11
*
* @param publicKey
*
* @return
*/
private static int getMaxEncryptBlock(RSAPublicKey publicKey) {
if (publicKey == null) return 0;
return (publicKey.getModulus().bitLength() >> 3) - 11;
}
/**
* RSA最大解密密文大小,私钥modules长度(若不是外来key,那么就是KEY_SIZE) / 8
*
* @param privateKey
*
* @return
*/
private static int getMaxDecryptBlock(RSAPrivateKey privateKey) {
if (privateKey == null) return 0;
return privateKey.getModulus().bitLength() >> 3;
}
}
```