I have a java class for enc/dec in DESEDE;
for some cipher texts it results in this exception:
Input length must be multiple of 8 when decrypting with padded cipher
I have not written this code myself I have found it in a thread in this website;
could anyone please help me come up with the reason why this happens?
for example the following cipher text is the result of encrypting a random 24 character key
in similar C# class:
gd95f%2finjchs6O9BVdnEgU8L
but decrypting this in java faces the stated exception, since the decryption in C# is fine;
thanks in advance;
ps.to be precise, we have our encryption done in C# and decryption in java;
the classes are tested and work fine together except for some texts which seem that their length is a problem;
private static String key = "4d89g13j4j91j27c582ji693";
private static String initializationVector = "abcdefgh";
public static String encryptText(String plainText) throws Exception
{
//---- Use specified 3DES key and IV from other source -------------------------
byte[] plaintext = plainText.getBytes();
byte[] tdesKeyData = key.getBytes();
byte[] myIV = initializationVector.getBytes();
Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "DESede");
IvParameterSpec ivspec = new IvParameterSpec(myIV);
c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
byte[] cipherText = c3des.doFinal(plaintext);
// return Base64Coder.encodeString(new String(cipherText));
BASE64Encoder enc = new BASE64Encoder();
String retCrypt = enc.encodeBuffer(cipherText);
System.out.println("retCrypt " + retCrypt);
return URLEncoder.encode(retCrypt);
}
public static String decryptText(String cipherText)throws Exception
{
byte[] encData = new sun.misc.BASE64Decoder().decodeBuffer(cipherText);
Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
byte[] tdesKeyData = key.getBytes();
SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "DESede");
IvParameterSpec ivspec = new IvParameterSpec(initializationVector.getBytes());
decipher.init(Cipher.DECRYPT_MODE, myKey, ivspec);
byte[] plainText = decipher.doFinal(encData);
return new String(plainText);
}