JAVA   30

DHKeyWriter

Guest on 14th August 2022 06:39:23 AM

  1. // DHKeyWriter.java                                    -*- Java -*-
  2. //    The DH key object
  3. //
  4. // Copyright(C) 1998 Robert Sexton
  5. // You can do anything you want with this, except pretend you
  6. // wrote it.
  7. //
  8. // Written   :   Robert Sexton         University of Cincinnati
  9. //   By          
  10. //
  11. // Written   :   John Franco
  12. //   For         Special Topics: Java Programming
  13. //               15-625-595-001, Fall
  14. // RCS       :
  15. //
  16. // $Source: /home/franco/CVS/html/Courses/c6053/lectures/Java/DH/DHKeyWriter.java,v $
  17. // $Revision: 1.1 $
  18.  
  19. //
  20. // $Log: DHKeyWriter.java,v $
  21. //   franco
  22. // *** empty log message ***
  23. //
  24. // Revision 1.1    franco
  25. // *** empty log message ***
  26. //
  27. // Revision 0.2    bkuhn
  28. //   -- latest changes from Robert
  29. //
  30. // Revision 1.1    robert
  31. // Initial revision
  32. //
  33. // Revision 0.1    bkuhn
  34. //   # initial version
  35. //
  36.  
  37. import java.io.*;
  38. import java.util.Date;
  39. import java.math.BigInteger;
  40. import java.security.*;
  41.  
  42. /*
  43.  * This object is used for Public Key Exchange.
  44.  * The Crypto routines require it.  I haven't put the heavy
  45.  * duty methods in here because I want it to stay small
  46.  */
  47.  
  48. class DHKey implements Serializable {
  49.    BigInteger p, g;    /* These two make up the public Key */
  50.  
  51.    String Description;
  52.    Date created;
  53.    
  54.    DHKey (BigInteger P, BigInteger G, String what) {
  55.       p = P;
  56.       g = G;
  57.      
  58.       Description = what;
  59.       created = new Date();
  60.    }
  61.  
  62.    /* You may wish to customize the following */
  63.    public String toString() {
  64.       StringBuffer scratch = new StringBuffer();
  65.       scratch.append("Public Key(p): " + p.toString(32) +
  66.                      " (" + p.toString() + ")\n" );
  67.       scratch.append("Public Key(g): " + g.toString(32) +
  68.                      " (" + g.toString() + ")\n" );
  69.       scratch.append("Description: "   + Description  + "\n" );
  70.       scratch.append("Created: "       + created );
  71.       return scratch.toString();
  72.    }
  73. }
  74.  
  75. public class DHKeyWriter {
  76.    public static void main (String args[]) {
  77.       try {
  78.          BigInteger p = new BigInteger("563");
  79.          BigInteger g = new BigInteger("5");
  80.          DHKey dhout = new DHKey(p, g, "Diffie-Hellman public keys");
  81.          FileOutputStream fos = new FileOutputStream("DHKey");
  82.          ObjectOutputStream out = new ObjectOutputStream(fos);
  83.          out.writeObject(dhout);
  84.          fos.close();
  85.          
  86.          FileInputStream fin = new FileInputStream("DHKey");
  87.          ObjectInputStream in = new ObjectInputStream(fin);
  88.          DHKey dhin = (DHKey)in.readObject();
  89.          System.out.println(dhin.toString());
  90.       } catch (Exception e) {
  91.          System.out.println(e.toString());
  92.       }
  93.    }
  94. }

Raw Paste


Login or Register to edit or fork this paste. It's free.