In NodeJS this code produces this output: 'e5bd405394d639af20d072364b57ec7c'
var key = 'gustavo'
var src = 'arellano'
var cipher = crypto.createCipher("aes-128-ecb", key)
var result = cipher.update(src).toString('hex');
result += cipher.final().toString('hex');
console.log(result)
Now, in Java, I have this method:
private static String encrypt(String source) throws Exception {
byte[] input = source.getBytes(StandardCharsets.UTF_8);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest("gustavo".getBytes(StandardCharsets.UTF_8));
SecretKeySpec skc = new SecretKeySpec(thedigest, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skc);
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
return Base64.getEncoder().encodeToString(cipherText);
}
But, encrypt("arellano") returns "5b1AU5TWOa8g0HI2S1fsfA=="
How can I adjust the Java code for me to obtain the string that NodeJS is giving me?
The right answer is: Use this line of code:
return String.format("%040x", new BigInteger(1, cipherText));
Instead of:
return Base64.getEncoder().encodeToString(cipherText);
That's it.
Thanks everyone for your help.