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.

