So i'm reading the certificate, privatekey..etc from a usb dongle and i try to sign a pdf, but it gives me the following exception :
Exception in thread "main" java.security.InvalidKeyException: Supplied key (sun.security.mscapi.CPrivateKey) is not a RSAPrivateKey instance
at org.bouncycastle.jcajce.provider.asymmetric.rsa.DigestSignatureSpi.engineInitSign(Unknown Source)
at java.base/java.security.Signature$Delegate.engineInitSign(Signature.java:1370)
at java.base/java.security.Signature.initSign(Signature.java:635)
at com.itextpdf.text.pdf.security.PrivateKeySignature.sign(PrivateKeySignature.java:114)
Here i'm using the com.itextpdf.text library (MakeSignature, ExternalSignature and some other classes) and this is what my -main- code looks like :
KeyStore keyStore = KeyStore.getInstance("Windows-MY");
keyStore.load(null, password.toCharArray());
InputStream in = new ByteArrayInputStream(keyStore.aliases().nextElement().getBytes(StandardCharsets.UTF_8));
keyStore.load(in, password.toCharArray());
String alias = keyStore.aliases().nextElement();
PrivateKey pk = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
java.security.cert.Certificate[] chain = keyStore.getCertificateChain(alias);
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);
ExternalDigest digest = new BouncyCastleDigest();
ExternalSignature signature = new PrivateKeySignature(pk, DigestAlgorithms.SHA1, provider.getName());
System.out.println(signature.getEncryptionAlgorithm());
System.out.println(signature.getHashAlgorithm());
MakeSignature.signDetached(appearance, digest, signature, chain, null, null, null,
0, MakeSignature.CryptoStandard.CADES);
pdf.close();
reader.close();
os.close();
what i want to know is where does the incompatibility lay ??
is it between the pk algorithm and the provider ?
or is it between the signing class and the pk ? cuz afterall i get the privatekey from the dongle and i cannot change it! Which would mean i need to change something else in my program.