Crypto X509EncodedKeySpec equivalent code in Flutter

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!


Solution 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.