Encrypted Client/Server


The Client

The Server

The Code

import java.net.*;
import java.util.*;
import java.io.*;

public class encrypted_byteClient {
    public static void main(String args[]) {
        try {
            Socket c_sock;
            Akeystore16 K=new Akeystore16("mykey.dat");
            String the_message=K.Encode("Code Me.");
            byte[] the_bytes=the_message.getBytes("ISO-8859-1");
            InetAddress ip = InetAddress.getByName("localhost");
            try {
                c_sock = new Socket(ip, 5000);
            } catch (Exception e1) {
                System.out.println("Port  not Available");
                return;
            }
            DataOutputStream dataOut = new DataOutputStream(c_sock.getOutputStream());
            dataOut.writeInt(the_bytes.length); // write length of the message
            dataOut.write(the_bytes);           // write the message
            System.out.println("the_bytes length Written= "+the_bytes.length);                      //
        } catch (Exception e) {
        }
    }
}
///////////////////////////////////////////////////////////////
import java.net.*;
import java.util.*;
import java.io.*;
public class encrypted_byteServer {
    public static void main(String args[]) {
        try {
            Akeystore16 K=new Akeystore16("mykey.dat");
            ServerSocket s_socket = new ServerSocket(5000);
            Socket sock = s_socket.accept();
            DataInputStream dataIn = new DataInputStream(sock.getInputStream());
            int length = dataIn.readInt();                    // read length of incoming message
            byte[] the_bytes = new byte[length];
            dataIn.readFully(the_bytes, 0, length); // read the message
            String the_string = new String(the_bytes,"ISO-8859-1");
            System.out.println( "Received undecoded: "+the_string );
            System.out.println(K.Decode(the_string));
        } catch (Exception e) {
            System.out.println(e);
            return;
        }
    }
}