tx

wangcx

栖迟於一丘,则天下不易其乐
79,429
HuTool 实现 RSA 加密 HuTool 实现 RSA 加密
  public static void main(String[] args) {
        // 待加密的原始数据
        String plainText = "Hello, RSA!";

        // 初始化 RSA 加密解密工具
        AsymmetricCrypto rsa = new AsymmetricCrypto("RSA");

        // 获取公钥和私钥(生成一对)
        String publicKey = rsa.getPublicKeyBase64();
        String privateKey = rsa.getPrivateKeyBase64();

        // 输出公钥和私钥
        System.out.println("Public Key: " + publicKey);
        System.out.println("Private Key: " + privateKey);


        // 使用公钥加密数据
        byte[] encrypted = rsa.encrypt(plainText.getBytes(),KeyType.PublicKey);

        // 将加密后的数据转换成 Base64 编码的字符串
        String encryptedBase64 = Base64.encode(encrypted);
        System.out.println("Encrypted Text: " + encryptedBase64);

        // 使用私钥解密数据
        byte[] decrypted = rsa.decrypt(Base64.decode(encryptedBase64),KeyType.PrivateKey);

        // 将解密后的数据转换成字符串
        String decryptedText = new String(decrypted);
        System.out.println("Decrypted Text: " + decryptedText);

    }
0 条评论
user 编辑评论信息
插入图片

隐私评论

随机一言

:D 获取中...

文章目录

最新评论

随机文章

truncate 命令用法 1895 浏览 - 2024/06/12
MySQL 唯一键约束 1212 浏览 - 2022/04/22
macOS JDK 多版本配置及切换 1220 浏览 - 2019/10/11
对 List 进行手动分页 1152 浏览 - 2022/11/09