从PEM文件中获取ECDSA密钥
程序员文章站
2022-04-09 23:37:56
PEM文件为证书文件,里面的公私钥是不能贴出来直接用的。所以需要把PEM文件里的String型的密钥转换成PrivateKey型package com.csl.ECDSA.demo;import java.security.KeyFactory;import java.security.PrivateKey;import java.security.spec.PKCS8EncodedKeySpec;import org.apache.commons.codec.binary.Hex;impor...
PEM文件为证书文件,里面的公私钥是不能贴出来直接用的。所以需要把PEM文件里的String型的密钥转换成PrivateKey型
package com.csl.ECDSA.demo;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import org.apache.commons.codec.binary.Hex;
import sun.misc.BASE64Decoder;
public class GetKey {
public static void main(String[] args) throws Exception {
PrivateKey privateKey = null;
String keyStr = "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgcG8o9xybP5r3/OJJgWr997rDPqY0YVA4c0c5zqARg8mhRANCAAT7EINiYCZgyqG1Y3Q+DlTeGo+1GCibXmT4PawhbT+EDYz0cdtC5wfAOgELM210id5ELUY+KoNP0ua16JjrGpql";
privateKey = getPrivateKey(keyStr);
System.out.println(Hex.encodeHexString(privateKey.getEncoded()));
}
/**
* 从string转private key
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] bytes = new BASE64Decoder().decodeBuffer(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
KeyFactory keyFactory = KeyFactory.getInstance("EC");
return keyFactory.generatePrivate(keySpec);
}
}
本文地址:https://blog.csdn.net/han404997715/article/details/107378459