-2

I m requirement is to convert below java encrypt code to flutter as i need to encrypt few fields value before sending to api. Below is java encrypt code which i need to convert to java

public static String encrypt(String text, String algo, Key key) {
        byte[] cipherText;
        String output;
        try {
            if("RSA".equals(algo)) {
                throw new IllegalArgumentException("Do not pass just algo pass with padding and blocking stuff!");
            }
            if(BuildConfig.DEBUG) {
                Log.d(TAG, "encrypt in: "+text);
                Log.d(TAG, "algo: "+algo);
                Log.d(TAG, "key: "+key);
            }

            final Cipher cipher = Cipher.getInstance(algo);

            if(algo.contains("AES")) {
                AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
                cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
            } else {
                cipher.init(Cipher.ENCRYPT_MODE, key);
            }

            cipherText = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
            output = new String(Base64.encode(cipherText));

            if(BuildConfig.DEBUG) {
                Log.d(TAG, "encrypt out: "+output);
            }

            return output;
        } catch (Exception e) {
            Log.e(TAG, SDKConstants.STRING_ERROR + e, e);
            return null;
        }
}

            char[] encPwd = Objects.requireNonNull(CryptoHelper.encrypt(Objects.requireNonNull(binding.textIPassword.getEditText()).getText().toString(), CryptoHelper.ALGO_FOR_RSA, key)).toCharArray();

Please help me in converting above java code to flutter as i need to encrypt one field before sending it to api call.

Any help is appreciated!

Hemavathi
  • 195
  • 1
  • 16

1 Answers1

1

Just work through the Java line by line and figure out Dart equivalents. Use the other linked question as your guide. This should work (if I guessed the cipher correctly):

import 'package:pointycastle/export.dart';

String encrypt(String plainText, RSAPublicKey public) {
  final plainBytes = utf8.encode(plainText) as Uint8List;

  final cipher = PKCS1Encoding(RSAEngine())
    ..init(true, PublicKeyParameter<RSAPublicKey>(public));
  final cipherBytes = cipher.process(plainBytes);
  return base64Encode(cipherBytes);
}
Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • Note that the Java converts the resulting base 64 to chars. There's no real equivalent in Dart. You will have to figure out how the Java uses those chars before it sends to the API. I've left the encrypted base 64 as a string. – Richard Heap Jan 18 '22 at 14:21
  • i will try passing this string to api n see if api works – Hemavathi Jan 19 '22 at 14:36
  • this is how i m passing encrypted pwd to setPassword method – Hemavathi Jan 19 '22 at 14:37
  • All depends what Java `LoginRequest` looks like. It seems strange to be using `Arrays.toString()` Does the API really want a string that looks like `[a, b, c, d]` rather than `abcd` ?? – Richard Heap Jan 19 '22 at 16:57
  • It seems to me that this question is answered: how to convert the `encrypt` method from Java to Dart – Richard Heap Jan 19 '22 at 16:59