FileDocCategorySizeDatePackage
Receive.javaAPI DocExample2678Sun Oct 25 18:13:36 GMT 1998None

Receive

public class Receive extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

		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);
		}