try {
Security.addProvider(new XYZProvider());
String data = null;
byte signature[] = null;
FileInputStream fis = new FileInputStream("test");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
try {
data = (String) o;
} catch (ClassCastException cce) {
System.out.println("Unexpected data in file");
System.exit(-1);
}
o = ois.readObject();
try {
signature = (byte []) o;
} catch (ClassCastException cce) {
System.out.println("Unexpected data in file");
System.exit(-1);
}
System.out.println("Received message");
System.out.println(data);
KeyStoreHandler ksh = new KeyStoreHandler(null);
KeyStore ks = ksh.getKeyStore();
java.security.cert.Certificate c =
ks.getCertificate(args[0]);
PublicKey pk = c.getPublicKey();
Signature s = Signature.getInstance("XYZwithSHA");
s.initVerify(pk);
s.update(data.getBytes());
if (s.verify(signature)) {
System.out.println("Message is valid");
}
else System.out.println("Message was corrupted");
} catch (Exception e) {
System.out.println(e);
}