aspose file tools*
The moose likes Security and the fly likes javax.crypto.BadPaddingException - not using strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "javax.crypto.BadPaddingException - not using strings" Watch "javax.crypto.BadPaddingException - not using strings" New topic
Author

javax.crypto.BadPaddingException - not using strings

Sally Molloy
Greenhorn

Joined: Mar 01, 2012
Posts: 6
Hi, I am completely new to Java but am posting this here as similar issues seem to have come up here before.
I have STRUGGLED to complete a crypto assignment that has to be handed in in Java though I am not a java programmer. My code now works and implements Otway-Rees most of the time but then suddenly starts failing with the below exception. Most other reports of this exception I have come across seem to boil down to representing encrypted data as a string or character and the suggestion is to change it to a byte{] but I am already doing that (I think!!) The exception seems to mainly occur when I am decrypting rather than encrypting but most times it just works. Money on though it will produce the error when handed up to teacher so any and all help greatly appreciated.

javax.crypto.BadPaddingException
at gnu.javax.crypto.jce.cipher.CipherAdapter.engineDoFinal(libgcj.so.11)
at javax.crypto.Cipher.doFinal(libgcj.so.11)
at javax.crypto.Cipher.doFinal(libgcj.so.11)
at MyUtility.decrypt(MyUtility.java:11)
at ClientBOraConnectionHandler.run(ClientB.java:302)
at java.lang.Thread.run(libgcj.so.11)

greg stark
Ranch Hand

Joined: Aug 10, 2006
Posts: 220
There is nothing wrong with the code shown, which mean the problem is with some code you haven't shown.


Nice to meet you.
Sally Molloy
Greenhorn

Joined: Mar 01, 2012
Posts: 6
Thanks for the reply. I am posting the code that calls the code I previously posted. I apologise in advance for posting so much code but I have already illustrated that I am not capable of identifying the relevant code


greg stark
Ranch Hand

Joined: Aug 10, 2006
Posts: 220
That code is pretty hard to read, but from what I can see it use a protocol that doesn't make sense for ciphertext. Ciphertext bytes can be anything, include the value 0. Your code seem to treat 0 specially, by incrementing a variable numNulls and then terminating the loop after 2 are received. I don't see how that can work.
Sally Molloy
Greenhorn

Joined: Mar 01, 2012
Posts: 6
Yeah sorry - the code is pretty horrendous but as I said I am not a Java programmer and everytime I tried to tidy anything up it seemed to produce compile errors!!!

Anyway, the 00 or nulls thing is the encoding that has been specified. I have to send out certain composite data made up of different sections. Two 00s (i.e. 00 00) indicates the end of a section and is meant to allow the recipient to parse it. Now there is the obvious problem that if the data contains an actual or "real" 00 you need to escape that. How you do that is you add a 01 after it. So in summary, if you want to write 'end of segment' you write 00 00; if you genuinely want to send 00 as part of the data you ssend 00 01.

This is what all the looping through byte[]s is about . Hopeless???
greg stark
Ranch Hand

Joined: Aug 10, 2006
Posts: 220
Well, ok, but now I'm out of ideas
Sally Molloy
Greenhorn

Joined: Mar 01, 2012
Posts: 6
Thanks for looking though!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javax.crypto.BadPaddingException - not using strings