'Encrypt in frontend -> Decrypt in Backend (Flutter (encrypt) to String Boot (decrypt)) Getting Messy

I am trying to encrypt a JWT token on the fronted and decrypt it on the backend (I need to do it this).

The problem is that it my strings turn to bytes and stuff like that and I think it becomes a mess and I don't get the decrypted version of the JWT token.

Please take a look at my code and correct me (help me) if you can.

Thank you.

My flutter encryption code:

List encryptJwt (String jwtToken) {

  final key = encrypt.Key.fromUtf8('11111111111111111111111111111111');
  final iv = IV.fromLength(16);
  print(iv);
  final encrypter = Encrypter(AES(key, mode: AESMode.ctr));
  final encrypted = encrypter.encrypt(jwtToken, iv: iv);
  // final decrypted = encrypter.decrypt(encrypted, iv: iv);

  // print(decrypted);
  print("encrypted token:   " + encrypted.toString());
  return [encrypted, iv];
}

My Spring Boot decryption code:

@GetMapping(value = "/endpoint")
    public SseEmitter endpointFunction(@RequestParam String encryptedJwtToken, @RequestParam String ivString) {
        System.out.println("Encrypted => " + encryptedJwtToken);
        byte[] ivBytes = ivString.getBytes();
        final String base64Key = "11111111111111111111111111111111";
        byte[] keyBytes = DatatypeConverter.parseBase64Binary(base64Key);
        byte[] decryptedBytes = AES.decrypt(keyBytes, ivBytes, DatatypeConverter.parseBase64Binary(encryptedJwtToken));
        String jwtToken = new String(decryptedBytes, StandardCharsets.UTF_8);
        System.out.println("Decrypted => " + jwtToken);
}

Java Console:

Encrypted => Instance of 'Encrypted'
Decrypted => O�1#��姿�m|

The decrypted is definetly not the JWT token I wanted :(

EXTRA:::::CODE::::::IN::::JAVA::::

public class AES {
    public static byte[] encrypt(final byte[] keyBytes, final byte[] ivBytes, final byte[] messageBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
        return AES.transform(Cipher.ENCRYPT_MODE, keyBytes, ivBytes, messageBytes);
    }

    public static byte[] decrypt(final byte[] keyBytes, final byte[] ivBytes, final byte[] messageBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
        return AES.transform(Cipher.DECRYPT_MODE, keyBytes, ivBytes, messageBytes);
    }

    private static byte[] transform(final int mode, final byte[] keyBytes, final byte[] ivBytes, final byte[] messageBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        final SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        final IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        final Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");

        cipher.init(mode, keySpec, ivSpec);

        return cipher.doFinal(messageBytes);
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source