FileDocCategorySizeDatePackage
Receive.javaAPI DocExample1636Sat Jan 13 14:36:38 GMT 2001javasec.samples.ch12

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

            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("MD5withRSA");
            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);
        }