我有一个.pem格式的私钥。内容以“begin rsa private key”(开始rsa私钥)开头,并且是ascii铠装的。
我需要在签名对象中使用此密钥,但它会抛出一个错误,说明密钥格式无效。请帮忙!
package com.scb;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
public class Dec {
public static void main(String[] args) {
String input = "hello";
try {
//Capturing the key file
byte[] keyBytes = Files.readAllBytes(Paths.get("C:\\Users\\note3\\Desktop\\Key pair\\0x719D855F-sec.asc"));
PKCS8EncodedKeySpec spec =
new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
Signature privateSignature = Signature.getInstance("SHA256withRSA");
privateSignature.initSign(kf.generatePrivate(spec));
privateSignature.update(input.getBytes("UTF-8"));
byte[] s = privateSignature.sign();
System.out.println(Base64.getEncoder().encodeToString(s));
}
catch(Exception e) {
e.printStackTrace();
}
}}
错误截图
暂无答案!
目前还没有任何答案,快来回答吧!