Hi is urgent here , i m facing some encryption and decryption problem between midlet and tomcat server , i encypted in midlet then send the tomcat server for decryption
But whenever decrypt in tomcat server, it showing IllegalBlockSizeException as below : -
Exp :Input length must be multiple of 8 when decrypting with padded cipher
Exp :javax.crypto.IllegalBlock
SizeExcept
ion: Input length must be multiple of 8 when decrypting with padded cipher
(StandardEngineValve.java:
109)
at org.apache.catalina.connec
tor.Coyote
Adapter.se
rvice(Coyo
teAdapter.
java:263)
at org.apache.coyote.http11.H
ttp11AprPr
ocessor.pr
ocess(Http
11AprProce
ssor.java:
852)
at org.apache.coyote.http11.H
ttp11AprPr
otocol$Htt
p11Connect
ionHandler
.process(H
ttp11AprPr
otocol.jav
a:584)
at org.apache.tomcat.util.net
.AprEndpoi
nt$Worker.
run(AprEnd
point.java
:1508)
Midlet side encryption code : -
cipher = Cipher.getInstance("DES/CB
C/PKCS5Pad
ding");
key = new SecretKeySpec(kDESKey, 0, kDESKey.length, "DES");
iv = new IvParameterSpec(kDESiv, 0, kDESiv.length);
// Calculate ciphertext size.
int blocksize = 16;
int ciphertextLength = 0;
int remainder = nonBlockPlaintext.length % blocksize;
if (remainder == 0) {
ciphertextLength = nonBlockPlaintext.length;
}else{
ciphertextLength = nonBlockPlaintext.length - remainder + blocksize;
}
cipher.init(Cipher.ENCRYPT
_MODE, key, iv);
ciphertext = new byte[ciphertextLength];
cipher.doFinal(nonBlockPla
intext, 0, nonBlockPlaintext.length, ciphertext, 0);
// base64 encrypted ciphertext then send to tomcat server
Tomcat server side decryption : -
cipher = Cipher.getInstance("DES/CB
C/PKCS5Pad
ding");
iv = new IvParameterSpec(kDESiv, 0, kDESiv.length);
key = new SecretKeySpec(kDESKey, 0, kDESKey.length, "DES");
cipher.init(Cipher.DECRYPT
_MODE, key, iv);
int blocksize = 16;
int ciphertextLength = 0;
int remainder = ciphertext.length % blocksize;
if (remainder == 0) {
ciphertextLength = ciphertext.length;
}else{
ciphertextLength = ciphertext.length - remainder + blocksize;
}
decrypted = new byte[ciphertextLength];
cipher.doFinal(ciphertext,
0, ciphertext.length, decrypted, 0);
//throw exception Input length must be multiple of 8 when decrypting with padded cipher
Its related with PKCS5Padding , i just wonder is that the midlet side or server side padding calculation is wrongly ?
Is urgent , thanks for advice