com.github.qq275860560.common.util.RsaUtil Maven / Gradle / Ivy
package com.github.qq275860560.common.util;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import org.springframework.util.Base64Utils;
import lombok.extern.slf4j.Slf4j;
/**
* @author [email protected]
RSA字符串生成器
*/
@Slf4j
public class RsaUtil {
public static void main(String[] args) throws Exception {
RsaUtil.generateRsaKeyBase64EncodeString();
}
public static Map generateRsaKeyBase64EncodeString() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
String privateKeyBase64EncodeString = Base64Utils.encodeToString(keyPair.getPrivate().getEncoded());
log.info("privateKeyBase64EncodeString=" + privateKeyBase64EncodeString);
String publicKeyBase64EncodeString = Base64Utils.encodeToString(keyPair.getPublic().getEncoded());
log.info("publicKeyBase64EncodeString=" + publicKeyBase64EncodeString);
return new HashMap() {
{
put("privateKeyBase64EncodeString", privateKeyBase64EncodeString);
put("publicKeyBase64EncodeString", publicKeyBase64EncodeString);
}
};
}
public static PrivateKey getPrivateKey(String privateKeyBase64EncodeString) throws Exception {
byte[] keyBytes = Base64Utils.decode(privateKeyBase64EncodeString.getBytes());
PKCS8EncodedKeySpec keySpec_privateKey = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory_privateKey = KeyFactory.getInstance("RSA");
return keyFactory_privateKey.generatePrivate(keySpec_privateKey);
}
public static PublicKey getPublicKey(String publicKeyBase64EncodeString ) throws Exception {
byte[] keyBytes = Base64Utils.decode(publicKeyBase64EncodeString.getBytes());
X509EncodedKeySpec keySpec_publicKey = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory_publicKey = KeyFactory.getInstance("RSA");
return keyFactory_publicKey.generatePublic(keySpec_publicKey);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy