 HuTool 实现 RSA 加密
        
        
          HuTool 实现 RSA 加密        
      
                
                1年前
            
          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);
    } 
            