I have a private key in .pem format. The contents start with "BEGIN RSA Private Key" and is ASCII armored.
I need to use this key in the object of Signature but it throws an error stating Invalid Key format. Please help!
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();
}
}}