1

I have added pointycastle and generated a keypair, encrypting the trial "Hello World" string. From this, I want to get the values of the Private and Public Key. Is there anywhere they are stored, because whenever I try to print the values of keyPair.privateKey, it returns Instance of 'RSAPrivateKey.

Here is the code I used

        var keyParams = new RSAKeyGeneratorParameters(new BigInt.from(65537), 2048, 5);
        var secureRandom = new FortunaRandom();
        var random = new Random.secure();
        List<int> seeds = [];
        for (int i = 0; i < 32; i++) {
          seeds.add(random.nextInt(255));
        }
        secureRandom.seed(new KeyParameter(new Uint8List.fromList(seeds)));

        var rngParams = new ParametersWithRandom(keyParams, secureRandom);
        var k = new RSAKeyGenerator();
        k.init(rngParams);
        var keyPair = k.generateKeyPair();
        var cipher = new RSAEngine()..init( true, new PublicKeyParameter<RSAPublicKey>(keyPair.publicKey));
        print("pubkey: ${keyPair.publicKey.toString()}");
        var cipherText = cipher.process(new Uint8List.fromList("Hello World".codeUnits));
        print("Encrypted: ${new String.fromCharCodes(cipherText)}");
        cipher.init( false, new PrivateKeyParameter<RSAPrivateKey>(keyPair.privateKey));
        //cipher.init( false, new PrivateKeyParameter(keyPair.privateKey) )
        var decrypted = cipher.process(cipherText);
        print("Decrypted: ${new String.fromCharCodes(decrypted)}");
Epic Gamer_1
  • 104
  • 3
  • 12

1 Answers1

0

Make sure to import package:pointycastle/asymmetric/api.dart, then use:

  var k = RSAKeyGenerator()..init(rngParams);
  AsymmetricKeyPair<PublicKey, PrivateKey> keyPair = k.generateKeyPair();
  RSAPrivateKey privateKey = keyPair.privateKey;
  RSAPublicKey publicKey = keyPair.publicKey;
  print(privateKey.d); // prints private exponent
  print(publicKey.n); // prints modulus

Recreate from the individual parts:

  RSAPrivateKey foo = RSAPrivateKey(
    privateKey.n,
    privateKey.d,
    privateKey.p,
    privateKey.q,
  );
  RSAPublicKey bar = RSAPublicKey(publicKey.n, publicKey.e);
Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • From this, can i recreate a key from just the modulus of public key and exponent of private key? – Epic Gamer_1 Mar 19 '19 at 16:38
  • Both need the modulus and exponent. The private key also needs `p` and `q`. Check the constructors of `RSAPrivateKey` and `RSAPublicKey`. – Richard Heap Mar 19 '19 at 16:59
  • I know you can do that, but from the value of the key, can you make it? (From a single key value?) – Epic Gamer_1 Mar 19 '19 at 19:27
  • It doesn't look like it, but try it by supplying dummy values instead. (Incidentally, PKCS#1 format includes `p` and `q` so if you're storing your key using that format you'll have those numbers available.) – Richard Heap Mar 19 '19 at 21:31
  • also, can I generate a .pem file for this? – Epic Gamer_1 Mar 20 '19 at 11:12
  • Yes, but you might need to write that yourself using the asn package. It's probably easier to do it the other way round. Generate the key pair elsewhere, save as pem, then parse that in dart and use that to construct the pointy private or public key. – Richard Heap Mar 20 '19 at 16:12