Ideas2IT / cordova-aes256

MIT License
13 stars 20 forks source link

Encryption ChiperText not valid while doing decryption at java layer #31

Open elavankarthik84 opened 4 years ago

elavankarthik84 commented 4 years ago

Dear Team, We are using ionic v3 project and doing AES 256 encryption algorithm. the chiper Text not valid and it is not base64 format. how do we change the format to supported java. i have generated chiper Text as below format, it is working decryption within ionic project, if we use similar to other java layers. it is not able to decrypted. please helps.

format of chiper text : 77be64b6202dbbf43bfdce7669722b9c

praveenraji2i commented 4 years ago

@elavankarthik84 Can you please give us more details to debug? Are you trying to access the decryption method from some other Java class?

drik commented 1 year ago

Late answer, but, it can still help someone. I was confronted with this concern with this plugin: if you encrypt data with this plugin, impossible to decrypt on another system (c# or java server/application ). When I looked at the source code of the plugin to understand how the encrypt and decrypt is done, I found that the key provided is not directly used to encrypt. The plugin generates another encryption key from the one provided. No other system except this same plugin will ever be able to decrypt data encrypted by this plugin, since the keys will always be different

private String encrypt(String secureKey, String value, String iv) throws Exception {

    byte[] pbkdf2SecuredKey = generatePBKDF2(secureKey.toCharArray(), PBKDF2_SALT.getBytes("UTF-8"),
            PBKDF2_ITERATION_COUNT, PBKDF2_KEY_LENGTH);

    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("UTF-8"));
    SecretKeySpec secretKeySpec = new SecretKeySpec(pbkdf2SecuredKey, "AES");

    Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

    byte[] encrypted = cipher.doFinal(value.getBytes());

    return Base64.encodeToString(encrypted, Base64.DEFAULT);

}

The first two lines regenerate a key from the key provided to it in addition to other parameters that are specific to the plugin (PBKDF2_SALT, PBKDF2_ITERATION_COUNT, PBKDF2_KEY_LENGTH) If you want to succeed decryption on Java side, you must also regenerate a new key using the same technique as in this plugin. I can provide a working Java code that can decrypt data encrypted with this plugin