以下是Java 8代码,结果显示“signature NOT signed by matching key”,我不明白为什么。有任何想法吗?我的猜测是与openssl生成私钥或证书的方式有关,但从我的研究来看,我正在使用正确的命令。我试图加载私钥,签署一条消息,然后使用x509证书中包含的公钥验证签名。privatekey.pkcs8.key文件是使用基于MS Windows的openssl生成的以下命令生成的:
公钥证书publickey.cer也是使用基于MS Windows的openssl生成的,使用以下命令:
openssl.exe" genrsa -out privatekey.pem 1024
openssl pkcs8 -in privatekey.pem -inform PEM -topk8 -out privatekey.pkcs8.key -outform DER -nocrypt
公钥证书publickey.cer也是使用基于MS Windows的openssl生成的,使用以下命令:
openssl req -x509 -key privatekey.pem -days 365 -out publickey.cer -new
我一直在使用以下Java代码尝试验证签名:
public static void main(String[] args) throws Exception{
new TestSigs();
}
public TestSigs {
byte[] signature = generateSignatureForMessage("src/cryptoprivatekey.pkcs8.key", "Hello");
verifySignature("src/crypto/publickey.cer", signature);
}
public byte[] generateSignatureForMessage(String privateKeyPath, String message) throws Exception {
RSAPrivateKey privKey = loadPrivateRSAKeyFromFile(privateKeyPath);
Signature s = Signature.getInstance("SHA256withRSA");
s.initSign(privKey);
s.update(ByteBuffer.wrap(message.getBytes()));
byte[] signature = s.sign();
return signature;
}
private void verifySignature(String publicKeyPath, byte[] signature) throws Exception {
Certificate cert = loadCertificate(publicKeyPath);
Signature s = Signature.getInstance("SHA256withRSA");
s.initVerify(cert);
if(s.verify(signature)) {
System.out.println("signature signed by matching key");
} else {
System.out.println("signature NOT signed by matching key");
}
}
private Certificate loadCertificate(String filename) throws FileNotFoundException, CertificateException {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
return cf.generateCertificate(bis);
}
private RSAPrivateKey loadPrivateRSAKeyFromFile(String keyPath) throws Exception {
byte[] privKeyBytes = loadRSAKeyBytesFromFile(keyPath);
KeyFactory kFact = KeyFactory.getInstance("RSA");
KeySpec ks = new PKCS8EncodedKeySpec(privKeyBytes);
return (RSAPrivateKey)kFact.generatePrivate(ks);
}