1

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.

Grim
  • 1,938
  • 10
  • 56
  • 123
Krrish Raj
  • 1,505
  • 12
  • 28
  • 1
    From the docs (https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html#init(int,%20java.security.Key,%20java.security.spec.AlgorithmParameterSpec)): `opmode - the operation mode of this cipher (this is one of the following: ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE or UNWRAP_MODE)`. Are you sure you are using the `ENCRYPT_MODE` and not `DECRYPT_MODE` when calling the `init` method? – ymz Feb 28 '19 at 13:10
  • You taking the wrong direction. You should not try to get the same ciphertext (which can be impossible in case a random padding was used). Instead you should try to decrypt the existing cipher text. Start with NoPadding and see what was added to the plain text data. Then search for the added data to which padding it belongs. – Robert Feb 28 '19 at 18:26
  • There is no global default, the default is provider-specific. You should **never** rely on defaults for the transformation string in `Cipher.getInstance()`, it provides no benefit and results in non-portable (and thus buggy) behavior. – President James K. Polk Mar 01 '19 at 14:05

1 Answers1

5

Don't rely on default behavior. If you known the options, go ahead and specify them. If you don't know the options, then go and find them out, and then specify them.

Both of the encrypted messages are 'hello' encrypted with your key .. the only difference is different padding modes:

57C758B2B3A8580658A11DBD95109EC4 decrypts to   68656c6c6f0b0b0b0b0b0b0b0b0b0b0b
                                               h e l l o <-- pkcs5 padding  -->

3874350661ABB0B452A4960FE3953C18 decrypts to   68656c6c6f0000000000000000000000
                                               h e l l o <-- zerro padding  -->
Ebbe M. Pedersen
  • 7,250
  • 3
  • 27
  • 47