java中将byte[]转换为PrivateKey进行数字签名
我需要首先使用 SHA-1 摘要算法对字符串进行数字签名,然后应用 RSA 算法,使用 PrivateKey 对其进行签名。我已经将 PrivateKey 作为 base64 数据类型 char(250) 存储在我的数据库中。我的问题是,我不知道如何将其转换为 PrivateKey 以便使用它进行登录:
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] cipherText = cipher.doFinal(digest);
Digest 是一个字节数组,我对其应用了 SHA-1 摘要算法:
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte [] ba = cadena.getBytes();
byte [] digest = md.digest(ba);
这是我想到的解决方案,但如果任何人有更好的解决方案我将不胜感激。
I need to digitally sign a String using the SHA-1 digest algorithm first and then apply the RSA algorithm, using a PrivateKey to sign it. I already have the PrivateKey stored in my database as data type char(250) in base64. My problem is that I don't know how to convert it into a PrivateKey for using it for signing in:
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] cipherText = cipher.doFinal(digest);
Digest was an array of bytes to which I applied the SHA-1 digest algorithm:
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte [] ba = cadena.getBytes();
byte [] digest = md.digest(ba);
That is the solution I thought about, but if anyone has a better solution I would appreciate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道你是如何将私钥保存到数据库中的。但是此页面提供了一些关于如何从文件系统加载
KeyStore
,并检索Private
和Public
密钥。相关代码片段(根据您的要求进行修改)是,
然后,
I don't know how you persisted the private key into the DB. But this page provides some information on how to load a
KeyStore
from a file system, and retrieve thePrivate
andPublic
keys.Relevant code snippet (modified to suit your requirement) is,
and then,