Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I gotta write a simple client server chat program for me college assignment. I need to use a key to encrypt and decrypt the message. However I am not able to send and receive the encrypted string. Help! Thanks!

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question I gotta write a simple client server chat program for me college assignment. I need to use a key to encrypt and decrypt the message. However I am not able to send and receive the encrypted string. Help! Thanks!

    //Server

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.util.Arrays;
    import java.io.*;
    import java.net.*;

    class MyServer implements Runnable
    {

    //Declare class variables to be used
    int i,j=0,tmp,t,k=0,l;
    int[] P={1,2,3,4};
    int[] K={1,1,1,1};
    int[] C={0,0,0,0};
    int[] D={0,0,0,0};
    int[] S={0,1,2,3,4,5,6,7};
    int[] T={1,1,1,1,1,1,1,1};
    char x[]=new char[4];
    char y[]=new char[4];
    char a,b;
    String sent="",received=null;
    ObjectInputStream ois=null;
    ObjectOutputStream oos=null;
    BufferedReader br=null;
    ServerSocket s=null;


    public void run()
    {
    try
    {
    //1. Create a server socket
    s=new ServerSocket(1025,5);

    System.out.println("Client-Server Chat \n-----------------------------------------------------------\nPress Alt+F4 to quit this application.");

    //2. Wait for connection
    System.out.println("\nWaiting for connection from Client");
    Socket connection=s.accept();
    System.out.println("Connection received from "+connection.getInetAddress().getHostName()+":"+co nnection.getInetAddress());
    System.out.println("Waiting for response.. ");


    //3. Get Input and Output streams
    oos=new ObjectOutputStream(connection.getOutputStream());
    oos.flush();
    ois=new ObjectInputStream(connection.getInputStream());


    br=new BufferedReader(new InputStreamReader(System.in));



    while(true)
    {
    receiveMessage();

    System.out.println("Enter message: ");
    sendMessage();

    System.out.println("Waiting for response.. ");
    }
    }

    catch(Exception e)
    {
    System.out.println(e);
    }

    }

    void receiveMessage()
    {
    try
    {

    received=(String)ois.readObject();
    if(k==0)
    {
    l=received.length();
    for(i=0;i<l;i++)
    {
    b=received.charAt(i);
    P[i]=(int)b; // converting the string into integer data for ciphering
    }
    for(i=0;i<8;i++)
    {
    j=(j+S[i]+T[i])%8;
    tmp=S[j];
    S[j]=S[i];
    S[i]=tmp;
    }
    i=0; j=0;
    for(i=0;i<8;i++)
    {
    i=(i+1)%8;
    j=(j+S[i])%8;
    tmp=S[j];
    S[j]=S[i];
    S[i]=tmp;
    t=(S[i]+S[j])%8;
    k=S[t];
    if(i<4) K[i]=k; // generating the cipher key
    }
    for(i=0;i<l;i++)
    {
    C[i]=P[i]^K[i]; // ciphering the data with the key
    }
    for(i=0;i<l;i++)
    {
    x[i]=(char)C[i];
    }
    }
    else
    {
    for(i=0;i<l;i++)
    {
    C[i]=P[i]^K[i]; // ciphering the data with the key
    }
    for(i=0;i<l;i++)
    {
    x[i]=(char)C[i];
    }
    }
    String code=new String(x); // the ciphered text that is sent
    System.out.println("\nClient->");
    System.out.println(code);

    }

    catch(Exception e)
    {
    System.out.println(e);

    }

    }

    void sendMessage()
    {
    try
    {
    System.out.println("(Type DOT(.) and press enter to terminate the message)");
    String temp=null;
    sent="";

    while(true)
    {
    temp=br.readLine();
    if(temp.equalsIgnoreCase("."))
    break;
    else
    {
    l=temp.length();
    for(i=0;i<l;i++)
    {
    b=temp.charAt(i);
    P[i]=(int)b; // converting the string into integer data for ciphering
    }
    for(i=0;i<8;i++)
    {
    j=(j+S[i]+T[i])%8;
    tmp=S[j];
    S[j]=S[i];
    S[i]=tmp;
    }
    i=0; j=0;
    for(i=0;i<8;i++)
    {
    i=(i+1)%8;
    j=(j+S[i])%8;
    tmp=S[j];
    S[j]=S[i];
    S[i]=tmp;
    t=(S[i]+S[j])%8;
    k=S[t];
    if(i<4) K[i]=k; // generating the cipher key
    }
    for(i=0;i<l;i++)
    {
    C[i]=P[i]^K[i]; // ciphering the data with the key
    }
    for(i=0;i<l;i++)
    {
    x[i]=(char)C[i];
    }
    String code=new String(x); // the ciphered text that is sent
    sent=sent+code+"\n";
    }
    }
    oos.writeObject(sent);
    oos.flush();
    }

    catch(Exception e)
    {
    System.out.println(e);

    }
    }

    public static void main(String args[])
    {

    MyServer s=new MyServer();
    s.run();
    }
    }


    //Client

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.util.Arrays;
    import java.io.*;
    import java.net.*;

    class MyClient
    {
    //Declare class variables to be used
    int i,j=0,tmp,t,k=0,l;
    int[] P={1,2,3,4};
    int[] K={1,1,1,1};
    int[] C={0,0,0,0};
    int[] D={0,0,0,0};
    int[] S={0,1,2,3,4,5,6,7};
    int[] T={1,1,1,1,1,1,1,1};
    char x[]=new char[4];
    char y[]=new char[4];
    char a,b;
    String sent="",received=null;
    ObjectInputStream ois=null;
    ObjectOutputStream oos=null;
    BufferedReader br=null;
    Socket s=null;

    public void runCli()
    {

    try
    {
    s=new Socket("localhost",1025);

    System.out.println("Client-Server Chat \n-----------------------------------------------------------\nPress Alt+F4 to quit this application.");

    System.out.println("\nSuccessfully connected to Server-"+s.getInetAddress()+":"+s.getLocalPort()+"\n" );
    ois=new ObjectInputStream(s.getInputStream());
    oos=new ObjectOutputStream(s.getOutputStream());
    br=new BufferedReader(new InputStreamReader(System.in));



    while(true)
    {
    System.out.println("Enter message: ");
    sendMessage();

    System.out.println("Waiting for response.. ");
    receiveMessage();


    }
    }

    catch(Exception e)
    {
    System.out.println(e);
    }
    }

    void sendMessage()
    {
    try
    {
    System.out.println("(Type DOT(.) and press enter to terminate the message)");
    String temp=null;
    sent="";

    while(true)
    {
    temp=br.readLine();
    if(temp.equalsIgnoreCase("."))
    break;
    else
    {
    l=temp.length();
    for(i=0;i<l;i++)
    {
    b=temp.charAt(i);
    P[i]=(int)b; // converting the string into integer data for ciphering
    }
    for(i=0;i<8;i++)
    {
    j=(j+S[i]+T[i])%8;
    tmp=S[j];
    S[j]=S[i];
    S[i]=tmp;
    }
    i=0; j=0;
    for(i=0;i<8;i++)
    {
    i=(i+1)%8;
    j=(j+S[i])%8;
    tmp=S[j];
    S[j]=S[i];
    S[i]=tmp;
    t=(S[i]+S[j])%8;
    k=S[t];
    if(i<4) K[i]=k; // generating the cipher key
    }
    for(i=0;i<l;i++)
    {
    C[i]=P[i]^K[i]; // ciphering the data with the key
    }
    for(i=0;i<l;i++)
    {
    x[i]=(char)C[i];
    }
    String code=new String(x); // the ciphered text that is sent

    sent=sent+code+"\n";
    }
    }
    oos.writeObject(sent);
    oos.flush();
    }

    catch(Exception e)
    {
    System.out.println(e);

    }
    }

    void receiveMessage()
    {
    try
    {
    received=(String)ois.readObject();

    if(k==0)
    {
    l=received.length();
    for(i=0;i<l;i++)
    {
    b=received.charAt(i);
    P[i]=(int)b; // converting the string into integer data for ciphering
    }
    for(i=0;i<8;i++)
    {
    j=(j+S[i]+T[i])%8;
    tmp=S[j];
    S[j]=S[i];
    S[i]=tmp;
    }
    i=0; j=0;
    for(i=0;i<8;i++)
    {
    i=(i+1)%8;
    j=(j+S[i])%8;
    tmp=S[j];
    S[j]=S[i];
    S[i]=tmp;
    t=(S[i]+S[j])%8;
    k=S[t];
    if(i<4) K[i]=k; // generating the cipher key
    }
    for(i=0;i<l;i++)
    {
    C[i]=P[i]^K[i]; // ciphering the data with the key
    }
    for(i=0;i<l;i++)
    {
    x[i]=(char)C[i];
    }
    }
    else
    {
    for(i=0;i<l;i++)
    {
    C[i]=P[i]^K[i]; // deciphering the data with the key
    }
    for(i=0;i<l;i++)
    {
    x[i]=(char)C[i];
    }
    }
    String code=new String(x);
    System.out.println("\nServer->");
    System.out.println(code);

    }

    catch(Exception e)
    {
    System.out.println(e);

    }

    }

    public static void main(String args[])
    {
    MyClient c1=new MyClient();
    c1.runCli();
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I gotta write a simple client server chat program for me college assignment. I need to use a key to encrypt and decrypt the message. However I am not able to send and receive the encrypted string. Help! Thanks!

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    I am not able to send and receive the encrypted string
    Can you explain what the problem is?
    Can you send and receive any String?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Muliple Client Server chat application..how to send message from server..
    By jesroni in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2013, 11:17 PM
  2. Simple string encrypt and decrypt
    By mds1256 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 9th, 2013, 07:47 AM
  3. my encrypting simple client to server program unable to get the key from client
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 07:15 AM
  4. Simple client-server chat program
    By Saloni Patil in forum Java Networking
    Replies: 3
    Last Post: October 22nd, 2011, 09:29 AM
  5. UDP Server Client program to send and receive messages
    By Koren3 in forum Java Networking
    Replies: 1
    Last Post: September 5th, 2011, 10:16 AM

Tags for this Thread