sign.verify(signature)在verifySignature方法中返回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");
答案 0 :(得分:1)
您必须在更新消息的字节数组后调用sign。例如:
String message = "Hi Sign ME!!!";
s.update(message.getBytes("UTF8");
byte[] signature = s.sign()
oos.writeObject(message);
oos.writeObject(signature);