Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Given final block not properly padded

843811Jun 11 2005 — edited Mar 10 2007
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!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 7 2007
Added on Jun 11 2005
8 comments
933 views