flutterrsapublic-key-encryptionencryption-asymmetric

Read a PEM file with Flutter API encrypt


import 'package:encrypt/encrypt.dart';
import 'package:encrypt/encrypt_io.dart';
import 'dart:io';
import 'package:pointycastle/asymmetric/api.dart';
import 'dart:async';
import 'package:flutter/services.dart' show rootBundle;

class Encrypt {
  Future<String> loadPrivateKey() async {
    return await rootBundle.loadString('assets/private_key.pem');
  }

  Future<String> loadPublicKey() async {
    return await rootBundle.loadString('assets/public_key.pem');
  }

  encryptString() async {
    print(loadPublicKey().toString());
    final publicKey =
        await parseKeyFromFile<RSAPublicKey>('${loadPublicKey()}');
    final privateKey =
        await parseKeyFromFile<RSAPrivateKey>('${loadPrivateKey()}');

    final plainText = 'James Bond';
    final encrypter =
        Encrypter(RSA(publicKey: publicKey, privateKey: privateKey));

    final encrypted = encrypter.encrypt(plainText);
    final decrypted = encrypter.decrypt(encrypted);

    print(decrypted);
    print(encrypted.base64);
  }
}

ERROR: Performing hot reload... Syncing files to device AOSP on IA Emulator... Reloaded 8 of 707 libraries in 1,021ms. I/flutter ( 7395): Instance of 'Future' E/flutter ( 7395): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FileSystemException: Cannot open file, path = 'Instance of 'Future'' (OS Error: No such file or directory, errno = 2)

I did add assets in yaml file as:

flutter:
  assets:
    - assets/

Solution

  • parseKeyFromFile is a convenience function that reads a file and parses the contents. You don't have a file, you have an asset that you are already doing the work of reading into a string. Having read the file, it just parses it - and that's what you need.

    This should work:

    final publicPem = await rootBundle.loadString('assets/public_key.pem');
    final publicKey = RSAKeyParser().parse(publicPem) as RSAPublicKey;
    

    and similar for the private key.