public static void main(String[] args) throws Exception{
        // 算法
        String algorithm = "DES";
        Cipher cipher = Cipher.getInstance(algorithm);
        // 原密码
        String password = "hhhhhhhh";
        String content = "你好, hello";
        Charset charset = StandardCharsets.UTF_8;
        // 生成密钥
        SecretKeySpec key = new SecretKeySpec(password.getBytes(charset), algorithm);
        // 加密初始化
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] bytes = Base64.getEncoder().encode(cipher.doFinal(content.getBytes(charset)));
        System.out.println("密文: " + new String(bytes));
        // 解密初始化
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] bytes1 = cipher.doFinal(Base64.getDecoder().decode(bytes));
        System.out.println("原文: "+ new String(bytes1));
    }
}
 
public class RSADemoTest {
    public static void main(String[] args) throws Exception {
        String content = "hello, 中国";
        Map<String, Key> keyGen = gen();
        String enCodeContent = encode(content, keyGen.get("pri"));
        System.out.println("密文: " + enCodeContent);
        String deCodeContent = decode(enCodeContent, keyGen.get("pub"));
        System.out.println("原文: " + deCodeContent);
    }
    public static Map<String, Key> gen() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        HashMap<String, Key> map = new HashMap<>();
        map.put("pub", publicKey);
        map.put("pri", privateKey);
        return map;
    }
    /**
     * 加密
     */
    public static String encode(String content, Key pubKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        byte[] bytes = Base64.getEncoder().encode(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));
        return new String(bytes);
    }
    /**
     * 解密
     */
    public static String decode(String content, Key priKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        byte[] bytes = cipher.doFinal(Base64.getDecoder().decode(content.getBytes(StandardCharsets.UTF_8)));
        return new String(bytes);
    }
}