tx

wangcx

栖迟於一丘,则天下不易其乐
21,791
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);

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

隐私评论
  1. @
    isYjUqGgoKdJ 3月15日
    Windows 10 · Chrome 120 · 法国 OVH SA
    此条为悄悄话,仅发布者可见
  2. @
    isYjUqGgoKdJ 3月15日
    Windows 10 · Chrome 120 · 法国 OVH SA
    此条为悄悄话,仅发布者可见
  3. @
    isYjUqGgoKdJ 3月15日
    Windows 10 · Chrome 120 · 法国 OVH SA
    此条为悄悄话,仅发布者可见
  4. @
    isYjUqGgoKdJ 3月15日
    Windows 10 · Chrome 120 · 法国 OVH SA
    此条为悄悄话,仅发布者可见

随机一言

:D 获取中...

文章目录

最新评论

随机文章

Docker 安装中文 Portainer 485 浏览 - 2023/07/15
MySQL 的 now() 函数 468 浏览 - 2023/02/07
修改 Windows控制台编码为UTF-8 638 浏览 - 2022/09/06
不蒜子网站统计的用法 483 浏览 - 2023/09/13