what is the valid key length for DESede Cipher
843811Jan 9 2009 — edited Jan 14 2009Hi,
I am facing problem in using the DESede cipher. I am wondering what is the valid key length for DESede.. I get the below error message
java.security.InvalidKeyException: Invalid key length: 48 bytes
at com.sun.crypto.provider.DESedeCipher.engineGetKeySize(DashoA13*..)
at javax.crypto.Cipher.b(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
Here is my current code :
String hexKey = "1111111111111111AAAAAAAAAAAAAAAA1111111111111111";
retailerId = "retailerId";
billRef1 = "billRef1";
currency = "458";
amount = "1000.00";
try{
MessageDigest md = MessageDigest.getInstance("MD5");
md.update((retailerId + billRef1 + amount + currency).getBytes());
byte[] bHashedDate = md.digest();
SecretKey sk = new SecretKeySpec(hexKey.getBytes(), "DESede");
byte[] bCheckSum = encryptDecrypt(Cipher.ENCRYPT_MODE, bHashedDate, sk, null);
checkSum = toHexString(bCheckSum);
} catch (Exception e){
e.printStackTrace();
}
private byte[] encryptDecrypt(int intOpMode, byte[] bData, SecretKey secretKey, byte[] bIvspec) throws Exception
{
byte[] bOutput = null;
try
{
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
if (bIvspec == null)
{
bIvspec = new byte[cipher.getBlockSize()]; // or bIvspec = new byte[8]
}
IvParameterSpec ivParameterSpec = new IvParameterSpec(bIvspec);
cipher.init(intOpMode, secretKey, ivParameterSpec);
bOutput = cipher.doFinal(bData);
}
catch (Exception e)
{
throw new Exception("TripleDESFactory.encryptDecrypt exception", e);
}
return bOutput;
}
Appreciate anyone could help me on this.
Thanks.