使用Rsa的私钥签名的时候,遇到一个坑。
因为私钥是python使用的,用的pcks1格式,然后java使用pcks8生成秘钥的时候就报错:
algid parse error, not a sequence
网上有很多解决方案,但是比较之后,最方便,最快速的方案是,转换格式,把PKCS1转成PKCS8,使用的工具是支付宝的转换工具,下载地址:/291/106097/
使用的生成签名的代码如下:
public static String rsaSignHex(String content, String privateKey,String signType) {
try {
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec( (privateKey) );
KeyFactory keyf = ("RSA");
PrivateKey priKey = (priPKCS8);//privateKey格式不会,这儿会crash
signature = (signType);// "SHA-256" "SHA1WithRSA" SHA256withRSA
(priKey);
( ());
byte[] signed = ();
return bytesToHex(signed);
}catch (Exception e) {
();
}
return null;
}