I need to port some code from java
to javascript
which handles encryption. I am unable to reproduce same cipher text as per the existing code, given the same key.
I am suspecting that I am not able to figure out the correct mode. Attaching the code snippet-
public String encrypt(String message, String enc_key) throws Exception {
try {
initEncrypt(enc_key);
byte[] encstr = cipher.doFinal(message.getBytes());
return HexUtil.HextoString(encstr);
} catch (BadPaddingException nse) {
throw new Exception("Invalid input String");
}
}
public void initEncrypt(String key) throws Exception {
try {
skeySpec = new SecretKeySpec(HexUtil.HexfromString(key), "AES");
cipher = Cipher.getInstance("AES");
// cipher.
cipher.init(1, skeySpec);
System.out.println(cipher.getAlgorithm());
} catch (NoSuchAlgorithmException nsae) {
throw new Exception("Invalid Java Version");
} catch (NoSuchPaddingException nse) {
throw new Exception("Invalid Key");
}
}
HexToString function just converts the hex string key in byte array.
Providing the key - 10663d0b39d73d614116f0b3cbebd666
(hex)
Plain text- hello
, I get -
Cipher text - 57C758B2B3A8580658A11DBD95109EC4
According to this so answer, default mode should be AES/ECB/PKCS5Padding
But when I try to encrypt using same values here I get cipher text-
3874350661ABB0B452A4960FE3953C18
I have tried various other modes but I am unable to match the cipher text. Any help will be appreciated.