CipherInputStream hangs












0















I am attempting to use encryption to communicate over sockets using java. I've successfully communicated with sockets without encryption, but when I try with encryption the program freezes.



Here is the parent Connection class that works just fine:



public class Connection implements Runnable
{
protected Socket socket;
protected ObjectInputStream objectInputStream;
protected ObjectOutputStream objectOutputStream;
protected Thread listeningThread;
protected Thread dispatchThread;
protected boolean listen;
protected ArrayBlockingQueue<Object> readingQueue;
protected ConnectionListener connectionListener;

public Connection()
{
listen = true;
readingQueue = new ArrayBlockingQueue<Object>(10);
}

public Connection(Socket socket, ConnectionListener listener)
{
listen = true;
connectionListener = listener;
readingQueue = new ArrayBlockingQueue<Object>(10);

this.socket = socket;

try
{
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectInputStream = new ObjectInputStream(socket.getInputStream());
}
catch (IOException e)
{
e.printStackTrace();
}

startConnection();
}


Here is the child class that uses Encryption:



public class EncryptedConnection extends Connection
{
private Key key;
private Cipher cipherEncryption;
private Cipher cipherDecryption;

public EncryptedConnection(Socket socket, ConnectionListener listener, byte keydata)
{
super();
super.socket = socket;
super.connectionListener = listener;

try
{
key = new SecretKeySpec(keydata, "AES");
cipherEncryption = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherDecryption = Cipher.getInstance("AES/CBC/PKCS5Padding");

byte iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IvParameterSpec ivspec = new IvParameterSpec(iv);

cipherEncryption.init(Cipher.ENCRYPT_MODE, key, ivspec);
cipherDecryption.init(Cipher.DECRYPT_MODE, key, ivspec);

objectOutputStream = new ObjectOutputStream(new CipherOutputStream(socket.getOutputStream(),cipherEncryption));
objectInputStream = new ObjectInputStream(new CipherInputStream(socket.getInputStream(),cipherDecryption));
//The hanging or freezing occurs on the above line of code

}
catch(Exception e)
{

}


Here is the Server code that creates the socket:



@Override
public void run()
{
try
{
while(true)
{
Socket s = serverSocket.accept();

byte key = new byte[16];
for(int i=0;i<key.length;i++)
key[i] = 0x01;


EncryptedConnection c = new EncryptedConnection(s,connectionListener,key);

connections.add(c);
System.out.println("New Connection Established From"+s.getInetAddress().toString());
}
}
catch(java.net.SocketException e)
{
System.out.println("Listening thread terminated with exception.");
}
catch(IOException e)
{

e.printStackTrace();
}
}


And here is the Client code that creates the socket:



@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == connect)
{
try
{
Socket s = new Socket(ipBox.getText(), Integer.parseInt(portBox.getText()));
byte key = new byte[16];
for(int i=0;i<key.length;i++)
key[i] = 0x01;
EncryptedConnection c = new EncryptedConnection(s,parent,key);

parent.connectionSuccessful(c);
}
catch (NumberFormatException e1)
{
JOptionPane.showMessageDialog(this, "Error! Port number must be a number", "Error", JOptionPane.ERROR_MESSAGE);
}
catch (UnknownHostException e1)
{
JOptionPane.showMessageDialog(this, "Error! Unable to find that host", "Error", JOptionPane.ERROR_MESSAGE);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}


I have viewed this post, but did not find it helpful.
ObjectInputStream with CipherInputStream freezing, hanging
I've also tried different AES encryption modes with padding on and off but I get the same result.



Here is sample code that works just fine. I am essentially doing the same thing but instead of files, using sockets.



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class IOTest
{
public static void main(String args)
{
FileOutputStream fos = null;
FileInputStream fis = null;
CipherInputStream cis = null;
CipherOutputStream cos = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Key key = null;
Cipher cipherD = null;
Cipher cipherE = null;
byte keydata = new byte[16];
byte iv = new byte[16];
IvParameterSpec ivspect = new IvParameterSpec(iv);

try
{
key = new SecretKeySpec(keydata,"AES");
cipherE = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherD = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherE.init(Cipher.ENCRYPT_MODE, key,ivspect);
cipherD.init(Cipher.DECRYPT_MODE, key, ivspect);

fos = new FileOutputStream("hello.data");
cos = new CipherOutputStream(fos,cipherE);
oos = new ObjectOutputStream(cos);

oos.writeObject(new String("H"));
oos.flush();
oos.close();


fis = new FileInputStream("hello.data");
cis = new CipherInputStream(fis, cipherD);
ois = new ObjectInputStream(cis);

String s = ois.readObject().toString();
System.out.println(s);

ois.close();

}
catch(Exception e)
{

}
}
}









share|improve this question




















  • 1





    What does the code on the other side of the Connection look like?

    – Erwin Bolwidt
    Nov 13 '18 at 2:44











  • I have edited the question with the information you requested.

    – Matthew
    Nov 13 '18 at 3:10











  • I must have missed it because I don't see any code that reads or writes anything.

    – James K Polk
    Nov 13 '18 at 4:41
















0















I am attempting to use encryption to communicate over sockets using java. I've successfully communicated with sockets without encryption, but when I try with encryption the program freezes.



Here is the parent Connection class that works just fine:



public class Connection implements Runnable
{
protected Socket socket;
protected ObjectInputStream objectInputStream;
protected ObjectOutputStream objectOutputStream;
protected Thread listeningThread;
protected Thread dispatchThread;
protected boolean listen;
protected ArrayBlockingQueue<Object> readingQueue;
protected ConnectionListener connectionListener;

public Connection()
{
listen = true;
readingQueue = new ArrayBlockingQueue<Object>(10);
}

public Connection(Socket socket, ConnectionListener listener)
{
listen = true;
connectionListener = listener;
readingQueue = new ArrayBlockingQueue<Object>(10);

this.socket = socket;

try
{
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectInputStream = new ObjectInputStream(socket.getInputStream());
}
catch (IOException e)
{
e.printStackTrace();
}

startConnection();
}


Here is the child class that uses Encryption:



public class EncryptedConnection extends Connection
{
private Key key;
private Cipher cipherEncryption;
private Cipher cipherDecryption;

public EncryptedConnection(Socket socket, ConnectionListener listener, byte keydata)
{
super();
super.socket = socket;
super.connectionListener = listener;

try
{
key = new SecretKeySpec(keydata, "AES");
cipherEncryption = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherDecryption = Cipher.getInstance("AES/CBC/PKCS5Padding");

byte iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IvParameterSpec ivspec = new IvParameterSpec(iv);

cipherEncryption.init(Cipher.ENCRYPT_MODE, key, ivspec);
cipherDecryption.init(Cipher.DECRYPT_MODE, key, ivspec);

objectOutputStream = new ObjectOutputStream(new CipherOutputStream(socket.getOutputStream(),cipherEncryption));
objectInputStream = new ObjectInputStream(new CipherInputStream(socket.getInputStream(),cipherDecryption));
//The hanging or freezing occurs on the above line of code

}
catch(Exception e)
{

}


Here is the Server code that creates the socket:



@Override
public void run()
{
try
{
while(true)
{
Socket s = serverSocket.accept();

byte key = new byte[16];
for(int i=0;i<key.length;i++)
key[i] = 0x01;


EncryptedConnection c = new EncryptedConnection(s,connectionListener,key);

connections.add(c);
System.out.println("New Connection Established From"+s.getInetAddress().toString());
}
}
catch(java.net.SocketException e)
{
System.out.println("Listening thread terminated with exception.");
}
catch(IOException e)
{

e.printStackTrace();
}
}


And here is the Client code that creates the socket:



@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == connect)
{
try
{
Socket s = new Socket(ipBox.getText(), Integer.parseInt(portBox.getText()));
byte key = new byte[16];
for(int i=0;i<key.length;i++)
key[i] = 0x01;
EncryptedConnection c = new EncryptedConnection(s,parent,key);

parent.connectionSuccessful(c);
}
catch (NumberFormatException e1)
{
JOptionPane.showMessageDialog(this, "Error! Port number must be a number", "Error", JOptionPane.ERROR_MESSAGE);
}
catch (UnknownHostException e1)
{
JOptionPane.showMessageDialog(this, "Error! Unable to find that host", "Error", JOptionPane.ERROR_MESSAGE);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}


I have viewed this post, but did not find it helpful.
ObjectInputStream with CipherInputStream freezing, hanging
I've also tried different AES encryption modes with padding on and off but I get the same result.



Here is sample code that works just fine. I am essentially doing the same thing but instead of files, using sockets.



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class IOTest
{
public static void main(String args)
{
FileOutputStream fos = null;
FileInputStream fis = null;
CipherInputStream cis = null;
CipherOutputStream cos = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Key key = null;
Cipher cipherD = null;
Cipher cipherE = null;
byte keydata = new byte[16];
byte iv = new byte[16];
IvParameterSpec ivspect = new IvParameterSpec(iv);

try
{
key = new SecretKeySpec(keydata,"AES");
cipherE = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherD = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherE.init(Cipher.ENCRYPT_MODE, key,ivspect);
cipherD.init(Cipher.DECRYPT_MODE, key, ivspect);

fos = new FileOutputStream("hello.data");
cos = new CipherOutputStream(fos,cipherE);
oos = new ObjectOutputStream(cos);

oos.writeObject(new String("H"));
oos.flush();
oos.close();


fis = new FileInputStream("hello.data");
cis = new CipherInputStream(fis, cipherD);
ois = new ObjectInputStream(cis);

String s = ois.readObject().toString();
System.out.println(s);

ois.close();

}
catch(Exception e)
{

}
}
}









share|improve this question




















  • 1





    What does the code on the other side of the Connection look like?

    – Erwin Bolwidt
    Nov 13 '18 at 2:44











  • I have edited the question with the information you requested.

    – Matthew
    Nov 13 '18 at 3:10











  • I must have missed it because I don't see any code that reads or writes anything.

    – James K Polk
    Nov 13 '18 at 4:41














0












0








0








I am attempting to use encryption to communicate over sockets using java. I've successfully communicated with sockets without encryption, but when I try with encryption the program freezes.



Here is the parent Connection class that works just fine:



public class Connection implements Runnable
{
protected Socket socket;
protected ObjectInputStream objectInputStream;
protected ObjectOutputStream objectOutputStream;
protected Thread listeningThread;
protected Thread dispatchThread;
protected boolean listen;
protected ArrayBlockingQueue<Object> readingQueue;
protected ConnectionListener connectionListener;

public Connection()
{
listen = true;
readingQueue = new ArrayBlockingQueue<Object>(10);
}

public Connection(Socket socket, ConnectionListener listener)
{
listen = true;
connectionListener = listener;
readingQueue = new ArrayBlockingQueue<Object>(10);

this.socket = socket;

try
{
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectInputStream = new ObjectInputStream(socket.getInputStream());
}
catch (IOException e)
{
e.printStackTrace();
}

startConnection();
}


Here is the child class that uses Encryption:



public class EncryptedConnection extends Connection
{
private Key key;
private Cipher cipherEncryption;
private Cipher cipherDecryption;

public EncryptedConnection(Socket socket, ConnectionListener listener, byte keydata)
{
super();
super.socket = socket;
super.connectionListener = listener;

try
{
key = new SecretKeySpec(keydata, "AES");
cipherEncryption = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherDecryption = Cipher.getInstance("AES/CBC/PKCS5Padding");

byte iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IvParameterSpec ivspec = new IvParameterSpec(iv);

cipherEncryption.init(Cipher.ENCRYPT_MODE, key, ivspec);
cipherDecryption.init(Cipher.DECRYPT_MODE, key, ivspec);

objectOutputStream = new ObjectOutputStream(new CipherOutputStream(socket.getOutputStream(),cipherEncryption));
objectInputStream = new ObjectInputStream(new CipherInputStream(socket.getInputStream(),cipherDecryption));
//The hanging or freezing occurs on the above line of code

}
catch(Exception e)
{

}


Here is the Server code that creates the socket:



@Override
public void run()
{
try
{
while(true)
{
Socket s = serverSocket.accept();

byte key = new byte[16];
for(int i=0;i<key.length;i++)
key[i] = 0x01;


EncryptedConnection c = new EncryptedConnection(s,connectionListener,key);

connections.add(c);
System.out.println("New Connection Established From"+s.getInetAddress().toString());
}
}
catch(java.net.SocketException e)
{
System.out.println("Listening thread terminated with exception.");
}
catch(IOException e)
{

e.printStackTrace();
}
}


And here is the Client code that creates the socket:



@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == connect)
{
try
{
Socket s = new Socket(ipBox.getText(), Integer.parseInt(portBox.getText()));
byte key = new byte[16];
for(int i=0;i<key.length;i++)
key[i] = 0x01;
EncryptedConnection c = new EncryptedConnection(s,parent,key);

parent.connectionSuccessful(c);
}
catch (NumberFormatException e1)
{
JOptionPane.showMessageDialog(this, "Error! Port number must be a number", "Error", JOptionPane.ERROR_MESSAGE);
}
catch (UnknownHostException e1)
{
JOptionPane.showMessageDialog(this, "Error! Unable to find that host", "Error", JOptionPane.ERROR_MESSAGE);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}


I have viewed this post, but did not find it helpful.
ObjectInputStream with CipherInputStream freezing, hanging
I've also tried different AES encryption modes with padding on and off but I get the same result.



Here is sample code that works just fine. I am essentially doing the same thing but instead of files, using sockets.



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class IOTest
{
public static void main(String args)
{
FileOutputStream fos = null;
FileInputStream fis = null;
CipherInputStream cis = null;
CipherOutputStream cos = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Key key = null;
Cipher cipherD = null;
Cipher cipherE = null;
byte keydata = new byte[16];
byte iv = new byte[16];
IvParameterSpec ivspect = new IvParameterSpec(iv);

try
{
key = new SecretKeySpec(keydata,"AES");
cipherE = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherD = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherE.init(Cipher.ENCRYPT_MODE, key,ivspect);
cipherD.init(Cipher.DECRYPT_MODE, key, ivspect);

fos = new FileOutputStream("hello.data");
cos = new CipherOutputStream(fos,cipherE);
oos = new ObjectOutputStream(cos);

oos.writeObject(new String("H"));
oos.flush();
oos.close();


fis = new FileInputStream("hello.data");
cis = new CipherInputStream(fis, cipherD);
ois = new ObjectInputStream(cis);

String s = ois.readObject().toString();
System.out.println(s);

ois.close();

}
catch(Exception e)
{

}
}
}









share|improve this question
















I am attempting to use encryption to communicate over sockets using java. I've successfully communicated with sockets without encryption, but when I try with encryption the program freezes.



Here is the parent Connection class that works just fine:



public class Connection implements Runnable
{
protected Socket socket;
protected ObjectInputStream objectInputStream;
protected ObjectOutputStream objectOutputStream;
protected Thread listeningThread;
protected Thread dispatchThread;
protected boolean listen;
protected ArrayBlockingQueue<Object> readingQueue;
protected ConnectionListener connectionListener;

public Connection()
{
listen = true;
readingQueue = new ArrayBlockingQueue<Object>(10);
}

public Connection(Socket socket, ConnectionListener listener)
{
listen = true;
connectionListener = listener;
readingQueue = new ArrayBlockingQueue<Object>(10);

this.socket = socket;

try
{
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectInputStream = new ObjectInputStream(socket.getInputStream());
}
catch (IOException e)
{
e.printStackTrace();
}

startConnection();
}


Here is the child class that uses Encryption:



public class EncryptedConnection extends Connection
{
private Key key;
private Cipher cipherEncryption;
private Cipher cipherDecryption;

public EncryptedConnection(Socket socket, ConnectionListener listener, byte keydata)
{
super();
super.socket = socket;
super.connectionListener = listener;

try
{
key = new SecretKeySpec(keydata, "AES");
cipherEncryption = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherDecryption = Cipher.getInstance("AES/CBC/PKCS5Padding");

byte iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IvParameterSpec ivspec = new IvParameterSpec(iv);

cipherEncryption.init(Cipher.ENCRYPT_MODE, key, ivspec);
cipherDecryption.init(Cipher.DECRYPT_MODE, key, ivspec);

objectOutputStream = new ObjectOutputStream(new CipherOutputStream(socket.getOutputStream(),cipherEncryption));
objectInputStream = new ObjectInputStream(new CipherInputStream(socket.getInputStream(),cipherDecryption));
//The hanging or freezing occurs on the above line of code

}
catch(Exception e)
{

}


Here is the Server code that creates the socket:



@Override
public void run()
{
try
{
while(true)
{
Socket s = serverSocket.accept();

byte key = new byte[16];
for(int i=0;i<key.length;i++)
key[i] = 0x01;


EncryptedConnection c = new EncryptedConnection(s,connectionListener,key);

connections.add(c);
System.out.println("New Connection Established From"+s.getInetAddress().toString());
}
}
catch(java.net.SocketException e)
{
System.out.println("Listening thread terminated with exception.");
}
catch(IOException e)
{

e.printStackTrace();
}
}


And here is the Client code that creates the socket:



@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == connect)
{
try
{
Socket s = new Socket(ipBox.getText(), Integer.parseInt(portBox.getText()));
byte key = new byte[16];
for(int i=0;i<key.length;i++)
key[i] = 0x01;
EncryptedConnection c = new EncryptedConnection(s,parent,key);

parent.connectionSuccessful(c);
}
catch (NumberFormatException e1)
{
JOptionPane.showMessageDialog(this, "Error! Port number must be a number", "Error", JOptionPane.ERROR_MESSAGE);
}
catch (UnknownHostException e1)
{
JOptionPane.showMessageDialog(this, "Error! Unable to find that host", "Error", JOptionPane.ERROR_MESSAGE);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}


I have viewed this post, but did not find it helpful.
ObjectInputStream with CipherInputStream freezing, hanging
I've also tried different AES encryption modes with padding on and off but I get the same result.



Here is sample code that works just fine. I am essentially doing the same thing but instead of files, using sockets.



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class IOTest
{
public static void main(String args)
{
FileOutputStream fos = null;
FileInputStream fis = null;
CipherInputStream cis = null;
CipherOutputStream cos = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Key key = null;
Cipher cipherD = null;
Cipher cipherE = null;
byte keydata = new byte[16];
byte iv = new byte[16];
IvParameterSpec ivspect = new IvParameterSpec(iv);

try
{
key = new SecretKeySpec(keydata,"AES");
cipherE = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherD = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherE.init(Cipher.ENCRYPT_MODE, key,ivspect);
cipherD.init(Cipher.DECRYPT_MODE, key, ivspect);

fos = new FileOutputStream("hello.data");
cos = new CipherOutputStream(fos,cipherE);
oos = new ObjectOutputStream(cos);

oos.writeObject(new String("H"));
oos.flush();
oos.close();


fis = new FileInputStream("hello.data");
cis = new CipherInputStream(fis, cipherD);
ois = new ObjectInputStream(cis);

String s = ois.readObject().toString();
System.out.println(s);

ois.close();

}
catch(Exception e)
{

}
}
}






java encryption objectinputstream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 17:06







Matthew

















asked Nov 13 '18 at 2:35









MatthewMatthew

1,83242659




1,83242659








  • 1





    What does the code on the other side of the Connection look like?

    – Erwin Bolwidt
    Nov 13 '18 at 2:44











  • I have edited the question with the information you requested.

    – Matthew
    Nov 13 '18 at 3:10











  • I must have missed it because I don't see any code that reads or writes anything.

    – James K Polk
    Nov 13 '18 at 4:41














  • 1





    What does the code on the other side of the Connection look like?

    – Erwin Bolwidt
    Nov 13 '18 at 2:44











  • I have edited the question with the information you requested.

    – Matthew
    Nov 13 '18 at 3:10











  • I must have missed it because I don't see any code that reads or writes anything.

    – James K Polk
    Nov 13 '18 at 4:41








1




1





What does the code on the other side of the Connection look like?

– Erwin Bolwidt
Nov 13 '18 at 2:44





What does the code on the other side of the Connection look like?

– Erwin Bolwidt
Nov 13 '18 at 2:44













I have edited the question with the information you requested.

– Matthew
Nov 13 '18 at 3:10





I have edited the question with the information you requested.

– Matthew
Nov 13 '18 at 3:10













I must have missed it because I don't see any code that reads or writes anything.

– James K Polk
Nov 13 '18 at 4:41





I must have missed it because I don't see any code that reads or writes anything.

– James K Polk
Nov 13 '18 at 4:41












1 Answer
1






active

oldest

votes


















0














As AES is a block cipher (block size of 128 bit), it processes data in blocks of 16 bytes .. if there is not enough data for a full encryption block, the data will just sit in an input buffer waiting for more data to be present. In the receiving end you would just be stuck.



Only when enough data is present for a full block or if the steams is closed, will the stuck data be processed. In the case of closing the stream, the final data is patched up to a full block size according to the padding scheam used (eg. PKCS5Padding).






share|improve this answer
























  • This is what I don't understand. A socket is created prior to sending any data. We have a socket object on both the client and the server. I then try to simply create a CipherInputStream passing in the socket's inputstream. I may never have data to send. I don't understand why the code works fine with just an ObjectInputStream, but hangs with a CipherInputStream.

    – Matthew
    Nov 13 '18 at 19:49











  • The cipher stream is lazy .. It only passes data through when it have enough data for a full AES block. If you eg. try to send a block of 20 bytes, it will encrypt the first 16 bytes, but wait with the last 4 bytes until it gets 12 more bytes to have the next full AES block. The receiver that waits for 20 bytes will just be stuck as he don't get the last 4 bytes. You can't easily use a block cipher on a stream together with an object stream ...

    – Ebbe M. Pedersen
    Nov 13 '18 at 21:45











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272959%2fcipherinputstream-hangs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














As AES is a block cipher (block size of 128 bit), it processes data in blocks of 16 bytes .. if there is not enough data for a full encryption block, the data will just sit in an input buffer waiting for more data to be present. In the receiving end you would just be stuck.



Only when enough data is present for a full block or if the steams is closed, will the stuck data be processed. In the case of closing the stream, the final data is patched up to a full block size according to the padding scheam used (eg. PKCS5Padding).






share|improve this answer
























  • This is what I don't understand. A socket is created prior to sending any data. We have a socket object on both the client and the server. I then try to simply create a CipherInputStream passing in the socket's inputstream. I may never have data to send. I don't understand why the code works fine with just an ObjectInputStream, but hangs with a CipherInputStream.

    – Matthew
    Nov 13 '18 at 19:49











  • The cipher stream is lazy .. It only passes data through when it have enough data for a full AES block. If you eg. try to send a block of 20 bytes, it will encrypt the first 16 bytes, but wait with the last 4 bytes until it gets 12 more bytes to have the next full AES block. The receiver that waits for 20 bytes will just be stuck as he don't get the last 4 bytes. You can't easily use a block cipher on a stream together with an object stream ...

    – Ebbe M. Pedersen
    Nov 13 '18 at 21:45
















0














As AES is a block cipher (block size of 128 bit), it processes data in blocks of 16 bytes .. if there is not enough data for a full encryption block, the data will just sit in an input buffer waiting for more data to be present. In the receiving end you would just be stuck.



Only when enough data is present for a full block or if the steams is closed, will the stuck data be processed. In the case of closing the stream, the final data is patched up to a full block size according to the padding scheam used (eg. PKCS5Padding).






share|improve this answer
























  • This is what I don't understand. A socket is created prior to sending any data. We have a socket object on both the client and the server. I then try to simply create a CipherInputStream passing in the socket's inputstream. I may never have data to send. I don't understand why the code works fine with just an ObjectInputStream, but hangs with a CipherInputStream.

    – Matthew
    Nov 13 '18 at 19:49











  • The cipher stream is lazy .. It only passes data through when it have enough data for a full AES block. If you eg. try to send a block of 20 bytes, it will encrypt the first 16 bytes, but wait with the last 4 bytes until it gets 12 more bytes to have the next full AES block. The receiver that waits for 20 bytes will just be stuck as he don't get the last 4 bytes. You can't easily use a block cipher on a stream together with an object stream ...

    – Ebbe M. Pedersen
    Nov 13 '18 at 21:45














0












0








0







As AES is a block cipher (block size of 128 bit), it processes data in blocks of 16 bytes .. if there is not enough data for a full encryption block, the data will just sit in an input buffer waiting for more data to be present. In the receiving end you would just be stuck.



Only when enough data is present for a full block or if the steams is closed, will the stuck data be processed. In the case of closing the stream, the final data is patched up to a full block size according to the padding scheam used (eg. PKCS5Padding).






share|improve this answer













As AES is a block cipher (block size of 128 bit), it processes data in blocks of 16 bytes .. if there is not enough data for a full encryption block, the data will just sit in an input buffer waiting for more data to be present. In the receiving end you would just be stuck.



Only when enough data is present for a full block or if the steams is closed, will the stuck data be processed. In the case of closing the stream, the final data is patched up to a full block size according to the padding scheam used (eg. PKCS5Padding).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 9:09









Ebbe M. PedersenEbbe M. Pedersen

5,45031937




5,45031937













  • This is what I don't understand. A socket is created prior to sending any data. We have a socket object on both the client and the server. I then try to simply create a CipherInputStream passing in the socket's inputstream. I may never have data to send. I don't understand why the code works fine with just an ObjectInputStream, but hangs with a CipherInputStream.

    – Matthew
    Nov 13 '18 at 19:49











  • The cipher stream is lazy .. It only passes data through when it have enough data for a full AES block. If you eg. try to send a block of 20 bytes, it will encrypt the first 16 bytes, but wait with the last 4 bytes until it gets 12 more bytes to have the next full AES block. The receiver that waits for 20 bytes will just be stuck as he don't get the last 4 bytes. You can't easily use a block cipher on a stream together with an object stream ...

    – Ebbe M. Pedersen
    Nov 13 '18 at 21:45



















  • This is what I don't understand. A socket is created prior to sending any data. We have a socket object on both the client and the server. I then try to simply create a CipherInputStream passing in the socket's inputstream. I may never have data to send. I don't understand why the code works fine with just an ObjectInputStream, but hangs with a CipherInputStream.

    – Matthew
    Nov 13 '18 at 19:49











  • The cipher stream is lazy .. It only passes data through when it have enough data for a full AES block. If you eg. try to send a block of 20 bytes, it will encrypt the first 16 bytes, but wait with the last 4 bytes until it gets 12 more bytes to have the next full AES block. The receiver that waits for 20 bytes will just be stuck as he don't get the last 4 bytes. You can't easily use a block cipher on a stream together with an object stream ...

    – Ebbe M. Pedersen
    Nov 13 '18 at 21:45

















This is what I don't understand. A socket is created prior to sending any data. We have a socket object on both the client and the server. I then try to simply create a CipherInputStream passing in the socket's inputstream. I may never have data to send. I don't understand why the code works fine with just an ObjectInputStream, but hangs with a CipherInputStream.

– Matthew
Nov 13 '18 at 19:49





This is what I don't understand. A socket is created prior to sending any data. We have a socket object on both the client and the server. I then try to simply create a CipherInputStream passing in the socket's inputstream. I may never have data to send. I don't understand why the code works fine with just an ObjectInputStream, but hangs with a CipherInputStream.

– Matthew
Nov 13 '18 at 19:49













The cipher stream is lazy .. It only passes data through when it have enough data for a full AES block. If you eg. try to send a block of 20 bytes, it will encrypt the first 16 bytes, but wait with the last 4 bytes until it gets 12 more bytes to have the next full AES block. The receiver that waits for 20 bytes will just be stuck as he don't get the last 4 bytes. You can't easily use a block cipher on a stream together with an object stream ...

– Ebbe M. Pedersen
Nov 13 '18 at 21:45





The cipher stream is lazy .. It only passes data through when it have enough data for a full AES block. If you eg. try to send a block of 20 bytes, it will encrypt the first 16 bytes, but wait with the last 4 bytes until it gets 12 more bytes to have the next full AES block. The receiver that waits for 20 bytes will just be stuck as he don't get the last 4 bytes. You can't easily use a block cipher on a stream together with an object stream ...

– Ebbe M. Pedersen
Nov 13 '18 at 21:45


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272959%2fcipherinputstream-hangs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."