-1

My requirement is i need to convert below android method in dart & use it for some operation so i m trying to convert below android method in dart.Please help how to convert below method Android method as below:

    public static PublicKey getPublicKeyFromBase64EncodedKey(String algo,String b64key) throws NoSuchAlgorithmException, InvalidKeySpecException {
            if(b64key==null) {
                throw new NullPointerException("b64key cant be null");
            }
            byte[] encodedKey = Base64.decode(b64key.toCharArray());
            EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
            KeyFactory generator = KeyFactory.getInstance(algo);
            return generator.generatePublic(publicKeySpec);
        }

 key = CryptoHelper.getPublicKeyFromBase64EncodedKey("RSA", keyInfo.getRsaPubKey());

               

This above method i need to convert in dart & use it for other purpose any flutter developer who see this please help i m stuck in this conversion.

In have added below dependency : crypto: ^3.0.1

I have already pasted my dart code which u suggested which i m using currently is as below

RSAPublicKey getPublicKeyFromBase64EncodedKey(String b64) {
  final pem =
      '-----BEGIN RSA PUBLIC KEY-----\n$b64\n-----END RSA PUBLIC KEY-----';
  return CryptoUtils.rsaPublicKeyFromPem(pem);
}
 final public = getPublicKeyFromBase64EncodedKey(
                            value.encodedPublicKey);
                        print(public.modulus);

output of java & dart as below:

