rsautil java,java非对称加密解密 - RSAUtil

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;

}

}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值