翻译一段用C写的代码到JAVA, C代码验证签名可以返回正确的结果,JAVA接口返回的结果一直是false。很沮丧,没有任何思路了,谁能帮忙看看,给点思路?C code: if (1 != RSA_verify(NID_sha1, md, sizeof(md), sig, sigLen, aikRsa)) {
fprintf (stderr, "Error, bad RSA signature in quote\n");
exit (2);
}
RSA_verify原型:int RSA_verify(int type, const unsigned char *m, unsigned int m_len,
unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
aikRsa 从PEM文件中调用接口直接读取:if ((aikRsa = PEM_read_RSA_PUBKEY(key_file, NULL, NULL, NULL)) == NULL) {
fprintf (stderr, "Unable to read RSA file %s\n", av[1]);
exit (1);
}
PEM_read_RSA_PUBKEY原型: RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
pem_password_cb *cb, void *u);
翻译过去的JAVA code: Signature signature;
signature = Signature.getInstance("SHA1withRSA");
signature.initVerify(pubkey);
signature.update(md);
if (!signature.verify(sig)) {
log.error("signature is not correct\n");
} else {
log.info("signature is correct\n");
}
pubkey用下面的方法从文件中读取(从网上找到的一个方法):public PublicKey getPemPublicKey(String filename) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close(); String temp = new String(keyBytes);
String publicKeyPEM = temp.replace("-----BEGIN PUBLIC KEY-----\n", "");
publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", ""); Base64 b64 = new Base64();
byte [] decoded = b64.decode(publicKeyPEM); X509EncodedKeySpec spec =
new X509EncodedKeySpec(decoded);
//KeyFactory kf = KeyFactory.getInstance(algorithm);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}一个一个比较了signature中的参数,"sig" and "md" 与C代码中的值完全对应除了C中是用unsigned char来表示一个byte,而java中的byte是有符号的值,到底哪里有问题了??谢谢各位!!
fprintf (stderr, "Error, bad RSA signature in quote\n");
exit (2);
}
RSA_verify原型:int RSA_verify(int type, const unsigned char *m, unsigned int m_len,
unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
aikRsa 从PEM文件中调用接口直接读取:if ((aikRsa = PEM_read_RSA_PUBKEY(key_file, NULL, NULL, NULL)) == NULL) {
fprintf (stderr, "Unable to read RSA file %s\n", av[1]);
exit (1);
}
PEM_read_RSA_PUBKEY原型: RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x,
pem_password_cb *cb, void *u);
翻译过去的JAVA code: Signature signature;
signature = Signature.getInstance("SHA1withRSA");
signature.initVerify(pubkey);
signature.update(md);
if (!signature.verify(sig)) {
log.error("signature is not correct\n");
} else {
log.info("signature is correct\n");
}
pubkey用下面的方法从文件中读取(从网上找到的一个方法):public PublicKey getPemPublicKey(String filename) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close(); String temp = new String(keyBytes);
String publicKeyPEM = temp.replace("-----BEGIN PUBLIC KEY-----\n", "");
publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", ""); Base64 b64 = new Base64();
byte [] decoded = b64.decode(publicKeyPEM); X509EncodedKeySpec spec =
new X509EncodedKeySpec(decoded);
//KeyFactory kf = KeyFactory.getInstance(algorithm);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}一个一个比较了signature中的参数,"sig" and "md" 与C代码中的值完全对应除了C中是用unsigned char来表示一个byte,而java中的byte是有符号的值,到底哪里有问题了??谢谢各位!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货