Hutool工具之RSA非对称加解密

本文介绍了如何使用RSA算法创建密钥对,并展示了公钥加密和私钥解密的实际应用案例。通过实例演示了如何在Java中操作RSA工具,包括获取公钥和私钥,以及加密和解密字符串的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 生成密钥对

    @Test
    void createRsa() {
        // 初始化RSA工具,生成密钥对
        RSA rsa = new RSA();
        // 获取公钥
        String pubKey = rsa.getPublicKeyBase64();
        System.out.println("pubKey:"+pubKey);
        // 获取私钥
        String priKey = rsa.getPrivateKeyBase64();
        System.out.println("priKey:"+priKey);
    }

加解密用例

一般使用时,一个方法只会用到一个密钥,公钥加密私钥解密,或私钥加密公钥解密。


    @Test
    void rsa() {
        String data = "这是一个小例子haha";
        String pubKey = "xxxxx";
        String priKey = "xxxxx";
       
        // 初始化RSA工具并设置私钥
        RSA priRsa = new RSA(priKey, null);
        // 初始化RSA工具并设置公钥
        RSA pubRsa = new RSA(null, pubKey);
        // 初始化自定义密钥对RSA工具,但是一般不这么用,因为公私钥需要分离
        RSA Rsa = new RSA(priKey, pubKey);

        // 公钥加密
        String encodeStr = pubRsa.encryptBase64(data, KeyType.PublicKey);
        System.out.println("encodeStr by public key:" + encodeStr);
        // 私钥解密
        String decodeStr = priRsa.decryptStr(encodeStr, KeyType.PrivateKey);
        System.out.println("decodeStr by private key:" + decodeStr);
        
        // 私钥加密
        String encodeStr1 = priRsa.encryptBase64(data, KeyType.PrivateKey);
        System.out.println("encodeStr1 by private key:" + encodeStr1);
        // 公钥解密
        String decodeStr1 = pubRsa.decryptStr(encodeStr1, KeyType.PublicKey);
        System.out.println("decodeStr1 by public key:" + decodeStr1);
    }


Hutool是一个Java工具包,提供了RSA非对称加密算法的实现。使用Hutool进行RSA加密,需要进行以下步骤: 1. 生成RSA密钥对 使用`SecureUtil`类的`generateKeyPair`方法,可以生成RSA密钥对。 ```java // 生成RSA密钥对 KeyPair keyPair = SecureUtil.generateKeyPair("RSA"); PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); ``` 2. 使用公钥进行加密 使用公钥对需要加密的数据进行加密。使用`RSACipher`类的`encrypt`方法进行加密。 ```java // 使用公钥加密数据 byte[] data = "Hello, RSA!".getBytes(); byte[] encrypted = RSACipher.encrypt(publicKey, data, KeyType.PublicKey); ``` 3. 使用私钥进行解密 使用私钥对加密后的数据进行解密。使用`RSACipher`类的`decrypt`方法进行解密。 ```java // 使用私钥解密数据 byte[] decrypted = RSACipher.decrypt(privateKey, encrypted, KeyType.PrivateKey); String result = new String(decrypted); System.out.println(result); ``` 完整的加密解密示例代码如下: ```java import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.asymmetric.RSACipher; import cn.hutool.crypto.asymmetric.RSAUtil; import cn.hutool.crypto.symmetric.SymmetricAlgorithm; import cn.hutool.crypto.symmetric.SymmetricCrypto; import cn.hutool.crypto.symmetric.SymmetricCrypto.Mode; import cn.hutool.crypto.symmetric.SymmetricCrypto.Padding; import java.security.KeyPair; import java.security.PrivateKey; import java.security.PublicKey; public class RSATest { public static void main(String[] args) { // 生成RSA密钥对 KeyPair keyPair = RSAUtil.generateKeyPair(); PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // 使用公钥加密数据 byte[] data = "Hello, RSA!".getBytes(); byte[] encrypted = RSACipher.encrypt(publicKey, data, KeyType.PublicKey); // 使用私钥解密数据 byte[] decrypted = RSACipher.decrypt(privateKey, encrypted, KeyType.PrivateKey); String result = new String(decrypted); System.out.println(result); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lizz666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值