当前位置: 移动技术网 > IT编程>开发语言>Java > 从PEM文件中获取ECDSA密钥

从PEM文件中获取ECDSA密钥

2020年07月17日  | 移动技术网IT编程  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网