Node:Key Agreement Example, Previous:The KeyAgreementFactory class, Up:Key Agreements



Example, Key agreement

The following example shows ...

     IKeyPairGenerator kpg =
         KeyPairGeneratorFactory.getInstance(Registry.DH_KPG);
     kpg.setup(new HashMap()); // use default values
     KeyPair kpA = kpg.generate();
     KeyPair kpB = kpg.generate();
     IKeyAgreementParty A = new DiffieHellmanSender();
     IKeyAgreementParty B = new DiffieHellmanReceiver();
     
     Map mapA = new HashMap();
     mapA.put(DiffieHellmanKeyAgreement.KA_DIFFIE_HELLMAN_OWNER_PRIVATE_KEY,
              kpA.getPrivate());
     Map mapB = new HashMap();
     mapB.put(DiffieHellmanKeyAgreement.KA_DIFFIE_HELLMAN_OWNER_PRIVATE_KEY,
              kpB.getPrivate());
     
     A.init(mapA);
     B.init(mapB);
     
     // (1) A -> B: g**x mod p
     OutgoingMessage out = A.processMessage(null);
     
     // (2) B -> A: g^^y mod p
     out = B.processMessage(new IncomingMessage(out.toByteArray()));
     
     byte[] k2 = B.getSharedSecret();
     
     // A computes the shared secret
     out = A.processMessage(new IncomingMessage(out.toByteArray()));
     
     byte[] k1 = A.getSharedSecret();