JAVA - 如何从 sqlite db 存储和读取 RSA 公钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3172123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:41:03  来源:igfitidea点击:

JAVA - How to store and read an RSA public key from an sqlite db

javasecurityrsa

提问by michele

i have to store a public key and a private one into a sqlite database. Actually i write the pubKey.getEncoded() into the database, and to recreate the pubkey i use the following code:

我必须将公钥和私钥存储到 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);
    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);

but it gives me the following error

但它给了我以下错误

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

线程“main”中的异常 java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: 检测过早的 EOF

at this point :

这一点 :

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

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

Anyone can help me?

任何人都可以帮助我吗?

Thank you in advance

先感谢您

回答by Romain Hippeau

If you look here: http://java.sun.com/docs/books/tutorial/jdbc/basics/retrieving.htmlyou will see how to properly retrieve from a DB. That is assuming you have a type of char(n) for your DB column.

如果您看这里:http: //java.sun.com/docs/books/tutorial/jdbc/basics/retrieving.html,您将看到如何从数据库中正确检索。那是假设您的 DB 列有一种 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);
}

If the type is a BLOB or CLOB then you need to use a different mechanism. http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/blob.html

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

回答by Mike Baranczak

  1. When you first get a ResultSet, the cursor points to the space beforethe first row of results. So you need to call next() on it before trying to retrieve the data.

  2. You should make sure that the query actually succeeded.

  1. 当你第一次得到一个 ResultSet 时,光标指向第一行结果之前的空间。因此,您需要在尝试检索数据之前对其调用 next()。

  2. 您应该确保查询确实成功了。