3

i encrypt my string in JAVA like:

public String encrypt(String toEncrypt) {
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.ENCRYPT_MODE, aesKey);
            byte[] encVal = c.doFinal(toEncrypt.getBytes());
            return Base64.getEncoder().encodeToString(encVal);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

and I want to decrypt it in typescript:

public static decrypt(key, value){
    key = btoa(key);
    let decryptedData = CryptoJS.AES.decrypt(value, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return decryptedData.toString( CryptoJS.enc.Utf8 );
  }

the decryptedData.toString( CryptoJS.enc.Utf8 ) returns the following Error: Malformed UTF-8 data

I followed along this tutorial

1
  • Does it help if you replace toEncrypt.getBytes() with toEncrypt.getBytes("UTF-8") in your Java code? Your error mentions malformed UTF-8 data converting the decrypted bytes to UTF-8 text, so it's possible your source string wasn't converted to bytes using UTF-8 before being encrypted. Commented Jan 22, 2021 at 18:35

1 Answer 1

3

here is a working example

JAVA:

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class Crypter {

    private final String key = "1234567890123456";
    
    public String encrypt(String toEncrypt) {
        try {
            IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());
            return new String(Base64.getEncoder().encode(encrypted));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String decrypt(String toDecrypt) {
        try {
            IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] cipherText = cipher.doFinal(Base64.getDecoder().decode(toDecrypt));
            return new String(cipherText);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

TypeScript:

import * as CryptoJS from 'crypto-js';

export class Crypter{

  public static encrypt(key, value){
    key = CryptoJS.enc.Utf8.parse(key);
    let ciphertext = CryptoJS.AES.encrypt(value, key, {iv: key}).toString();
    return ciphertext;
  }

  public static decrypt(key, value){
    key = CryptoJS.enc.Utf8.parse(key);
    let decryptedData = CryptoJS.AES.decrypt(value, key, {
      iv: key
    });
    return decryptedData.toString( CryptoJS.enc.Utf8 );
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Security warning: Using the key as initialization vector is highly UNSECURE - please use a random IV instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.