简体   繁体   中英

I encrypted the data in java using AES and trying the same to decrypt form javascript, but i am getting empty string as output in . Any suggestions

Below Code used for encryption in java side and encrypted data is shared to ui where we need to decrypt. Front end and backend using same key and iv.

    public static Optional<String> encrypt(String strToEncrypt) {
            try {
                Field field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("isRestricted");
                field.setAccessible(true);
                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
                field.set(null, false);
    
                byte[] keyHex = Hex.decodeHex(secretKey.toCharArray());
                byte[] decodeHex = Hex.decodeHex(ivKey.toCharArray());
                IvParameterSpec ivspec = new IvParameterSpec(decodeHex);
    
                SecretKeySpec secretKeySpec = new SecretKeySpec(keyHex, AES_ENC);
    
                Cipher cipher = Cipher.getInstance(ALGORITHM);
                cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivspec);
                String encodedToString = Base64.getEncoder()
                        .encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8)));
                System.out.println("ENC:>"+Base64.getDecoder().decode(encodedToString));
                return Optional.ofNullable(encodedToString);
            } catch (Exception e) {
                //log.error("AesUtil : Error occurred while encrypting the data : " + e);
            }
            return Optional.empty();
        }
    
        public static Optional<String> decrypt(String strToDecrypt) {
    
            try {
                Field field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("isRestricted");
                field.setAccessible(true);
                Field modifiersField = Field.class.getDeclaredField("modifiers");
                modifiersField.setAccessible(true);
                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
                field.set(null, false);
    
                byte[] keyHex = Hex.decodeHex(secretKey.toCharArray());
                byte[] decodeHex = Hex.decodeHex(ivKey.toCharArray());
                IvParameterSpec ivspec = new IvParameterSpec(decodeHex);
    
                SecretKeySpec secretKeySpec = new SecretKeySpec(keyHex, AES_ENC);
    
                Cipher cipher = Cipher.getInstance(ALGORITHM);
                cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivspec);
                byte[] decode = Base64.getDecoder().decode(strToDecrypt);
                String finalRes = new String(cipher.doFinal(decode));
                System.out.println(finalRes);
                return Optional.ofNullable(finalRes);
            } catch (Exception e) {
                //log.error("AesUtil : Error occurred while decrypting the data : " + e);
            }
            return Optional.empty();
        }

`Below code i tried to decrypt but its printing empty in console log also tried to decode the data in js and tried to decrypt, but still getting same empty output.

const decrypt = ()=>{

    var decode  = atob(encrypted);
var decrypted = CryptoJS.AES.decrypt({
    ciphertext: encrypted //decode
 },key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
});
    console.log(decrypted.toString()); 
    console.log(decrypted.toString(CryptoJS.enc.Utf8));
    
}
var decrypted = CryptoJS.AES.decrypt({
    ciphertext: CryptoJS.enc.Base64.parse(encrypted) //decode
 },CryptoJS.enc.Hex.parse(key), {
    iv: CryptoJS.enc.Hex.parse(iv),
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM