converting java to applet

Discussion in 'Software' started by anub, Sep 11, 2007.

  1. anub

    anub

    Joined:
    Sep 11, 2007
    Messages:
    4
    Hai,
    i am new to java. i am attaching a portion of java application which i want to convert as applet. Please help me to convert this into applet. expecting ur valuable commnets
    regards
    anu

    <code>

    import java.io.*;
    import java.math.BigInteger;
    import java.security.*;
    import java.security.spec.*;
    import java.security.interfaces.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import javax.crypto.interfaces.*;
    import com.sun.crypto.provider.SunJCE;

    /**
    * This program executes the Diffie-Hellman key agreement protocol
    * between 2 parties: Alice and Bob.
    *
    */

    public class DHKeyAgreement2 {

    private DHKeyAgreement2() {}

    public static void main(String argv[]) {
    try {
    String mode = "USE_SKIP_DH_PARAMS";

    DHKeyAgreement2 keyAgree = new DHKeyAgreement2();

    if (argv.length > 1) {
    keyAgree.usage();
    throw new Exception("Wrong number of command options");
    } else if (argv.length == 1) {
    if (!(argv[0].equals("-gen"))) {
    keyAgree.usage();
    throw new Exception("Unrecognized flag: " + argv[0]);
    }
    mode = "GENERATE_DH_PARAMS";
    }

    keyAgree.run(mode);
    } catch (Exception e) {
    System.err.println("Error: " + e);
    System.exit(1);
    }
    }

    private void run(String mode) throws Exception {

    DHParameterSpec dhSkipParamSpec;

    if (mode.equals("GENERATE_DH_PARAMS")) {
    // Some central authority creates new DH parameters
    System.out.println
    ("Creating Diffie-Hellman parameters (takes VERY long) ...");
    AlgorithmParameterGenerator paramGen
    = AlgorithmParameterGenerator.getInstance("DH");
    paramGen.init(512);
    AlgorithmParameters params = paramGen.generateParameters();
    dhSkipParamSpec = (DHParameterSpec)params.getParameterSpec
    (DHParameterSpec.class);
    } else {
    // use some pre-generated, default DH parameters
    System.out.println("Using SKIP Diffie-Hellman parameters");
    dhSkipParamSpec = new DHParameterSpec(skip1024Modulus,
    skip1024Base);
    }

    /*
    * Alice creates her own DH key pair, using the DH parameters from
    * above
    */
    System.out.println("ALICE: Generate DH keypair ...");
    KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH");
    aliceKpairGen.initialize(dhSkipParamSpec);
    KeyPair aliceKpair = aliceKpairGen.generateKeyPair();

    // Alice creates and initializes her DH KeyAgreement object
    System.out.println("ALICE: Initialization ...");
    KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH");
    aliceKeyAgree.init(aliceKpair.getPrivate());

    // Alice encodes her public key, and sends it over to Bob.
    byte[] alicePubKeyEnc = aliceKpair.getPublic().getEncoded();

    /*
    * Let's turn over to Bob. Bob has received Alice's public key
    * in encoded format.
    * He instantiates a DH public key from the encoded key material.
    */
    KeyFactory bobKeyFac = KeyFactory.getInstance("DH");
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec
    (alicePubKeyEnc);
    PublicKey alicePubKey = bobKeyFac.generatePublic(x509KeySpec);

    /*
    * Bob gets the DH parameters associated with Alice's public key.
    * He must use the same parameters when he generates his own key
    * pair.
    */
    DHParameterSpec dhParamSpec = ((DHPublicKey)alicePubKey).getParams();

    // Bob creates his own DH key pair
    System.out.println("BOB: Generate DH keypair ...");
    KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH");
    bobKpairGen.initialize(dhParamSpec);
    KeyPair bobKpair = bobKpairGen.generateKeyPair();

    // Bob creates and initializes his DH KeyAgreement object
    System.out.println("BOB: Initialization ...");
    KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH");
    bobKeyAgree.init(bobKpair.getPrivate());

    // Bob encodes his public key, and sends it over to Alice.
    byte[] bobPubKeyEnc = bobKpair.getPublic().getEncoded();

    /*
    * Alice uses Bob's public key for the first (and only) phase
    * of her version of the DH
    * protocol.
    * Before she can do so, she has to instantiate a DH public key
    * from Bob's encoded key material.
    */
    KeyFactory aliceKeyFac = KeyFactory.getInstance("DH");
    x509KeySpec = new X509EncodedKeySpec(bobPubKeyEnc);
    PublicKey bobPubKey = aliceKeyFac.generatePublic(x509KeySpec);
    System.out.println("ALICE: Execute PHASE1 ...");
    aliceKeyAgree.doPhase(bobPubKey, true);

    /*
    * Bob uses Alice's public key for the first (and only) phase
    * of his version of the DH
    * protocol.
    */
    System.out.println("BOB: Execute PHASE1 ...");
    bobKeyAgree.doPhase(alicePubKey, true);

    /*
    * At this stage, both Alice and Bob have completed the DH key
    * agreement protocol.
    * Both generate the (same) shared secret.
    */
    byte[] aliceSharedSecret = aliceKeyAgree.generateSecret();
    int aliceLen = aliceSharedSecret.length;

    byte[] bobSharedSecret = new byte[aliceLen];
    int bobLen;
    try {
    // show example of what happens if you
    // provide an output buffer that is too short
    bobLen = bobKeyAgree.generateSecret(bobSharedSecret, 1);
    } catch (ShortBufferException e) {
    System.out.println(e.getMessage());
    }
    // provide output buffer of required size
    bobLen = bobKeyAgree.generateSecret(bobSharedSecret, 0);

    System.out.println("Alice secret: " +
    toHexString(aliceSharedSecret));
    System.out.println("Bob secret: " +
    toHexString(bobSharedSecret));

    if (!java.util.Arrays.equals(aliceSharedSecret, bobSharedSecret))
    throw new Exception("Shared secrets differ");
    System.out.println("Shared secrets are the same");

    }


    </code>
     
    anub, Sep 11, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean Putmans

    Java Applet - Problem

    Jean Putmans, Aug 6, 2004, in forum: Firefox
    Replies:
    0
    Views:
    556
    Jean Putmans
    Aug 6, 2004
  2. Dick Stouffer
    Replies:
    8
    Views:
    2,078
    Dick Stouffer
    Oct 5, 2004
  3. pbuchheit@hotmail.com

    Java applet crashes Firefox

    pbuchheit@hotmail.com, Oct 4, 2005, in forum: Firefox
    Replies:
    1
    Views:
    4,178
  4. Phil

    java applet

    Phil, Mar 2, 2004, in forum: Computer Support
    Replies:
    4
    Views:
    522
    Mike245
    Mar 3, 2004
  5. granville

    java applet fails to load

    granville, Mar 3, 2004, in forum: Computer Support
    Replies:
    1
    Views:
    519
    Alexander Rogge
    Mar 3, 2004
Loading...

Share This Page