In java : key = CryptoHelper.getPublicKeyFromBase64EncodedKey("RSA", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm2nhT1TYYA1v/IBmdCYanrjxJVtzCt8X7O646sFPLOt/LXRXugH12B4gMsekwF+aYnb018pr0bqjjtP/vnN2mYcOfiv07JJqxP7RZ67DFV6hPk4vWV9ppIHksbXB7uQP4upFvPiRiflC2m75EbQXQHns73JazsCPdUcBAhaOgQiWrmZcldrOyS62aQ5XygMtQP7vxsHHGmkVIjRryfm2zKIn1nl/pYyyVUA3q50P0Zxj73h6uBi5T2ffbhtzXG740eJ+hPCfB3D1lbEQiPn5Xb5EACU+G7gCTNgrGsbqCKjAR97nyxsfTIFaOI1j9FDZbtt8SxfexO0qtGqnR32WnwIDAQAB"); Log.d(TAG,"key:"+key.toString());

ouput:

2022-01-16 11:42:27.400 26752-26752/com.test.com D/LoginFragment: key:OpenSSLRSAPublicKey{modulus=9b69e14f54d8600d6ffc806674261a9eb8f1255b730adf17eceeb8eac14f2ceb7f2d7457ba01f5d81e2032c7a4c05f9a6276f4d7ca6bd1baa38ed3ffbe737699870e7e2bf4ec926ac4fed167aec3155ea13e4e2f595f69a481e4b1b5c1eee40fe2ea45bcf89189f942da6ef911b4174079ecef725acec08f75470102168e810896ae665c95dacec92eb6690e57ca032d40feefc6c1c71a691522346bc9f9b6cca227d6797fa58cb2554037ab9d0fd19c63ef787ab818b94f67df6e1b735c6ef8d1e27e84f09f0770f595b11088f9f95dbe4400253e1bb8024cd82b1ac6ea08a8c047dee7cb1b1f4c815a388d63f450d96edb7c4b17dec4ed2ab46aa7477d969f,publicExponent=10001}

In Dart:

final public = getPublicKeyFromBase64EncodedKey( "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm2nhT1TYYA1v/IBmdCYanrjxJVtzCt8X7O646sFPLOt/LXRXugH12B4gMsekwF+aYnb018pr0bqjjtP/vnN2mYcOfiv07JJqxP7RZ67DFV6hPk4vWV9ppIHksbXB7uQP4upFvPiRiflC2m75EbQXQHns73JazsCPdUcBAhaOgQiWrmZcldrOyS62aQ5XygMtQP7vxsHHGmkVIjRryfm2zKIn1nl/pYyyVUA3q50P0Zxj73h6uBi5T2ffbhtzXG740eJ+hPCfB3D1lbEQiPn5Xb5EACU+G7gCTNgrGsbqCKjAR97nyxsfTIFaOI1j9FDZbtt8SxfexO0qtGqnR32WnwIDAQAB"); print(public.modulus); output:

I/flutter (23425): 19619148701255955064187742708345140267013188779302139280202352807669671677792626393629337988857597157230299104670179462381571284295659293566421465426699584753017694044543806475612955132231994585293968427073110740635269679598382323175630527971729493648655706989113013803508897373922142337599207439057332225191488277595692501770999573164627345568152980723556404414806110219729981052166106807913426879541751226598668581272017132775809446906215982582846094616588532789843885723495718175489690570017871977893093768785977518763824406735295329252981640606586676369862992986824428920526987079303617436093360155294579501667999

Please check that for same key i m getting two different value in java & dart what i m missing why i m not getting same value?

Both java & dart code is pasted above which i m using currently.

Let me know why java & dart key are not matching.

Any help is appreciated!

Hemavathi
  • 195
  • 1
  • 16

2 Answers2

1

Use the basic_utils package. (Remember to add it to your pubspec.yaml).

import 'package:basic_utils/basic_utils.dart';

RSAPublicKey getPublicKeyFromBase64EncodedKey(String b64) {
  final pem =
      '-----BEGIN RSA PUBLIC KEY-----\n$b64\n-----END RSA PUBLIC KEY-----';
  return CryptoUtils.rsaPublicKeyFromPem(pem);
}

// example usage below...
void main() {
  final javaEncoded =
      'MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAnitmaCHY8T171EiimoIN8MUg6IMi2JXbZBFioFJK9fiXnX/bo/6+1fuNgk+T/r6fbtk0iCogBCL30j/K7L+3J9p06rhvpM0/XAxi7YhMrIU5zetToZPBStzWt/qS1vOF2WFK6OfloFwFNAJltNljeVl7M7KbAkYcLxhfwyaPZTvmumAsCkkVhQevaVNBD2i6BWbMWb+3aki20FE2+nwzSa0ZgtwA9FJ4AV023eA1sw1ON9KS9eqdz78d/ve4aJJ/pv2Tvvc3iY2HhlvJmzS81tcocO660QediiI3K7rXus1QmGpHNEBvv818ai+RS4sehvyAhZl6dr+HH9zpdV0QESHqsyi7zJaal2GYeILdNGZtyYEsaqEq8K8MiynCvk7HSXHGZsx7zNFtVztc8T6ubN9sBDx4myFj/a1pdDltQ8OJGvomLX6wBwC0ywiroDeMAdbpfU1xNBYUqj8S0dTkrlul+ucbOySTpTpQJOnj9X98kptGnL2ESqi71vQh+qcMALjF/OqbGVOnPzzEqPlUWsR9jykmjBHkgeC9vSoMeGMdx+pUV7D1AXrXGs3ALOuBoUcBR00JPwIQ69wSZ/WFL9C9+gPfIvO802NEuckEYQB1eHVQFnhxdNF+slii+RBZ7UqnH3Fzvai4rPq5Yd/+Enxa8Gd3dqkO0QmkP5OSPzECAwEAAQ==';

  final public = getPublicKeyFromBase64EncodedKey(javaEncoded);
  print(public.modulus);
}

To confirm the validity of this code, compare the outputs of these two working examples:

Java

      String pub =
              "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAnitmaCHY8T171EiimoIN8MUg6IMi2JXbZBFioFJK9fiXnX/bo/6+1fuNgk+T/r6fbtk0iCogBCL30j/K7L+3J9p06rhvpM0/XAxi7YhMrIU5zetToZPBStzWt/qS1vOF2WFK6OfloFwFNAJltNljeVl7M7KbAkYcLxhfwyaPZTvmumAsCkkVhQevaVNBD2i6BWbMWb+3aki20FE2+nwzSa0ZgtwA9FJ4AV023eA1sw1ON9KS9eqdz78d/ve4aJJ/pv2Tvvc3iY2HhlvJmzS81tcocO660QediiI3K7rXus1QmGpHNEBvv818ai+RS4sehvyAhZl6dr+HH9zpdV0QESHqsyi7zJaal2GYeILdNGZtyYEsaqEq8K8MiynCvk7HSXHGZsx7zNFtVztc8T6ubN9sBDx4myFj/a1pdDltQ8OJGvomLX6wBwC0ywiroDeMAdbpfU1xNBYUqj8S0dTkrlul+ucbOySTpTpQJOnj9X98kptGnL2ESqi71vQh+qcMALjF/OqbGVOnPzzEqPlUWsR9jykmjBHkgeC9vSoMeGMdx+pUV7D1AXrXGs3ALOuBoUcBR00JPwIQ69wSZ/WFL9C9+gPfIvO802NEuckEYQB1eHVQFnhxdNF+slii+RBZ7UqnH3Fzvai4rPq5Yd/+Enxa8Gd3dqkO0QmkP5OSPzECAwEAAQ==";

      EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.decode(pub));
      PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
      System.out.println(publicKey.toString());

