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 12 of 12

Thread: Cipher Client/Server Encryption/Decryption Program - Please Help

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

    Default Cipher Client/Server Encryption/Decryption Program - Please Help

    import java.io.*;
    import java.net.*;
    import java.security.*;
    import javax.crypto.*;
     
    public class CipherClient
    {
    	public static void main(String[] args) throws Exception 
    	{
    		// -Generate a DES key.
    		Key key1;
    		KeyGenerator kg = KeyGenerator.getInstance("DES");
    		kg.init(new SecureRandom());
    		key1 = kg.generateKey();
     
                    String message = "The quick brown fox jumps over the lazy dog.";
    		String host = "127.0.0.1";
    		int port = 7999;
    		Socket s = new Socket(host, port);            
     
                    // -Store it in a file.
    		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Keyfile3.txt"));
    		out.writeObject(key1);
                    out.close();
     
    		// -Use the key to encrypt the message above and send it over socket s to the server.
    		Cipher cipher = Cipher.getInstance("DES");
    		cipher.init(Cipher.ENCRYPT_MODE, key1);
    		CipherOutputStream cipherout = new CipherOutputStream(s.getOutputStream(),cipher);
                    cipherout.write(message.getBytes());
     
    	}
    }
    import java.io.*;
    import java.net.*;
    import java.security.*;
    import javax.crypto.*;
     
    public class CipherServer
    {
    	public static void main(String[] args) throws Exception 
    	{
    		int port = 7999;
    		ServerSocket server = new ServerSocket(port);
    		Socket s = server.accept();
     
    		// -Read the key from the file generated by the client.
    		ObjectInputStream in = new ObjectInputStream(new FileInputStream("Keyfile3.txt"));
    		Key key1 = (Key)in.readObject();
     
    		// -Use the key to decrypt the incoming message from socket s.	
    		Cipher cipher = Cipher.getInstance("DES");
    		cipher.init(Cipher.DECRYPT_MODE, key1);
    		CipherInputStream cipherIn = new CipherInputStream(s.getInputStream(), cipher);
     
    		// -Print out the decrypt String to see if it matches the original message.
    		FileOutputStream fos = new FileOutputStream("output3.txt");
     
    		byte[] buffer = new byte[1024];
                    int length = cipherIn.read(buffer);
                    fos.write(buffer, 0, length);
                    fos.flush();
                    fos.close();
                    cipherIn.close();
    	}
    }

    Errors that I'm getting on CipherClient:
    run:
    Exception in thread "main" java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.pee kByte(ObjectInputStream.java:2577)
    at java.io.ObjectInputStream.readObject0(ObjectInputS tream.java:1315)
    at java.io.ObjectInputStream.readObject(ObjectInputSt ream.java:369)
    at CipherServer.main(CipherServer.java:16)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 8 seconds)


    Errors that I get when running on CipherServer:
    run:
    Exception in thread "main" java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutp utStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStre am.java:141)
    at javax.crypto.CipherOutputStream.write(CipherOutput Stream.java:157)
    at javax.crypto.CipherOutputStream.write(CipherOutput Stream.java:141)
    at CipherClient.main(CipherClient.java:30)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cipher Client/Server Encryption/Decryption Program - Please Help

    What is in the .txt file that the readObject() method is trying to read?
    If the Server stops executing at the exception there will be nothing for the client to read.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cipher Client/Server Encryption/Decryption Program - Please Help

    My intent is for the key to be read from:

    out.writeObject(key1);

    after the key has been made and stored.

    Keyfile3.txt

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cipher Client/Server Encryption/Decryption Program - Please Help

    How is the program tested? In What order are the programs executed?
    How does the program make sure the file is written before it is read?
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    drwatson (April 15th, 2013)

  6. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cipher Client/Server Encryption/Decryption Program - Please Help

    Server executed first, and client afterwards.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cipher Client/Server Encryption/Decryption Program - Please Help

    If the .txt file must exist before the server can execute, why does the client write it?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cipher Client/Server Encryption/Decryption Program - Please Help

    IC - didn't think of that till just now.

    I have the client looking at the server for the socket, so if I reverse it will error out too.

    Any recommendations?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cipher Client/Server Encryption/Decryption Program - Please Help

    Send the key over the socket before sending the encrypted message. Don't use a disk file to pass the key.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cipher Client/Server Encryption/Decryption Program - Please Help

    With "KeyFile3.ser"? I sorry to bother you, but I'm lost.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cipher Client/Server Encryption/Decryption Program - Please Help

    Don't write the Keyfile3.txt file to disk. Send its contents via a socket.

    What if the client and server were on different PCs? The posted code requires that the client write the file before the server reads it. That is Not possible if they are on different PCs.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cipher Client/Server Encryption/Decryption Program - Please Help

    I do want the client to write the key before server reading it.

    Client - writes key on server to keyfile.txt ==> uses keyfile.txt from server to encrypt message string ==> creating cipherstream
    Server - accept socket ==> accept keyfile.txt ==> uses keyfile.txt from server to decrypt message string ==> write output from original string

    I'm probably thinking about this all wrong, but that's my intent of the program.

    Thank you!

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cipher Client/Server Encryption/Decryption Program - Please Help

    Client - writes key on server
    How does it do that if the client and server are running on different PCs?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: March 28th, 2013, 06:27 AM
  2. 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
  3. Simple encryption/ decryption problem
    By searchformeaning in forum Loops & Control Statements
    Replies: 2
    Last Post: May 7th, 2012, 01:34 AM
  4. Java encryption and decryption
    By frozen java in forum Java Theory & Questions
    Replies: 2
    Last Post: December 4th, 2011, 04:01 PM
  5. Client-Server program
    By Reztem in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 12th, 2010, 05:36 PM