gpt4 book ai didi

JAVA - 如何从 sqlite 数据库存储和读取 RSA 公钥

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:40:57 26 4
gpt4 key购买 nike

我必须将公钥和私钥存储到 sqlite 数据库中。实际上我将 pubKey.getEncoded() 写入数据库,并使用以下代码重新创建公钥:

    ResultSet result = stat.executeQuery("select publickey from cert where id='1'");
KeyFactory rsaKeyFac = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(result.getBytes(1));
RSAPublicKey pubKey;
pubKey = (RSAPublicKey) rsaKeyFac.generatePublic(keySpec);

但它给了我以下错误

Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: Detect premature EOF

此时:

pubKey = (RSAPublicKey) rsaKeyFac.generatePublic(keySpec);

谁能帮帮我?

提前致谢

最佳答案

如果你看这里:http://java.sun.com/docs/books/tutorial/jdbc/basics/retrieving.html您将看到如何正确地从数据库中检索。这是假设您的数据库列有一种 char(n) 类型。

ResultSet result = stat.executeQuery("select publickey from cert where id='1'");
while(result.next())
{
KeyFactory rsaKeyFac = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(result.getString(1));
RSAPublicKey pubKey;
pubKey = (RSAPublicKey) rsaKeyFac.generatePublic(keySpec);
}

如果类型是 BLOB 或 CLOB,那么您需要使用不同的机制。 http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/blob.html

关于JAVA - 如何从 sqlite 数据库存储和读取 RSA 公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3172123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com