prints

Sun RSA public key, 4096 bits
  modulus: 645275390326802305796722819034088836976003725806021799628037897843868932719697802965877145717247604947518842882814179785936681008636656886696758084371867917154441640743545144537941469156397615593045890816454600969825776385363330066934758142523874777834719518858990349742860129814224119445706989715051290080318067004820787705472378166728845309170446353424808231545833588671651578224735883665484576289759618393886858881290500288552946853567710052280601585965384697980882826473356107237649366462179698852297230881475985712110361026313416993822664229340960345558719494253800209514972242058636429960367372444249155692344002157750139875246802393322163057667798966321303985019789296465668158272400553481441180029628521163495951016164475613785872069444579865424438846320202922474155887130904562211237290305196733484966015542544655020509813671198968192930065626107661751969777360593888232334708060382538847449808353912116208484177712302818612015731798438236119340776752076853990527431276144042648865410627856065954817472781997274414926614538925504604790427085339097234337959337523733852299076253282194017094151841464632054573385068930613865263464747141946012633907103277931571513822894938524552734133383957013866154414802157710432954404192049

Dart

The code at the top of the answer produces:

645275390326802305796722819034088836976003725806021799628037897843868932719697802965877145717247604947518842882814179785936681008636656886696758084371867917154441640743545144537941469156397615593045890816454600969825776385363330066934758142523874777834719518858990349742860129814224119445706989715051290080318067004820787705472378166728845309170446353424808231545833588671651578224735883665484576289759618393886858881290500288552946853567710052280601585965384697980882826473356107237649366462179698852297230881475985712110361026313416993822664229340960345558719494253800209514972242058636429960367372444249155692344002157750139875246802393322163057667798966321303985019789296465668158272400553481441180029628521163495951016164475613785872069444579865424438846320202922474155887130904562211237290305196733484966015542544655020509813671198968192930065626107661751969777360593888232334708060382538847449808353912116208484177712302818612015731798438236119340776752076853990527431276144042648865410627856065954817472781997274414926614538925504604790427085339097234337959337523733852299076253282194017094151841464632054573385068930613865263464747141946012633907103277931571513822894938524552734133383957013866154414802157710432954404192049

