Hi, I want to create a Server so it read the data form a file and then encrypte it Using AES encryption and then send it to the client, the Client code is provided by my teacher, I need to write the Server side I am posting what i have done with server yet and the client code hope to get help. thanks in advance
Client Code
Code:package networksecurity; /** * DJL based on CoreJava: 1/2008 * Modified 2/2009 */ import java.io.*; import java.net.*; import java.util.*; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; public class SFClient { final static String filename = "testout.txt"; public static void main(String[] args) { try { Socket s = new Socket("localhost",8187); try { OutputStream outs = s.getOutputStream(); InputStream ins = s.getInputStream(); Scanner sc = new Scanner(System.in); System.out.println("Enter your password"); String pwd = sc.next(); MessageDigest md1 = MessageDigest.getInstance("MD5"); byte[] phash = md1.digest(pwd.getBytes()); outs.write(phash); SecretKeySpec keySpec = new SecretKeySpec(phash,"AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(cipher.DECRYPT_MODE, keySpec); MessageDigest md2 = MessageDigest.getInstance("SHA-1"); FileOutputStream fouts = new FileOutputStream(filename); byte[] hashcode = crypt(ins, fouts, cipher, md2); ins.close(); fouts.close(); printHashCode(hashcode); } finally { s.close(); } } catch (Exception e) { System.out.println("Error" + e); e.printStackTrace(); } } public static byte[] crypt(InputStream in, OutputStream out, Cipher cipher, MessageDigest hash) throws IOException, GeneralSecurityException { int blockSize = cipher.getBlockSize(); int outputSize = cipher.getOutputSize(blockSize); byte[] inBytes = new byte[blockSize]; byte[] outBytes = new byte[outputSize]; int inLength = 0; int outLength = 0; boolean more = true; while (more) { inLength = in.read(inBytes); if (inLength == blockSize) { outLength = cipher.update(inBytes, 0, blockSize, outBytes); hash.update(outBytes, 0, outLength); out.write(outBytes, 0, outLength); } else more = false; } if (inLength > 0) outLength = cipher.doFinal(inBytes, 0, inLength, outBytes); else outLength = cipher.doFinal(outBytes, 0); hash.update(outBytes, 0, outLength); //** had this last line wrong //out.write(outBytes); out.write(outBytes, 0, outLength); return hash.digest(); } public static void printHashCode(byte[] hashcode) { String d = ""; for(int i = 0; i < hashcode.length; i++) { int v = hashcode[i] & 0xFF; if (v < 16) d += " "; d += Integer.toString(v, 16).toUpperCase() + " "; } System.out.println("Digest of received file is:"); System.out.println(d); } }
and this is my Server Code so far:
I created the method readFile, and den called this method in main, and now i want to encrypte it and then send it to the client, but I am stuckCode:package networksecurity; /** version 1.0 15.02.2010 author: Harris B */ import java.io.*; import java.net.*; import java.security.GeneralSecurityException; import java.security.MessageDigest; import java.util.*; import javax.crypto.Cipher; /** This program makes a socket connection to the atomic clock in Boulder, Colorado, and prints the time that the server sends. */ public class Server { public static void main(String[] args) { try { //Create a Socket in listen to connection at port 8187 ServerSocket sock = new ServerSocket(8187); try { Socket newsock = sock.accept(); //Stream to recieve clients data. DataInputStream inp = new DataInputStream(newsock.getInputStream()); Scanner in = new Scanner(inp); //Stream to write data to client. PrintStream outp = new PrintStream(newsock.getOutputStream()); ReadFile(); String Sendfile = ReadFile(); outp.write(ReadFile); while (in.hasNextLine()) { String line = in.nextLine(); System.out.println(line); } } finally { sock.close(); } } catch (IOException e) { e.printStackTrace(); } } //methode to read from file public static void ReadFile() { DataInputStream is = null; byte ch; try { is = new DataInputStream(new FileInputStream("D:\\MyText.txt")); while (true) { // exception deals catches EOF ch = is.readByte(); System.out.print((char) ch); System.out.flush(); } } catch (FileNotFoundException noFile) { System.err.println("File not found! " + noFile); } catch (IOException io) { System.err.println("I/O error occurred: " + io); } catch (Throwable anything) { System.err.println("Abnormal exception caught !: " + anything); } finally { if (is != null) { try { is.close(); } catch (IOException ignored) { } } } } }![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks