zealings
04-11-2006, 21:28
I write a Java service of 3DES encryption, however, if the encrypt string length is not 8, 16, 24 ... bytes, it will throw "javax.crypto.IllegalBlockSizeException: Input data length not a multiple of blocksize.", howevere, when i test it in another server, it works properly, did any of you have any idea on that?
Furthermore, what should be the length of the key? is it 24 bytes or 32 bytes? is it possible to use a 32 bytes key to encrypt?
Thank you in advance
byte[] passphrase = null;
byte[] instring = null;
IDataCursor idc = null;
String Algorithm = "DESede";
byte[] encryptString = null;
try {
idc = pipeline.getCursor();
if (idc.first("instring"))
{
instring = (byte[])idc.getValue();
}
else
{
throw new ServiceException("Input parameter \'passphrase\' was not found.");
}
if (idc.first("passphrase"))
{
passphrase = (byte[])idc.getValue();
}
else
{
throw new ServiceException("Input parameter \'inputstring\' was not found.");
}
SecretKey deskey = new SecretKeySpec(passphrase, Algorithm);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
encryptString = c1.doFinal(instring);
}
catch (Exception ex) {
throw new ServiceException(ex.toString());
}
finally {
IDataUtil.put(idc, "encryptString",encryptString);
idc.destroy();
idc = null;
instring = null;
passphrase = null;
Algorithm = null;
encryptString = null;
}
Furthermore, what should be the length of the key? is it 24 bytes or 32 bytes? is it possible to use a 32 bytes key to encrypt?
Thank you in advance
byte[] passphrase = null;
byte[] instring = null;
IDataCursor idc = null;
String Algorithm = "DESede";
byte[] encryptString = null;
try {
idc = pipeline.getCursor();
if (idc.first("instring"))
{
instring = (byte[])idc.getValue();
}
else
{
throw new ServiceException("Input parameter \'passphrase\' was not found.");
}
if (idc.first("passphrase"))
{
passphrase = (byte[])idc.getValue();
}
else
{
throw new ServiceException("Input parameter \'inputstring\' was not found.");
}
SecretKey deskey = new SecretKeySpec(passphrase, Algorithm);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
encryptString = c1.doFinal(instring);
}
catch (Exception ex) {
throw new ServiceException(ex.toString());
}
finally {
IDataUtil.put(idc, "encryptString",encryptString);
idc.destroy();
idc = null;
instring = null;
passphrase = null;
Algorithm = null;
encryptString = null;
}