import java. io.*; import java. net.*; import java.math.*; import java.util.*; /** * This class is a simple server. Its port is set to 6502 on * the machine it is running on. It does not use threads but should. * * * This server reads a series of BigInteger objects from the client. * It is assumed that each BigInteger object was constructed on the client in the following * way: * * 1) A block of bytes was read from a file (or other location) on the * client's machine. This block of bytes was of size >= 1 and <= 10. * 2) The number of bytes read was prepended to this block of bytes and an * array of bytes was formed. For example, suppose the bytes we read from the * input file were 65 66 67 ('A', 'B', and 'C' in ASCII). We would form the * array of bytes x formatted as follows: * x[0] = 3 * x[1] = 65 * x[2] = 66 * x[3] = 67 * 3) The array of bytes was then converted to a BigInteger using Java's * BigInteger constructor that accepts an array of bytes as input. * 4) The BigInteger plaintext was then enciphered using RSA with a public * key. * * The server decodes each BigInteger object producing a decoded BigInteger. The * resulting BigInteger is converted to an array of bytes using Java's toByteArray() * method for BigIntegers. The first byte of this array specifies the number of bytes * that follow. The server writes out each of these bytes, excluding the first, so * that the file is the same as that found on the client's machine. * * Please use this class to test your client. On a later date, you will be given * a public key and be asked to encrypt binary and ASCII files and send them to * a server on the internet. **/ public class Server { // Choose a port outside of the range 1- 1024: static final int port = 6502; public static void main( String[] args ) throws IOException { if(args.length != 1) { System.out.println("Usage: java Server filename"); System.exit(-1); } KeyGen myGen = new KeyGen(); Decoder myDecoder = new Decoder(myGen.getd(),myGen.getn()); ServerSocket s = new ServerSocket(port); int fileCtr = 0; try { boolean flush = true; System. out. println(" Server Started: " + s); while(true) { // Blocks until a connection occurs: Socket socket = s.accept(); System. out. println( "Connection accepted, socket: "+ socket); ObjectInputStream inBig = new ObjectInputStream( socket.getInputStream()); DataOutputStream outBytes = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(args[0]+fileCtr))); System.out.println("Writing to " + args[0] + fileCtr); try { while(true) { // read BigInteger c BigInteger c = (BigInteger)inBig.readObject(); // decode c to m = c^d mod n // byte 0 will have the size of live bytes byte[] m = myDecoder.decode(c); for(int index = 1; index <= m[0]; index++){ outBytes.write(m[index]); } } } catch(EOFException finish) { System.out.println("Finished collecting BigIntegers"); } finally { System.out.println("Connection complete"); System.out.println("File " + args[0] + fileCtr + " written."); fileCtr++; outBytes.close(); inBig.close(); } } } catch (Exception e) { System.out.println("Oh No It's Crypto!"); e.printStackTrace(); } } }