hutool工具的rsa公钥加密和私钥解密
时间: 2023-08-09 14:05:21 浏览: 478
好的,我来为您讲解一下hutool工具的rsa公钥加密和私钥解密的方法。
首先,您需要导入hutool工具包,并且生成RSA公私钥对,可以使用下面的代码:
```java
// 生成RSA公私钥对
KeyPair keyPair = SecureUtil.generateKeyPair("RSA");
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// 获取公私钥的Base64编码
String publicKeyStr = Base64.encode(publicKey.getEncoded());
String privateKeyStr = Base64.encode(privateKey.getEncoded());
```
接下来,使用公钥进行加密,可以使用下面的代码:
```java
// 使用公钥进行加密
String plainText = "Hello, world!";
byte[] encryptedBytes = SecureUtil.rsa(publicKey.getModulus().toString(16), publicKey.getPublicExponent().toString(16)).encrypt(plainText.getBytes(), KeyType.PublicKey);
String encryptedStr = Base64.encode(encryptedBytes);
```
最后,使用私钥进行解密,可以使用下面的代码:
```java
// 使用私钥进行解密
byte[] decryptedBytes = SecureUtil.rsa(privateKey.getModulus().toString(16), privateKey.getPrivateExponent().toString(16)).decrypt(Base64.decode(encryptedStr), KeyType.PrivateKey);
String decryptedStr = new String(decryptedBytes);
```
以上就是使用hutool工具进行RSA公钥加密和私钥解密的方法,希望能对您有所帮助。
阅读全文
相关推荐


















