private static final String PUBLIC_KEY = “publicKey”;
private static final String PRIVATE_KEY = “privateKey”;
public static final String SIGNATURE_ALGORITHM = “SHA256withRSA”;
/**
* 生成公、私钥
* 根据需要返回String或byte[]类型
*
* @return
*/
private static Map<String, String> createRSAKeys() {
Map<String, String> keyPairMap = new HashMap<>();
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGenerator.initialize(KEY_SIZE, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
//获取公、私钥值
String publicKeyValue = byte2Base64String(publicKey.getEncoded());
String privateKeyValue = byte2Base64String(privateKey.getEncoded());
//存入
keyPairMap.put