【Java】RSA 生成密钥对
2021-05-02 19:28
标签:enc group spring port pac rop too tar pil 使用 RSA 算法生成公钥和私钥。 【Java】RSA 生成密钥对 标签:enc group spring port pac rop too tar pil 原文地址:https://www.cnblogs.com/jiangbo44/p/13202202.html环境
概述
pom.xml
RSADemo
package jiangbo.java.rsa;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class RSADemo {
public static void main(String[] args) throws NoSuchAlgorithmException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair pair = keyGen.generateKeyPair();
byte[] publicBytes = pair.getPublic().getEncoded();
byte[] privateBytes = pair.getPrivate().getEncoded();
System.out.println("public key: " + base64Encode(publicBytes));
System.out.println("private key: " + base64Encode(privateBytes));
}
static String base64Encode(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}
}