Note that the modulus printed each time is identical.

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • In above java code we have made use of X509EncodedKeySpec & KeyFactory with algo but i dont see same in the code which is here....Please let me know abt it? – Hemavathi Jan 07 '22 at 06:02
  • Any help is appreciated – Hemavathi Jan 07 '22 at 06:02
  • final public = CryptoHelper() .getPublicKeyFromBase64EncodedKey( value.encodedPublicKey); print("public" + public.modulus); at this line i m getting error as the argument type int cant be assigned to type int – Hemavathi Jan 07 '22 at 07:39
  • 1
    You said in the comment that algo is RSA, which is included in the `-----Begin RSA` string. Does your base64 string start with `MII...`? That makes no sense as int can be assigned to int! Please update your question with the Dart code you are now using and additional details above. – Richard Heap Jan 07 '22 at 12:13
  • public.modulus value which i got in flutter & value got in android both are different – Hemavathi Jan 12 '22 at 09:32
  • RSAPublicKey getPublicKeyFromBase64EncodedKey(String b64) { final pem = '-----BEGIN RSA PUBLIC KEY-----\n$b64\n-----END RSA PUBLIC KEY-----'; return CryptoUtils.rsaPublicKeyFromPem(pem); } – Hemavathi Jan 12 '22 at 09:32
  • value which i got in fluter public.modulus is only contains numbers or digits but value which i got in android is alpha numeric @Richard Heap please help – Hemavathi Jan 12 '22 at 09:34
  • Hi @Richard Heap i have pasted complete code in java what i want to achieve is as below:key = CryptoHelper.getPublicKeyFromBase64EncodedKey("RSA", keyInfo.getRsaPubKey()); char[] encPwd = Objects.requireNonNull(CryptoHelper.encrypt(Objects.requireNonNull(binding.textInputLayoutPassword.getEditText()).getText().toString(), CryptoHelper.ALGO_FOR_RSA, key)).toCharArray(); LoginRequest.setPassword(Arrays.toString(encPwd)); – Hemavathi Jan 12 '22 at 09:48
  • Any help appreciated! – Hemavathi Jan 12 '22 at 09:48
  • 1
    @Hemavathi You need to action some of the earlier comments. Does your base64 key start MII? Suggest you add it to the question in full. Update the question showing the Dart code you have created so far. Are you comparing like with like? The modulus will be a number, but what are you comparing with in Java? – Richard Heap Jan 12 '22 at 13:22
  • 1
    @Hemavathi Actually, I now see that you have edited the question and added a whole bunch more Java code. This is "question creep", and is unfair to answerers. Your original question was "how do I get a public key from a base 64 encoded representation" and that has been answered. You should accept the answer and ask a new question for any follow ups, like the new bits of Java code. – Richard Heap Jan 12 '22 at 13:46
  • Sorry i have edit my question we can go one by one. – Hemavathi Jan 13 '22 at 07:48
  • base 64 value starts with MIIB only, but in my question i have pasted equivalent java code above i am just printing getPublicKeyFromBase64EncodedKey key value & getPublicKeyFromBase64EncodedKey dart key value both looks to be different that is java key value is alpha numeric but dart key value is number.My doubt is should it not be same? – Hemavathi Jan 13 '22 at 07:53
  • 1
    Well, they should be the same if you are printing the same thing. Update question to show the Java `println` (or equivalent) statement you are using and the results from Dart and Java. – Richard Heap Jan 13 '22 at 14:31
  • I have updated my question taking same key & showed output for both java & dart please take a look & see key value in java not matching with dart.what is that i m missing.Please help – Hemavathi Jan 16 '22 at 06:19
  • I have already pasted java method & dart code which is being used currently in above question.Let me know what i m missing in dart method? – Hemavathi Jan 16 '22 at 06:21
  • Please help in solving above problem – Hemavathi Jan 16 '22 at 06:21
  • 1
    Those are the same number!! One's printed in hex, one's printed in decimal. Use https://www.rapidtables.com/convert/number/hex-to-decimal.html to confirm. I just did and they are the same. If you want the Dart code to print in hex, change it to this: `print(public.modulus!.toRadixString(16));` – Richard Heap Jan 16 '22 at 13:06
  • ok let me check & update here – Hemavathi Jan 17 '22 at 11:22
  • Thank you so much values are matching now. – Hemavathi Jan 18 '22 at 13:00
  • I have added one more question link: https://stackoverflow.com/questions/70756082/how-to-convert-encrypt-java-code-below-in-flutter.Please help with this query if i get answer for this i will complete my task & submit it.Please help – Hemavathi Jan 18 '22 at 13:06
-1

I think MethodChannel is one possible solution for you. check it out: platform-channels

reza
  • 1,354
  • 1
  • 7
  • 25
  • Not possible in flutter? – Hemavathi Jan 04 '22 at 11:35
  • I am implementing crypto applications too and we are using MethodChannel because packages in dart are not complete. – reza Jan 04 '22 at 11:40
  • can u send MethodChannel sample code – Hemavathi Jan 04 '22 at 11:43
  • MethodChannel means from flutter we access android code which is same flutter project? – Hemavathi Jan 04 '22 at 11:56
  • yes. it is. see the link provided in the answer. it has a very good sample code and complete instructions. please accept the answer if it was helpful. thanks :) – reza Jan 04 '22 at 12:37
  • can we pass values back to flutter again using method channel? – Hemavathi Jan 05 '22 at 04:11
  • @Hemavathi yes. of course. – reza Jan 05 '22 at 07:00
  • Why use a MethodChannel for a simple feature that's available in Dart? The question asker will now need to find and test the same implementation in Swift of iOS. Then, when they want to try Flutter Web, they'll have to implement it in, you guessed it, Dart (or Javascript). MethodChannels should be reserved for platform specific features (or access to a 3rd party library that exists for all your target platforms). – Richard Heap Jan 05 '22 at 13:23
  • can we have more than one method channel – Hemavathi Jan 05 '22 at 13:51
  • @RichardHeap can u help me in finding above code conversion in flutter any help is appreciated i m stuck for a week, need to complete this asap.please help – Hemavathi Jan 05 '22 at 13:54