如何使用java.security.Signature进行验证 [英] how to do verify using java.security.Signature

查看:55
本文介绍了如何使用java.security.Signature进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一个公开和私有的密钥对.我实际上如何使用 java.security.Signature 验证我用其中一个键签名的字符串?

I have a key pair already, public and private. How do I actually use the java.security.Signature to do verification of a string I signed with one of the keys?

我把两个键都当作字符串.验证方法,实际上是

I have both the keys as Strings. The verify method, it is actually

verify(byte[] signature)

javadoc说:

verify(byte []签名)指示给定签名是否可以使用公钥或签名者的证书进行验证.

verify(byte[] signature) Indicates whether the given signature can be verified using the public key or a certificate of the signer.

在调用verify方法之前,如何使该签名识别用于该验证的公用/专用密钥?换句话说,我如何将我的字符串键转换为可以被签名接受的键对象?

How would I make that signature recognize which public/private key to use for that verifying, before I call the verify method? In other words, how do I turn my string keys into key objects that would get accepted by signature?

推荐答案

  1. 使用 KeyFactory 将键规范转换为对象.
  2. 调用 Signature.getInstance(algName)获取签名实例.
  3. 使用 Signature initVerify 方法关联密钥以进行签名验证.
  4. 使用 update 填充签名字节.
  5. 最后,调用 verify .
  6. 利润
  1. Use KeyFactory to translate key specifications to objects.
  2. Call Signature.getInstance(algName) to get a signature instance.
  3. Use Signature's initVerify method to associate a key for signature verification.
  4. Use update to feed the Signature bytes.
  5. Finally, call verify.
  6. Profit

KeyFactory javadoc:

From the KeyFactory javadoc:

以下是如何使用密钥工厂以从其编码实例化DSA公共密钥的示例.假设爱丽丝已收到鲍勃的数字签名.鲍勃还向她发送了他的公共密钥(以编码格式)以验证他的签名.然后,爱丽丝执行以下操作:

The following is an example of how to use a key factory in order to instantiate a DSA public key from its encoding. Assume Alice has received a digital signature from Bob. Bob also sent her his public key (in encoded format) to verify his signature. Alice then performs the following actions:

X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(bobEncodedPubKey);
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
PublicKey bobPubKey = keyFactory.generatePublic(bobPubKeySpec);
Signature sig = Signature.getInstance("DSA");
sig.initVerify(bobPubKey);
sig.update(data);
sig.verify(signature);

这篇关于如何使用java.security.Signature进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