I am working on an application that sends encrypted data over the network using Blowfish. I developed a simple server and client to test and I getting this error when I try to decrypt the cypher in the server. Here is what I do:
This happens on the client side:
I encrypt my message with this method
public byte[] encryptedMessage() throws Exception{
KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
keyGenerator.init(128);
Key key = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plaintext = getMessage().getBytes("UTF8");
byte[] ciphertext = cipher.doFinal(plaintext);
return ciphertext;
}
Then send it to the server like this:
//get my cypher from the method shown before
cipher = mc.encryptedMessage();
//put the cypher in this simple object
InformationShared cipherToSend = new InformationShared(cipher.length);
cipherToSend.setMessage(cipher);
//Send it accross through the network
Socket socket = new Socket("127.0.0.1",8085);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(cipherToSend);
This is what happens in the server side:
//Get the incoming object
ObjectInputStream in = new ObjectInputStream( incoming.getInputStream() );
//downcast the object
InformationShared cipherIn = (InformationShared)in.readObject();
//decypher the message in the decypher method
String result = decypher(cipherIn.getMessage());
//This is the decipher method
public String decypher(byte cipherIn[]) throws Exception{
KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
keyGenerator.init(128); // need to initialize with the keysize
Key key = keyGenerator.generateKey();
Cipher ciph = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
ciph.init(Cipher.DECRYPT_MODE, key);
// Perform the decryption
byte[] decryptedText = ciph.doFinal(cipherIn);
String output = new String(decryptedText,"UTF8");;
return output;
}
The error happens in the decypher method in this line:
byte[] decryptedText = ciph.doFinal(cipherIn);
Thanks for any help!