Do you receive the Programmer's Heaven newsletter? If not, why not subscribe?

View Digital Envelope in Java (UDP Networking)\Client\KClient.java

Digital Envelope v1.0

Submitted By: ParvezMI
Rating: starstarstarstarstar (Rate It)


import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

class KClient
{
        //__________________ VARIABLES ___________________________
        public static String LocalIP="127.0.0.1";
        public static String MsgVal="";
        public static String PubKey="",PrvKey="",Nval="";
        public static String str1="",str2="",str3="",str4="";
        public static Rsa rsa=new Rsa(); //Creating an Object of Rsa class from Rsa.java
        //__________________ FRAME OBJECTS _______________________
        public static Frame Main1,Main2,Enc1,Enc2,Dec1,Dec2,Msg1,Msg2;
        //__________________ LOGIN FRAME _________________________
        public static Frame          Lgn1,Lgn2;
        public static Label          lblServIP    = new Label ("Server IP Address & Port Number");
        public static TextField txtServIP       = new TextField("192.168.1.4");
        public static Button    cmdConnect  = new Button("Connect");
        public static Label          lblShow1     = new Label("DIGITAL");
        public static Label          lblShow2     = new Label("ENVELOPE");
        //__________________ MAIN FRAME _________________________
        public static Label lblMain=new Label("DIGITAL ENVELOPE");
        public static   MenuBar mbar=new MenuBar();       
        public static   Menu mnufile=new Menu("F i l e ");
        public static   MenuItem mnuEnc,mnuDec,mnuExit;
        //__________________ Encrypt FRAME ______________________
        public static Label      elblMain            = new Label("\"DIGITAL ENVELOPE\" Encryption Screen");
        public static Label      elblUserList                = new Label("List of Logged Users");
        public static Label      elblRSAPubKey              = new Label("RSA Public Key");
        public static Label      elblOf                        = new Label("of");
        public static Label      elblLocalIP  = new Label("Local IP Address");
        public static Label      elblDESKey      = new Label("Random DES Key");
        public static Label      elblEDesKey  = new Label("Encrypted DES Key using RSA");
        public static Label      elblPlainTxt                = new Label("Plain Text");
        public static Label      elblCyferTxt                = new Label("Encrypted Text (Cypher) using DES Key");
        public static Button ecmdEncDesKey            = new Button("Encrypt DES Key");
        public static Button ecmdOpenFile              = new Button("Open File");
        public static Button ecmdEnc            = new Button("Encrypt >");
        public static Button ecmdSend         = new Button("< Send >")
        public static TextField etxtRSAPubKey   = new TextField("");
        public static TextField etxtRSAnVal          = new TextField("");
        public static TextField etxtRemoteIP    = new TextField("");
        public static TextField etxtLocalIP          = new TextField("127.0.0.1");
        public static TextField etxtDESKey            = new TextField("");
        public static TextField etxtEDESKey          = new TextField("");
        public static TextArea  etxtPlainText   = new TextArea("");
        public static TextArea  etxtCyferText   = new TextArea("");
        public static List            etxtUserList  = new List();
        //__________________ Decrypt FRAME ______________________
        public static Label      dlblMain            = new Label("\"DIGITAL ENVELOPE\" Decryption Screen");
        public static Label      dlblUserList                = new Label("Received IP Addresses");
        public static Label      dlblRSAPrvKey              = new Label("RSA Private Key");
        public static Label      dlblLocalIP  = new Label("Local IP Address");
        public static Label      dlblDESKey      = new Label("Encrypted DES Key   using RSA Public Key");
        public static Label      dlblDDesKey  = new Label("Decrypted DES Key   using RSA Private Key");
        public static Label      dlblCyferTxt                = new Label("Cypher Text");
        public static Label      dlblPlainTxt                = new Label("Decrypted Plain Text");
        public static Button dcmdDecDesKey            = new Button("Decrypt DES Key");
        public static Button dcmdDec            = new Button("Decrypt");
        public static Button dcmdSave         = new Button("Save");
        public static TextField dtxtLocalIP          = new TextField("");
        public static TextField dtxtRSAPrvKey    = new TextField("");
        public static TextField dtxtDESKey            = new TextField("");
        public static TextField dtxtDDesKey          = new TextField("");
        public static TextArea  dtxtPlainText   = new TextArea("");
        public static TextArea  dtxtCyferText   = new TextArea("");
        public static List            dtxtUserList  = new List(); //List of Senders
        public static List            dtxtDDesKeys  = new List(); //Hidden
        public static List            dtxtCyfers        = new List(); //Hidden
        //__________________ DataGram ___________________________
        public static DatagramSocket ds;
        public static int buffer_size=1024;
        public static byte buffer[]=new byte[buffer_size];
        public static boolean stat=false ; //Connection status
        /*=================================================================*/
         static class LoginFrame extends Frame
         {
               public LoginFrame() //Constructor
                {
                                setTitle("C O N N E C T  2  S E R V E R");//Setting Frame Title
                                setLocation(231, 175)/* Displaying Login Form */
                               setSize(340,194);
                               setLayout(null);       //Setting Layout of Frame      
                                setVisible(true);
                        addWindowListener(new XWindow());
                        add(lblServIP);
                        add(txtServIP);
                        add(cmdConnect);
                                lblServIP  .setBounds(13 , 69 , 184, 19);
                            txtServIP  .setBounds(199, 66 , 88 , 22);
                                cmdConnect.setBounds(120 , 129 , 109, 25);
                                cmdConnect.addActionListener(new cmdConnect_Click());
                                try{
                                  InetAddress in=InetAddress.getLocalHost();
                                  LocalIP=(in.getHostAddress());
                                }catch(UnknownHostException e){}
                }
            /*-------------------------------------------------------*/ 
                        static class  XWindow extends WindowAdapter //WindowListener for Encryption frame.
                        {
                                public void windowClosing(WindowEvent e)
                                {
                                 System.exit(1);
                                }
                        }
            /*-------------------------------------------------------*/ 
                public class cmdConnect_Click implements ActionListener
                {
                        public void actionPerformed(ActionEvent e)
                        {
                          setVisible(false);  Main2.setVisible(true); //Hide Me,& Show MainFrame
                            System.out.println(txtServIP.getText());
                                Send("PHOENIX",txtServIP.getText(),777);
                                  int k=777;
                                        if(stat==false) //Now Connect
                                        {                      
                                          stat=true;  cmdConnect.setLabel("Disconnect");
                                          Send("C "+LocalIP,txtServIP.getText(),k);
                                                  PubKey = Long.toString(Rsa.e);
                                                  PrvKey = Long.toString(Rsa.d);
                                                  Nval   = Long.toString(Rsa.n);
                                                  dtxtRSAPrvKey.setText(PrvKey);
                                          Send("c "+PubKey,txtServIP.getText(),k); //Sending Public Key to Server
                                          Send("n "+Nval,txtServIP.getText(),k);   //Sending n value to Server
                                         // Alert("Successfully connected to the Server","Connected"); 
                                        }
                        }
                }

         } //Login Frame Ends here
        /*=================================================================*/
                public static class MsgBox extends Frame
                {
                   Label lblMsg=new Label("");
                   Button cmdOK=new Button("OK");
                          public MsgBox()
                          {
                           setTitle("Notify");
                           setLocation(220,250);
                           setSize(350,150);
                           setResizable(false);
                           setLayout(null);
                           addWindowListener(new MWindow());
                           add(lblMsg);
                           add(cmdOK);
                           lblMsg.setBounds(25,15,330,90);
                           cmdOK.setBounds(130,110,80,30);
                           cmdOK.addActionListener(new cmdOK_Click());
                          }
                    /*-------------------------------------------------------*/ 
                        public class cmdOK_Click implements ActionListener 
                        {
                                public void actionPerformed(ActionEvent e)
                                {
                                 setVisible(false);
                                }
                        }
            /*-------------------------------------------------------*/ 
                        class  MWindow extends WindowAdapter
                        {
                            public void windowActivated(WindowEvent e)
                            {
                                 lblMsg.setText(MsgVal);
                                 repaint();
                                }
                                public void windowClosing(WindowEvent e)
                                {
                                 setVisible(false);
                                }
                                public void windowOpened(WindowEvent e)
                                {
                                 toFront();
                                }
                        }
                };                  //MsgBox Class Ends Here
                public static void Alert(String msg,String title)
                {
                  Msg2.setTitle(title);
                  MsgVal=msg;
                  Msg2.setVisible(true);
                }
        /*=================================================================*/
        static class MainFrame extends Frame
         {
               public MainFrame() //Constructor
                {
                                setTitle(LocalIP);//Setting Form Title
                                setLocation(0, 0);     /* Displaying Login Form */
                              setSize(800,600);
                                setLayout(null);       //Setting Layout of Frame      
                        addWindowListener(new XWindow());
                                        add(lblShow1);
                                        add(lblShow2);
                                        lblShow1.setBounds(50,50,700,200);
                                        lblShow1.setFont(new Font("Arial", Font.BOLD, 125));
                                        lblShow1.setAlignment(1);
                                        lblShow2.setBounds(50,220,700,200);
                                        lblShow2.setFont(new Font("Arial", Font.BOLD, 125));
                                        lblShow2.setAlignment(1);
                                        setMenuBar(mbar);
                        mnufile.add(mnuEnc =new MenuItem("Encrypt     "));
                        mnufile.add(mnuDec =new MenuItem("Decrypt     "));
                        mnufile.add(mnuExit=new MenuItem("Exit        "));
                                        mbar.add(mnufile);
                        mnuEnc.addActionListener(new mnuEncrypt_Click());
                        mnuDec.addActionListener(new mnuDecrypt_Click());
                        mnuExit.addActionListener(new mnuExit_Click());
                }       
                        /*-------------------------------------------------------*/ 
                        public class mnuEncrypt_Click implements ActionListener
                        {
                                public void actionPerformed(ActionEvent e)
                                {
                                 Main2.setVisible(false);
                                 Enc2.setVisible(true);
                                }
                        }
                        /*-------------------------------------------------------*/ 
                        public class mnuDecrypt_Click implements ActionListener
                        {
                                public void actionPerformed(ActionEvent e)
                                {
                                 Main2.setVisible(false);
                                 Dec2.setVisible(true);
                                }
                        }
                        /*-------------------------------------------------------*/ 
                        public class mnuExit_Click implements ActionListener
                        {
                                public void actionPerformed(ActionEvent e)
                                {
                                 Send("D "+LocalIP,txtServIP.getText(),777);
                                 System.exit(0);
                                }
                        }
                        /*-------------------------------------------------------*/ 
                        static class  XWindow extends WindowAdapter
                        {
                                public void windowClosing(WindowEvent e)
                                {
                                 Send("D "+LocalIP,txtServIP.getText(),777);
                                        System.exit(0);
                                }
                        }
         } //Main Frame Ends Here
        /*=================================================================*/
         static class EncFrame extends Frame
         {
               public EncFrame() //Constructor
                {
                                setTitle("E N C R Y P T I O N");//Setting Form Title
                                setLocation(0, 0);     /* Displaying Login Form */
                              setSize(800,600);
                                setLayout(null);       //Setting Layout of Frame      
                        addWindowListener(new ZWindow());
                                add(elblMain        );
                                add(elblUserList        );
                                add(elblRSAPubKey       );
                                add(elblOf                  );
                                add(elblLocalIP );
                                add(elblDESKey  );       
                                add(elblEDesKey );
                                add(elblPlainTxt);     
                                add(elblCyferTxt);     
                                add(ecmdEncDesKey);     
                                add(ecmdOpenFile        );
                                add(ecmdEnc          );
                                add(etxtRSAPubKey);     
                                add(etxtRSAnVal);
                                add(etxtRemoteIP        );
                                add(etxtLocalIP  );
                                add(etxtDESKey    );
                                add(etxtEDESKey  );
                                add(etxtPlainText       );
                                add(etxtCyferText       );
                                add(etxtUserList        );
                                add(ecmdSend);
                                etxtLocalIP.setText(LocalIP);
                                        elblMain.setFont(new Font("Arial", Font.BOLD, 30));
                                        elblMain.setAlignment(1);
                                        elblMain                .setBounds(27,22,724,46);
                                        elblUserList    .setBounds(36,75,115,16);
                                        elblRSAPubKey   .setBounds(198,99,91,19);
                                        elblOf      .setBounds(369,99,16,19);
                                        elblLocalIP          .setBounds(540,99,97,22);
                                        elblDESKey            .setBounds(198,135,136,16);
                                        elblEDesKey          .setBounds(198,195,163,16);
                                        elblPlainTxt    .setBounds(198,246,73,16);
                                        elblCyferTxt    .setBounds(510,246,235,16);
                                        ecmdEncDesKey   .setBounds(414,183,208,25);
                                        ecmdEncDesKey   .addActionListener(new EncDesKey_Click());
                                        ecmdOpenFile    .setBounds(336,243,97,22);
                                        ecmdOpenFile    .addActionListener(new OpenFile_Click());
                                        ecmdEnc   .setBounds(441,372,67,28);
                                        ecmdEnc   .addActionListener(new Enc_Click());
                                        etxtRSAPubKey   .setBounds(291,96,40,22); //73
                                        etxtRSAnVal          .setBounds(291+37,96,40,22);
                                        etxtRemoteIP    .setBounds(387,96,109,22);
                                        etxtLocalIP          .setBounds(642,96,109,22);
                                        etxtDESKey            .setBounds(198,156,553,22);
                                        etxtEDESKey          .setBounds(198,216,553,22);
                                        etxtPlainText   .setBounds(198,270,238,274);
                                        etxtCyferText   .setBounds(510,267,238,280);
                                        etxtUserList    .setBounds(36,96,115,446);
                                        etxtUserList    .addActionListener(new elstUserList_Click());                        
                                        ecmdSend                .setBounds(441,372+70,67,28);
                                        ecmdSend                .addActionListener(new Send_Click());
                 }
                        /*-------------------------------------------------------*/ 
                        public static String GenDesKey()  //Generating a Random 64bit DES-Key
                        {
                                String key="";
                                         int i;
                                         byte a[]=new byte [64];
                                         for(i=0;i<64;i++)
                                                a[i]=(byte)(48+(Math.floor((Math.random())*2)));
                                       
                                        for(i=0;i<64;i++)
                                                if(a[i]==49) key=key+"1"; else key=key+"0";

                                return(key);
                        }
                        public class Send_Click implements ActionListener
                        {
                                public void actionPerformed(ActionEvent e)
                                {
                                 int i; String kstr="",kstr1="",keystr="";
                                  Send("s "+LocalIP,etxtRemoteIP.getText(),666); //Sending LocalIP Address to Receiver
                                  /////////////////// Sending Encrypted Key
                                  Send("M ",etxtRemoteIP.getText(),666); //Start sending bytes
                                        char ch; byte ch1;
                                        keystr=etxtEDESKey.getText();
                                        for(i=0;i<keystr.length();i++)
                                        {
                                            ch=(keystr.charAt(i));
                                                ch1=(byte)ch;
                                                kstr1=kstr1.valueOf(ch1+128);
                                                Send("z "+kstr1,etxtRemoteIP.getText(),666); //Sending Bytes
                                        }       
                                        Send("m ",etxtRemoteIP.getText(),666); //Stop sending byte            &