读取签名文件后无法验证签名
verifySignature方法中sign.verify(signature)返回false;我认为这与我如何使用带有signedobject.obj文件的输出流和输入流(证书通过有效性)有关。我可以从文件中正确读取消息。
代码:
RSAPrivateKey pk = (RSAPrivateKey) ks.getKey("CS2", "fihjo".toCharArray());
Signature s = Signature.getInstance("SHA1withRSA");
s.initSign(pk);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
String message = "Hi Sign ME!!!";
oos.writeObject(message);
oos.writeObject(s.sign());
byte[] barr = baos.toByteArray();
s.update(barr);
FileOutputStream out1 = new FileOutputStream("signedobject.obj");
out1.write(barr);
//out1.write(s.sign());
out1.close();
verifySignature(ks);
verifySignature(KeyStore)方法:
FileInputStream fis = new FileInputStream("signedobject.obj");
ObjectInputStream ois = new ObjectInputStream(fis);
String message = (String)ois.readObject(); // read message
System.out.println("msg: "+message);
byte[] signature = (byte[])ois.readObject(); // read signature, hmmm
X509Certificate xcert = (X509Certificate) ks.getCertificate("CS1");
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initVerify(xcert.getPublicKey());
sign.update(message.getBytes());
if ( sign.verify(signature) ) // This is where it fails!
System.out.println("It is validly signed. String: "+message);
else System.out.println("It isn't valid");
sign.verify(signature) returns false in the verifySignature method; and I think it has something to do about how I use outputstreams and inputstreams with the signedobject.obj file (certificate passed validity). I can read the message correctly from file.
Code:
RSAPrivateKey pk = (RSAPrivateKey) ks.getKey("CS2", "fihjo".toCharArray());
Signature s = Signature.getInstance("SHA1withRSA");
s.initSign(pk);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
String message = "Hi Sign ME!!!";
oos.writeObject(message);
oos.writeObject(s.sign());
byte[] barr = baos.toByteArray();
s.update(barr);
FileOutputStream out1 = new FileOutputStream("signedobject.obj");
out1.write(barr);
//out1.write(s.sign());
out1.close();
verifySignature(ks);
verifySignature(KeyStore) method:
FileInputStream fis = new FileInputStream("signedobject.obj");
ObjectInputStream ois = new ObjectInputStream(fis);
String message = (String)ois.readObject(); // read message
System.out.println("msg: "+message);
byte[] signature = (byte[])ois.readObject(); // read signature, hmmm
X509Certificate xcert = (X509Certificate) ks.getCertificate("CS1");
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initVerify(xcert.getPublicKey());
sign.update(message.getBytes());
if ( sign.verify(signature) ) // This is where it fails!
System.out.println("It is validly signed. String: "+message);
else System.out.println("It isn't valid");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在更新消息的字节数组后呼号。例如:
You have to call sign after update on the byte array of the message. For example: