签署消息并通过Bouncy Castle进行恢复消息的验证 [英] sign the message and verify with recover message by Bouncy Castle

查看:97
本文介绍了签署消息并通过Bouncy Castle进行恢复消息的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我写了以下代码来对消息进行签名,然后通过Bouncy Castle在Java中对其进行验证。

签名可以正常工作,但不能正常工作。代码打印的结果:

签名被篡改

无法恢复

并返回null。

为什么eng.hasFullMessage()函数返回false,为什么以下代码不起作用?
全部感谢。



I write the following code for signing the message and then verify it, in java by Bouncy Castle.
signing work properly but verifying not work. the result of code print:
signature tampered
can not recover
and return null.
why eng.hasFullMessage() function return false and why the following code doesn't work?
thanks all.

 
public static String sigVer(PublicKey pu, PrivateKey pr, String original) throws Exception{
        //sign
        BigInteger big = ((RSAKey) pu).getModulus();
byte[] text = original.getBytes();
RSAKeyParameters rsaPriv = new RSAKeyParameters(true, big,((RSAPrivateKey) pr).getPrivateExponent()); RSAKeyParameters rsaPublic = new RSAKeyParameters(false, big,((RSAPublicKey) pu).getPublicExponent()); RSAEngine rsa = new RSAEngine(); byte[] data; Digest dig = new SHA1Digest(); ISO9796d2Signer eng = new ISO9796d2Signer(rsa, dig, true); eng.init(true, rsaPriv); eng.update(text[0]); eng.update(text, 1, text.length - 1);
data = eng.generateSignature(); String signature = data.toString(); //verify eng = new ISO9796d2Signer(rsa, dig, true); eng.init(false, rsaPublic); text = signature.getBytes(); if (!eng.verifySignature(text)) { System.out.println("signature tampered"); } try{ if (eng.hasFullMessage()) { eng.updateWithRecoveredMessage(signature.getBytes());
} byte[] message = eng.getRecoveredMessage(); String ss = message.toString(); return ss; } catch (Exception e) { System.out.println("can not recover"); return null;
}


推荐答案

在大多数情况下,我们没有原始消息可以通过签名进行更新和验证。另一方面,长度限制为234个字节。

Most of the times we don't have the original message to update and verifiy by the signature. On the other hand the length is limited to 234 bytes.

这篇关于签署消息并通过Bouncy Castle进行恢复消息的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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