-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!

7
  • @Richard Heap Please help in converting above java code to flutter, thanks Commented Jan 18, 2022 at 13:07
  • What is this constant in Java? CryptoHelper.ALGO_FOR_RSA Is it RSA/ECB/PKCS1? If not, what? Commented Jan 18, 2022 at 14:01
  • 1
    Kindly note that Stackoverflow is not a free code conversion service Commented Jan 18, 2022 at 14:02
  • This is pretty much covered here: stackoverflow.com/questions/56473470/… Commented Jan 18, 2022 at 14:15
  • public static final String ALGO_FOR_RSA = "RSA/ECB/PKCS1Padding"; Commented Jan 19, 2022 at 4:22

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

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.
i will try passing this string to api n see if api works
this is how i m passing encrypted pwd to setPassword method
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 ??
It seems to me that this question is answered: how to convert the encrypt method from Java to Dart

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.