try {
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);
// In 1.2 beta 4, we need an argument to the getInstance method
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(new FileInputStream(System.getProperty("user.home") + File.separator + ".keystore"), null);
java.security.cert.Certificate c = ks.getCertificate(args[0]);
PublicKey pk = c.getPublicKey();
Signature s = Signature.getInstance("DSA");
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);
}