Reputation: 1
Trying to write a client/server program where the client reads a text file and sends it to the server sockect using CipherOutputStream. The expected text file is created but empty and I the following error
Read Length-1
EOF:null
I have this method encrypt() that does the encrytion and then sends the out the data
private static void encrypt(InputStream is, OutputStream os) {
try {
byte[] buf = new byte[1024];
// bytes at this stream are first encoded
os = new CipherOutputStream(os, ecipher);
// read in the clear text and write to out to encrypt
int numRead = 0;
while ((numRead = is.read(buf)) >= 0) {
os.write(buf, 0, numRead);
}
// close all streams
os.close();
} catch (IOException e) {
System.out.println("I/O Error:" + e.getMessage());
}
}
Below is most of the code at the client end
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
File file = fc.getSelectedFile();
Socket s = null;
s = new Socket("localhost", 6880);
DataOutputStream output = new DataOutputStream(s.getOutputStream());
encrypt(new FileInputStream(file), output);
log.append("encrypted " + newline);
log.append("Sent" + file.getName() + "." + newline);
} catch (Exception ex) {
Logger.getLogger(FileChooserDemo.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
log.append("Open command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
//Handle save button action.
} else if (e.getSource() == saveButton) {
int returnVal = fc.showSaveDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would save the file.
log.append("Saving: " + file.getName() + "." + newline);
} else {
log.append("Save command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
}
}
And then the listening server reads in the data using CipherInputStream and then writes that to a text file. The server contains the following
private static void decrypt(InputStream is, OutputStream os) {
try {
byte[] buf = new byte[1024];
// bytes read from stream will be decrypted
CipherInputStream cis = new CipherInputStream(is, dcipher);
// read in the decrypted bytes and write the clear text to out
int numRead = 0;
while ((numRead = cis.read(buf)) >= 0) {
os.write(buf, 0, numRead);
}
// close all streams
cis.close();
is.close();
os.close();
} catch (IOException e) {
System.out.println("I/O Error:" + e.getMessage());
}
}
public void run() {
try {
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
decrypt(input, new FileOutputStream("cleartext-reversed.txt"));
FileWriter out = new FileWriter("test.txt");
BufferedWriter bufWriter = new BufferedWriter(out);
System.out.println("receive from : "
+ clientSocket.getInetAddress() + ":"
+ clientSocket.getPort());
//Step 1 read length
int nb = input.read();
System.out.println("Read Length" + nb);
String enctext = Character.toString(input.readChar());
Integer.toString(nb);
//Step 2 read byte
String st = new String("see if it can write");
bufWriter.append(enctext);
bufWriter.close();
//Step 1 send length
output.writeInt(st.length());
//Step 2 send length
output.writeBytes(st); // UTF is a string encoding
// output.writeUTF(data);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidAlgorithmParameterException ex) {
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
} catch (EOFException e) {
System.out.println("EOF:" + e.getMessage());
} catch (IOException e) {
System.out.println("IO:" + e.getMessage());
} finally {
try {
clientSocket.close();
} catch (IOException e) {/*close failed*/
}
}
}
Upvotes: 0
Views: 967
Reputation: 691625
The server does the following:
decrypt(input, new FileOutputStream("cleartext-reversed.txt"));
which reads everything from the input stream, decrypts it, and writes the result to the text file and closes the input stream.
Then right after, you're trying to do
int nb = input.read();
...
input.readChar()
which thus tries to read again from the input stream, that has just been read completely and closed.
Note: the diagnostic would be much easier if, instead of hiding the exception behind
System.out.println("EOF:" + e.getMessage());
you did
e.printStackTrace();
which would tell you what exception it is, and where it happens.
Upvotes: 1