如何从String加载RSA公钥以在Java中进行签名验证?

前端之家收集整理的这篇文章主要介绍了如何从String加载RSA公钥以在Java中进行签名验证? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有以下公共密钥,它以文本形式存储在数据库(Postgressql)中.这是一个字符串,在java中:

-----BEGIN RSA PUBLIC KEY-----     
MIICCgKCAgEA1ht0OqZpP7d/05373OE7pB7yCVGNGzkUEuCneyfOzps6iA03NbvI
1ZL0Jpp/N3AW73lGdhaoa3X3JE4GsI/bsToVLQwTKmIOC4yjTvBctmFEoyhhTfxW
s1UHZKl4XZ/7THbRlKHhRaTKyfDAbikkMAxNT/qutLAPjnN1qOwjb1oRq52NP6FJ
KWTTikz4UeOHroX+Xthn2fJSJDlQ4YMdBbgrZVx5JcHKNuPTKRf5gI8QQKMSA9Q9
QJRE5OGp7b6dG14ZmOUnUxb00Mp20LgcaGPcuWU+oFsbQaF6W4G4bdkSZRJJXhSg
d4Q7mahpar94/gnztJmth0GzqTWUYyZIWNqIFoMwuOgeaiDV43zb3uLsRVpRKYYy
esmzcOy/jTScVLRCD8QRyu9B2wgCkNAVztQOXPCOOa4O1LlVQWaecIs4WPhOqDhi
KTBhyVkpC1TrrBkp+QMqMqWll1OyVb6k/7uV0qE/i6rHJtjo5v9bcIgYzswyx9CD
9PKl2Q0L0Jg7TMG+yLDIrLfGeuSeEc4XYJzN7bJcCeiizzu5iU9dQUkrncOrq9jn
Ub2pM/+A+JqIsoPK3IY/pJKqH4JYpGKhO1iPQF6iXIZT1r3ZgJUSQtzSeyYqhkla
2uR2BsbPbDqebCuXm3lAsY5w+dujijcn96PKwYha1LsK5sACHuJ79AMCAwEAAQ==
-----END RSA PUBLIC KEY-----

很抱歉,我不知道该密钥是如何生成的.我被告知要使用此密钥并验证另一个称为“对象”的字符串的签名.有人告诉我,我用来验证“对象”的算法是SHA256withRSA.

因此,我编写了以下java方法来读取密钥

private PublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException,InvalidKeySpecException,UnsupportedEncodingException {
    publicKey = publicKey.replaceAll("\\n","");
    publicKey = publicKey.replace("-----BEGIN RSA PUBLIC KEY-----","");
    publicKey = publicKey.replace("-----END RSA PUBLIC KEY-----","");
    publicKey = publicKey.trim();
    byte[] keyDecoded = Base64.getDecoder().decode(publicKey.getBytes());
    X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(keyDecoded);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PublicKey pubKey = kf.generatePublic(publicSpec);
    return pubKey;
}

关键是我得到以下异常:

java.security.InvalidKeyException: IOException: algid parse error,not
a sequence

我已经阅读了很多关于stackoverflow的问题.其他用户编写的代码与我的代码非常相似(有时相同).所以我绝对不明白为什么它对我不起作用.其他开发人员(同事)在PHP中也做同样的事情,并且效果很好,因此我将放弃错误的公共密钥的假设.也许我不是很清楚这个过程?请问您有什么线索吗?

我还尝试使用BouncyCastle库来解决该问题,如建议的here,但我遇到了同样的异常.以下是我编写的代码

private static PublicKey getPublicKey(String publicKey)
        throws IOException,NoSuchAlgorithmException,InvalidKeySpecException {
    Security.addProvider(new BouncyCastleProvider());
    PemReader pp = new PemReader(new StringReader(publicKey));
    PemObject pem = pp.readPemObject();
    byte[] content = pem.getContent();
    pp.close();

    X509EncodedKeySpec spec = new X509EncodedKeySpec(content);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePublic(spec);
}
最佳答案
您无法使用X509EncodedKeySpec加载该密钥.根据它的JavaDoc documentation,它期望以下格式:

SubjectPublicKeyInfo ::= SEQUENCE {
   algorithm AlgorithmIdentifier,subjectPublicKey BIT STRING }

相反,您的密钥看起来有所不同.我使用了您的帖子中的数据,将其转换为十六进制数据,并将其发布到在线ASN.1 decoder中.

输出是这样的:

SEQUENCE (2 elem)
  INTEGER (4096 bit) 873481340827968071893572683200799871431146795599597693981565010037737…
  INTEGER 65537

如您所知,密钥不包含AlgorithmIdentifier,因此无法使用X509EncodedKeySpec加载它.

我的建议是使用BouncyCastle库及其PEMParser类加载此密钥:

File pemFile = new File("test.pem");
try (PEMParser pp = new PEMParser(new InputStreamReader(new FileInputStream(pemFile)))) {
    SubjectPublicKeyInfo subjPubKeyInfo = (SubjectPublicKeyInfo) pp.readObject();
    RSAKeyParameters rsa = (RSAKeyParameters) PublicKeyFactory.createKey(subjPubKeyInfo);

    RSAPublicKeySpec rsaSpec = new RSAPublicKeySpec(rsa.getModulus(),rsa.getExponent());
    KeyFactory kf = KeyFactory.getInstance("RSA");
    java.security.PublicKey publicKey = kf.generatePublic(rsaSpec);
    System.out.println(publicKey);
}

或者,您可以通过openssl手动将密钥转换为PKCS#8格式.

原文链接:https://www.f2er.com/java/532867.html

猜你在找的Java相关文章