File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java Micro Edition and the fly likes TripleDES  compatibility  (Sun - BC) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "TripleDES  compatibility  (Sun - BC)" Watch "TripleDES  compatibility  (Sun - BC)" New topic

TripleDES compatibility (Sun - BC)

Jorma Ikonen
Ranch Hand

Joined: Sep 17, 2001
Posts: 49
I'm using TripleDES (DESede) algorithm between servlet and J2ME application to encrypt data. Servlet uses libraries on javax.crypto-package and the other one Bouncy Castle's similar. Both application has exactly same encryption keys, but servlet throws an exception "Given final block not properly padded" while trying to decrypt received data.
Debugging shows that communication works and both application can encrypt/decrypt own data, but there is something what makes this modules incompatible to decrypt data from the other module.
J2ME app:
cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()));
cipher.init(true, new KeyParameter(cryptoKey));
byte[] plainBytes = plainText.getBytes();
byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
int oLen = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
cipher.doFinal(encryptedBytes, oLen);
}catch (CryptoException ce){
throw new Exception("ERROR:\n- Encryption error.");
// Create the cipher
DESede = Cipher.getInstance("DESede");
DESede.init(Cipher.DECRYPT_MODE, cryptoKey);
stringFromClient = new String(DESede.doFinal(readBytes));
-> above throws exception "Given final block not properly padded".
I have a feeling that the block size might cause this, but how to effect/ensure the situation? Also I'm wondering why user (user of the function) have to worry about details (processBytes, doFinal, etc) of the encryption/decryption? This does not follow main principles of object oriented programming (encapsulation).
Jorma Ikonen
Ranch Hand

Joined: Sep 17, 2001
Posts: 49
The problem is solved by modifying each modules to use BC's crypto libraries. It seems that SUN's DESede uses ECB by default and do not support CBC (not documentted thing). Further, BC support CBC, but not ECB, and therefore use of BC's libraries was must (works with servlet, applets, J2ME applications).
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Thanks for sharing the solution!

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
sawal lubis

Joined: Dec 28, 2007
Posts: 4
hi..this thread is more than 3 years old now...
but i want to ask about this thread, yes, about TripleDes in bouncycastle or in mobile application when we want to use ECB chiper mode..

i want to use ECB chiper mode, becouse in server, my friend had been devlop encrypt and dencrypt with this mode.So i must using the same mode, ECB chiper mode..

how can i encrypt in TripleDEs and ECB chiper mode with bouncycastle library..?if it can't, any library can i use to solve my problem..?

is Sun (java.crypto) for mobile can to what i want...?
Jorma Ikonen
Ranch Hand

Joined: Sep 17, 2001
Posts: 49
It's pretty funny that after 5 years break since my last visit here, I found someone reading/asking questions about this issue.

Anyway, I must say that I can't help you due the fact that my knowledge about making programs for MIDP is based on situation in 2003. I'm been doing mechanics for mobile phones and been waiting until phones will implement J2SE. (The reason why I'm visiting here is that I'm trying to get some information about use of GPS in MIDP.)

Let's hope that someone else could help you.
chetan dhumane
Ranch Hand

Joined: Jan 07, 2009
Posts: 641

Anyone has implemented this or have a code snippet as an example ?

Author @
Moody blogger who do not like to behave like target setting machines work.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Actually, we don't encourage "waking up the zombies", as we call it. Once a thread is dead, we'd rather see a new thread. But I congratulate you for at least looking for prior answers.

I haven't done anything recent on mobile encryption, but DES, even 3DES isn't considered to be very secure any more. They can be broken by raw horsepower on modern-day computers.

An IDE is no substitute for an Intelligent Developer.
chetan dhumane
Ranch Hand

Joined: Jan 07, 2009
Posts: 641

Anyone implemented this using bouncycastle ?
I agree. Here's the link:
subject: TripleDES compatibility (Sun - BC)
jQuery in Action, 3rd edition