thanh liem thanh liem - 2 years ago 109
Java Question

Decode Hexdecimal with java

I try decode hex result from encode but not success
String befor encode : 9041
String after decode is [B@1f2be27
Please help me.

public static void main(String[] args) throws Exception {

String text = "FEBA4C2EDD47FD32";
String encode = encryptData("9041","747062616e6b6570696e6e6577696e746567726174696f6e",168,true);
String result = decryptData("FEBA4C2EDD47FD32", "747062616e6b6570696e6e6577696e746567726174696f6e", 168, false);
System.out.println(encode);
System.out.println(result); // this is a byte array, you'll just see a reference to an array

}


//endcode function
public static String encryptData(String plainText, String key, int keySize, boolean paddingEnable)
throws Exception {
byte[] clearData = plainText.getBytes();
return encryptData(clearData, key, keySize, paddingEnable);
}

public static String encryptData(byte[] clearData, String key, int keySize, boolean paddingEnable)
throws Exception {
byte[] keyBytes = getEncryptionKey(key, keySize);
SecretKey secretKey = new SecretKeySpec(keyBytes, "DESede");
String algo = "DESede/ECB/pkcs5padding";
Cipher cipher = Cipher.getInstance(algo);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cipherText = cipher.doFinal(clearData);
System.out.println(clearData.toString());
return String.valueOf(Hex.encodeHex(cipherText, false));
}

public static String decryptData(String clearData, String key, int keySize, boolean paddingEnable)
throws Exception {
byte[] keyBytes = getEncryptionKey(key, keySize);
SecretKey secretKey = new SecretKeySpec(keyBytes, "DESede");
String algo = "DESede/ECB/pkcs5padding";
Cipher cipher = Cipher.getInstance(algo);
byte[] stringDecode = Hex.decodeHex(clearData.toCharArray());
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] cipherText = cipher.doFinal(stringDecode);
return cipherText.toString();
}


private static byte[] getEncryptionKey(String keyString, int keySize)
throws Exception {
int keyLength = keyString.length();
switch (keySize) {
case 56:
if (keyLength != 16) {
throw new InvalidKeyException("Hex Key length should be 16 for a 56 Bit Encryption, found [" + keyLength + "]");
}
break;
case 112:
if (keyLength != 32) {
throw new InvalidKeyException("Hex Key length should be 32 for a 112 Bit Encryption, found[" + keyLength + "]");
}
break;
case 168:
if ((keyLength != 32) && (keyLength != 48)) {
throw new InvalidKeyException("Hex Key length should be 32 or 48 for a 168 Bit Encryption, found[" + keyLength + "]");
}
if (keyLength == 32) {
keyString = keyString + keyString.substring(0, 16);
}
break;
default:
throw new InvalidKeyException("Invalid Key Size, expected one of [56, 112, 168], found[" + keySize + "]");
}
return Hex.decodeHex(keyString.toCharArray());
}

Answer Source

At the end of decryptData(), you have return cipherText.toString(); which calls toString() from the Object class on the byte array itself. This isn't a very interesting toString() implementation - it just returns some garbage based on the location in memory of the byte array object.

You need to actually convert your byte array back to a String. You could try

return new String(cipherText);

as the last line of decryptData() instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download