I have a client which is supposed to download an image file encrypted with AES (I'm not sure if I'm even implementing this correctly) from a server. However, with the following codes, the server gets stuck at the "Sending image" print message without moving on to the "Done" print message. In other words, I can't download the image file. Why is this so?
Server.java
public class Server { public static void main (String [] args ) throws InvalidKeyException, IOException, Exception { ServerSocket serverSocket = new ServerSocket(15123); Socket socket = null; //start of AES //Generate AES Key int sizeofkey = 128; KeyGenerator kg = KeyGenerator.getInstance("AES"); //initialize with sizeofkey kg.init(sizeofkey); Key mykey = kg.generateKey(); System.out.println("AES Key Generated"); //create cipher object & initialize with generated key Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, mykey); while(true) { socket = serverSocket.accept(); System.out.println("Accepted connection : " + socket.getRemoteSocketAddress().toString() + " <-> /127.0.0.1:15123" ); OutputStream sos = socket.getOutputStream(); // get the image from a webcam URL myimage = new URL("http://183.76.13.58:80/SnapshotJPEG?Resolution=640x480"); //Picture in string format //String plainpic = null; //Picture in byte array format byte[] plainpic = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream is = null; try { //in = new DataInputStream(myimage.openStream()); is = myimage.openStream (); byte[] byteChunk = new byte[4096]; int n; while ( (n = is.read(byteChunk)) > 0 ) { baos.write(byteChunk, 0, n); } //Picture in string format //plainpic=Base64.getUrlEncoder().encodeToString(baos.toByteArray()); //Picture in byte array format plainpic=baos.toByteArray(); } catch (Exception ee) { System.out.println("Check internet connection please"); socket.close(); return; } byte[] cipherpic = cipher.doFinal(plainpic); DateFormat dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss"); Date date = new Date(); System.out.println("Sending image " + dateFormat.format(date)); try { while (true) { sos.write(cipherpic); } } catch (EOFException ee) { System.out.println("-------------- Done ----------"); is.close(); } sos.flush(); sos.close(); socket.close(); } } }
Client.java
public class Client { public static void main(String [] args) throws IOException { String fname = "image.jpg"; SSLSocket sslsocket = new Socket("127.0.0.1",15123); DataInputStream in = null; try{ in = new DataInputStream(sslsocket.getInputStream()); } catch (Exception ee) { System.out.println("Check connection please"); sslsocket.close(); return; } FileOutputStream fos = new FileOutputStream(fname); try { while (true) fos.write(in.readByte()); } catch (EOFException ee) { System.out.println("File transfer complete"); in.close(); } fos.flush(); fos.close(); sslsocket.close(); public static String asHex (byte buf[]) { //Obtain a StringBuffer object StringBuffer strbuf = new StringBuffer(buf.length * 2); int i; for (i = 0; i < buf.length; i++) { if (((int) buf[i] & 0xff) < 0x10) strbuf.append("0"); strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); } // Return result string in Hexadecimal format return strbuf.toString(); } }