mohammad1408
mohammad1408

Reputation: 53

how to find or creat private.pem and public.pem in flutter

I want use rsa in flutter I have the following code for flutter

But I do not know about the part test/private.pem and test/public.pem how it is made in flutter

Of course, I have private and public keys that are made in Java with a length of 1024

Can I put them here? Or not, and must the PEM file be created? How do I generate a PEM file?

Thank you for your help

Future<void> main () async {

  final publicKey = await parseKeyFromFile<RSAPublicKey>('test/public.pem');
  final privKey = await parseKeyFromFile<RSAPrivateKey>('test/private.pem');

  final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
  final encrypter = Encrypter(RSA(publicKey: publicKey, privateKey: privKey));

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

  print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elit
  print(encrypted.base64); // kO9EbgbrSwiq0EYz0aBdljHSC/rci2854Qa+nugbhKjidlezNplsEqOxR+pr1RtICZGAtv0YGevJBaRaHS17eHuj7GXo1CM3PR6pjGxrorcwR5Q7/bVEePESsimMbhHWF+AkDIX4v0CwKx9lgaTBgC8/yJKiLmQkyDCj64J3JSE=
}

Upvotes: 1

Views: 4004

Answers (2)

narayann
narayann

Reputation: 481

there are 2 ways to set publicKey and privateKey.

  1. from your project :

    create a folder named as test/ inside it like this

    inside private.pem and public.pem paste your private and public key respectively . and get it using

   final publicKey = await parseKeyFromFile<RSAPublicKey>('test/public.pem');
   final privKey = await parseKeyFromFile<RSAPrivateKey>('test/private.pem');

above approach works good when public and private keys are constant

  1. from string :
import 'package:encrypt/encrypt.dart';
import 'package:pointycastle/asymmetric/api.dart';

String privateKeyString="key goes here";
String publicKeyString="key goes here";

//create a instance of RSA key praser    
RSAKeyParser keyParser = RSAKeyParser();

//and parse those string keys
RSAAsymmetricKey privateKeyParser = keyParser.parse(privateKeyString);
RSAAsymmetricKey publicKeyParser =keyParser.parse(publicKeyString);

final publicKey = RSAPublicKey(publicKeyParser.modulus!, publicKeyParser.exponent!);
final privKey;

if (privateKeyParser is RSAPrivateKey) {

privKey = RSAPrivateKey(privateKeyParser.modulus!,privateKeyParser.exponent!, privateKeyParser.p,privateKeyParser.q);
final plainText = 'hello world';

final encrypter = Encrypter(RSA(publicKey: publicKey, privateKey:privKey));

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

}

it worked for me!!

Upvotes: 4

d1nch8g
d1nch8g

Reputation: 619

You can use pointy castle package to do that: https://pub.dev/packages/pointycastle Just generate a key pair (code below) and use it in your app. Good luck!

import 'package:pointycastle/export.dart';
import 'dart:math';
import 'dart:typed_data';


AsymmetricKeyPair<RSAPublicKey, RSAPrivateKey> generateRSAkeyPair(
    SecureRandom secureRandom,
    {int bitLength = 2048}) {
  // Create an RSA key generator and initialize it

  final keyGen = RSAKeyGenerator()
    ..init(ParametersWithRandom(
        RSAKeyGeneratorParameters(BigInt.parse('65537'), bitLength, 64),
        secureRandom));

  // Use the generator

  final pair = keyGen.generateKeyPair();

  // Cast the generated key pair into the RSA key types

  final myPublic = pair.publicKey as RSAPublicKey;
  final myPrivate = pair.privateKey as RSAPrivateKey;

  return AsymmetricKeyPair<RSAPublicKey, RSAPrivateKey>(myPublic, myPrivate);
}

SecureRandom exampleSecureRandom() {
  final secureRandom = FortunaRandom();

  final seedSource = Random.secure();
  final seeds = <int>[];
  for (int i = 0; i < 32; i++) {
    seeds.add(seedSource.nextInt(255));
  }
  secureRandom.seed(KeyParameter(Uint8List.fromList(seeds)));

  return secureRandom;
}

// here is how you generate key pair
final pair = generateRSAkeyPair(exampleSecureRandom());

final public = pair.publicKey; // to get public
final private = pair.privateKey; // i know, you get it :D

Upvotes: 0

Related Questions