i have to decrypt a frame on my server. encrypted frame is coming from client device through GPRS on socket.encryption is done with "TripleDes" and with a given key.same algorithm and key i am using n server side. frame is a combination of Hex and Ascii String. problem is when i decrypt this frame i get an error :
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
and when i pad my byte array with zeros
i get an error :
javax.crypto.BadPaddingException: Given final block not properly padded
following is my code :
byte[] key = new byte[]{31, 30, 31, 36, 32, 11, 11, 11, 22, 26, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30};
myKeySpec = new DESedeKeySpec(key);
mySecretKeyFactory = SecretKeyFactory.getInstance("TripleDES");
de = mySecretKeyFactory.generateSecret(myKeySpec);
Cipher c = Cipher.getInstance("TripleDES");
c.init(Cipher.DECRYPT_MODE, key);
int l = completeHexStr.length();
if (l%8==1) completeHexStr = completeHexStr + "0000000";
--up to---
else if (l%8==7) completeHexStr = completeHexStr + "0";
byte decordedValue[] =completeHexString.getBytes();
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
System.out.println("decryptedValue= " + decryptedValue);
here are the functions which i am using inside the code:
public String stringToHex(String base) {
StringBuffer buffer = new StringBuffer();
int intValue = 0;
for (int x = 0; x < base.length(); x++) {
intValue = base.charAt(x);
String hex = Integer.toHexString(intValue);
if (hex.length() == 1) {
buffer.append("0" + hex + "");
} else {
buffer.append(hex + "");
}
}
return buffer.toString();
}
public String byteToAscii(byte[] b, int length) {
String returnString = "";
for (int i = 0; i < length; i++) {
returnString += (char) (b[i] & 0xff);
}
return returnString;
}
i am new in java cryptography. pl tell me how to do it? thanks in advance.