1

I need to use something similar to this php function openssl_public_encrypt('text', $output, $publicKey, OPENSSL_PKCS1_PADDING); in Flutter (Dart). What is the easiest way to accomplish that? Thank you!

1 Answers1

2

You can use encrypt library.

import 'dart:io';
import 'package:encrypt/encrypt.dart';

final plainText = 'Lorem ipsum dolor sit amet.';
final publicKey = RSAKeyParser().parse(File('public.pem').readAsStringSync());
final privateKey = RSAKeyParser().parse(File('private.pem').readAsStringSync());

final encrypter = Encrypter(
  RSA(
    publicKey: publicKey,
    privateKey: privateKey,
    encoding: RSAEncoding.PKCS1,
  ),
);

final encrypted = encrypter.encrypt(plainText);
final decrypted = encrypter.decrypt64(encrypted.base64);
janstol
  • 2,857
  • 1
  • 18
  • 21