生成公钥私钥:
/**
* RSA 生成公钥私钥
*/
public class CreateSecrteKey {
public static final String KEY_ALGORITHM = "RSA";
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
/**
* 获取公钥
* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap) {
// 获得map中的公钥对象 转为key对象
Key key = (Key) keyMap.get(PUBLIC_KEY);
// 编码返回字符串
return encryptBASE64(key.getEncoded());
}
/**
* 获取私钥
* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap) {
// 获得map中的私钥对象 转为key对象
Key key = (Key) keyMap.get(PRIVATE_KEY);
// 编码返回字符串
return encryptBASE64(key.getEncoded());
}
/**
* 解码返回byte
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptBASE64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
/**
* 编码返回字符串
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(byte[] key) {
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* map对象中存放公私钥
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
// 获得对象 KeyPairGenerator 参数 RSA 1024个字节
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
// 通过对象 KeyPairGenerator 获取对象KeyPair
KeyPair keyPair = keyPairGen.generateKeyPair();
// 通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKey
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// 公私钥对象存入map中
Map<String, Object> keyMap = new HashMap<>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
public static void main(String[] args) {
Map<String, Object> keyMap;
try {
keyMap = initKey();
String publicKey = getPublicKey(keyMap);
System.out.println(publicKey);
String privateKey = getPrivateKey(keyMap);
System.out.println(privateKey);
} catch (Exception e) {
e.printStackTrace();
}
}
}
打印公钥私钥:
将生成的公钥私钥放在yml配置中:
配置类:
RSA工具类:
public class RsaUtil {
/**
* String to hold name of the encryption algorithm.
*/
private static final String ALGORITHM = "RSA";
private static final String CHAR_SET = "utf-8";
private static final Logger logger = LoggerFactory.getLogger(RsaUtil.class);
/**
* Rsa encode string.
*
* @param publicCertificate the public certificate
* @param text the text
* @return the string
*/
public static String rsaEncode(String publicCertificate, String text) {
try {
byte[] publicBytes = baseStrToByte(publicCertificate);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PublicKey pubKey = keyFactory.generatePublic(keySpec);
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// encrypt the plain text using the public key
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherBytes = cipher.doFinal(text.getBytes(CHAR_SET));
return baseByteToStr(cipherBytes);
} catch (Exception e) {
logger.error("publicCertificate:{} \r\n text:{}", publicCertificate, text, e);
}
return null;
}
/**
* Rsa decode string.
*
* @param privateCertificate the private certificate
* @param text the text
* @return the string
*/
public static String rsaDecode(String privateCertificate, String text) {
try {
byte[] privateBytes = baseStrToByte(privateCertificate);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PrivateKey priKey = keyFactory.generatePrivate(keySpec);
byte[] cipherText;
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// encrypt the plain text using the public key
cipher.init(Cipher.DECRYPT_MODE, priKey);
byte[] textbyte = baseStrToByte(text);
cipherText = cipher.doFinal(textbyte);
return new String(cipherText, CHAR_SET);
} catch (Exception e) {
logger.error("privateCertificate:{} \r\n text:{}", privateCertificate, text, e);
}
return null;
}
/**
* @param str str
* @return byte[]
*/
private static byte[] baseStrToByte(String str) {
return Base64.getDecoder().decode(str);
}
/**
* @param bytes bytes
* @return String
*/
private static String baseByteToStr(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}
}
使用: