Cryptography Reference
In-Depth Information
private void exchangeKeys() {
try {
output.write(modulus.toByteArray());
byte[] buffer=new byte[ciphertextBlockSize];
input.read(buffer);
recipModulus=new BigInteger(1,buffer);
} catch (IOException ioe) {
System.err.println(“Error establishing keys”);
}
}
The go() method is where the client enters into a loop, listening for messages from its input
stream. It then proceeds to read input; if the disconnect message 0 is sent from the server,
or if there is no more input to be read, the client disconnects by calling its closeAll() method,
which you will soon see. This method will be called whenever the client clicks on “Connect
to server above.”
private void go() {
try {
//Read as long as there is input
byte[] buffer=new byte[ciphertextBlockSize];
boolean disconnectMsgSent=false;
while (!disconnectMsgSent&&input.read(buffer)!=-1) {
//Decipher the bytes read in
byte[] plaintext=Ciphers.RSADecipherWSalt(buffer,decipherExp,modulus);
if (plaintext.length==1&&plaintext[0]==0) {
disconnectMsgSent=true;
closeAll();
} else {
//convert to a string and display
message = new String(plaintext);
displayArea.append(“\n”+message);
}
}
} catch (IOException ioe) {
//Server disconnected-we can reconnect if we wish
}
}
The closeAll() method for the client is similar to the one for the server. It closes its IO
streams, then the socket. It ensures to set the socket to null, since this is how the client tests
for a connection. The client then changes its button to say “Connect to server above” again,
and shuts off the message entry field.
//Close socket and IO streams, change appearance/functionality of some components
private void closeAll() throws IOException {
displayArea.append(“\nConnection closing”);
output.close();
Search WWH ::




Custom Search