如何使用java.security.Signature进行验证 [英] how to do verify using 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?
推荐答案
- 使用
KeyFactory
将键规范转换为对象. - 调用
Signature.getInstance(algName)
获取签名实例. - 使用
Signature
的initVerify
方法关联密钥以进行签名验证. - 使用
update
填充签名
字节. - 最后,调用
verify
. - 利润
- Use
KeyFactory
to translate key specifications to objects. - Call
Signature.getInstance(algName)
to get a signature instance. - Use
Signature
'sinitVerify
method to associate a key for signature verification. - Use
update
to feed theSignature
bytes. - Finally, call
verify
. - 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屋!