Exception in decrypting using 3DES

hi all,

i use the following code:

publicclass Tool{

publicstaticvoid main(String[] args)throws Exception{

String key ="2580FBBAD354EAADFB1CE31A9DB940B3B0E626209737F4B5";

byte[] keyInByte = Converter.hex2ByteArray(key);

DESedeEncrypter desEde;

desEde =new DESedeEncrypter(new SecretKeySpec(keyInByte,"DESede"));

byte[] EncryptedPac ={0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01};

String s = Converter.byteArray2Hex(EncryptedPac );

System.out.println("Encrypted pin block : " +s);

byte[] DecryptedPac = desEde.decrypt(EncryptedPac );

String isoPinBlock = Converter.byteArray2Hex(DecryptedPac);

System.out.println("decrypted pin Block: " +isoPinBlock);

}

}

publicclass DESedeEncrypter{

Cipher ecipher;

Cipher dcipher;

public DESedeEncrypter(SecretKey key)throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException{

ecipher = Cipher.getInstance("DESede/ECB/PKCS5Padding","SunJCE");

dcipher = Cipher.getInstance("DESede/ECB/PKCS5Padding","SunJCE");

ecipher.init(Cipher.ENCRYPT_MODE, key);

dcipher.init(Cipher.DECRYPT_MODE, key);

}

publicbyte[] encrypt(byte[] data)throws IllegalStateException, IllegalBlockSizeException, BadPaddingException{

byte[] cipherText =null;

cipherText = ecipher.doFinal(data);

return cipherText;

}

publicbyte[] decrypt(byte[] encData)throws IllegalStateException, IllegalBlockSizeException, BadPaddingException{

byte[] cipherText =null;

cipherText = dcipher.doFinal(encData);

return cipherText;

}

}

when i run this code, the following exception is thrown:

javax.crypto.BadPaddingException: Given final block not properly padded

at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)

at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)

at com.sun.crypto.provider.DESedeCipher.engineDoFinal(DashoA12275)

at javax.crypto.Cipher.doFinal(DashoA12275)

at de.wincor.fonet.cardservice.crypt.DESedeEncrypter.decrypt(DESedeEncrypter.java:50)

at Tool.main(Tool.java:27)

this is thrown when i initialise directly a vector of 16 bytes (byte[] EncryptedPac = {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01};

), but it's OK when i encode a string and pass it to be decrypted:

String block ="1412342147162905";

byte[] byteBlock = Converter.hex2ByteArray(block);

String HEXBlock = Converter.byteArray2Hex(byteBlock);

System.out.println("block in HEXA : "+HEXBlock);

byte[] EncyptedPac = desEde.encrypt(byteBlock);

what could be the problem?

Thanks in advance.

[4460 byte] By [A.Antonioa] at [2007-11-27 5:55:44]
# 1

As far as I can see you are take a block of bytes all with value 1 and then trying to decrypt them assuming they represent an encryption of a PKCS5 padded data. PKCS5 pads the original data to a multiple of the block size with bytes whose value is the number of bytes of padding. You have slightly better than a 1 in 256 chance of picking a set of byte that meet this unless you use your encryption routine.

Moral - only try to decrypt data that has been encrypted.

sabre150a at 2007-7-12 16:24:59 > top of Java-index,Security,Cryptography...
# 2
public static void main(String[] args) throws Exception{Very bad programming practice!
DarumAa at 2007-7-12 16:24:59 > top of Java-index,Security,Cryptography...
# 3

> public static void main(String[] args) throws

> Exception{

>

> Very bad programming practice!

Not for a simple test program where you aren't going to do anything useful. In this case, it's exactly the right thing - as soon as the test hits an error, it falls over and dumps a stack trace, which is what it exists to do.

In general, tho, you're correct, "throws Exception" loosens your contract to the point of meaninglessness.

G

ggaineya at 2007-7-12 16:24:59 > top of Java-index,Security,Cryptography...
# 4
Agreed. I stand corrected.
DarumAa at 2007-7-12 16:24:59 > top of Java-index,Security,Cryptography...