下面是oppo平台的生成公钥的要求
公钥需要使用 RSA 算法生成,1024 位,生成后使用 Base64 进行编码,编码后的长度是 216 位
Base64 使用了 Apache 的 commons-codec 工具包,这个我也提供下,也可以直接去maven仓库下载
https://repo1.maven.org/maven2/commons-codec/commons-codec/1.10/commons-codec-1.10.jar
public class MyRSAUtils {
/**
* \* 加密算法RSA
*/
public static final String KEY_ALGORITHM = "RSA";
/**
* \* 获取公钥的key
*/
private static final String PUBLIC_KEY = "RSAPublicKey";
/**
* \* 获取私钥的key
*/
private static final String PRIVATE_KEY = "RSAPrivateKey";
/**
* \* <p>
* \* 生成密钥对(公钥和私钥)
* \* </p>
* <p>
* \* @return
* \* @throws Exception
*/
public static Map<String, Object> genKeyPair() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return base64Encode(key.getEncoded());
}
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return base64Encode(key.getEncoded());
}
/**
* \* 用base64编码
* \* @param bytes
* \* @return
*/
private static String base64Encode(byte[] bytes) {
return new String(Base64.encodeBase64(bytes));
}
public static void main(String[] args) throws Exception {
Map<String, Object> pairs = MyRSAUtils.genKeyPair();
System.out.println("公钥:" + MyRSAUtils.getPublicKey(pairs));
System.out.println("私钥:" + MyRSAUtils.getPrivateKey(pairs));
}
}
标签:return,String,keyMap,KeyPairGenerator,KEY,钥对,static,public,oppo
From: https://www.cnblogs.com/maowuge/p/16829505.html