PointyCastle / pointycastle

Moved into the Bouncy Castle project: https://github.com/bcgit/pc-dart
MIT License
271 stars 75 forks source link

How to get values of RSA Public & Private Key? #176

Open EpicGamer1YT opened 5 years ago

EpicGamer1YT commented 5 years ago

After generating a key pair and encrypting the test "Hello World" String, how do I get the values of the generated key pair(private and public key values)? it only tells me Instance of 'RSAPublicKey' when I try to print it to the console.

Here is my sample code:

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)}");
peter-meemo commented 5 years ago

API for AsymmetricKeyPair is here: https://pub.dartlang.org/documentation/pointycastle/latest/pointycastle.api/AsymmetricKeyPair-class.html

You can see that the keypair object has .privateKey and .publicKey members, and each of those has BigInt members for its values.

PITR-DEV commented 5 years ago

I would like to know too.

richardheap commented 5 years ago

See SO question/answer here

hoylen commented 4 years ago

how do I get the values of the generated key pair

Cast them into their actual classes. Cast the publicKey member from a PublicKey into an RSAPublicKey, and the privateKey member from a PrivateKey into an RSAPrivateKey.

The example below uses the Dart as keyword to perform this casting.

RSAKeyGenerator keyGen = ...

final pair = keyGen.generateKeyPair(); // produces an AsymmetricKeyPair

// Examine the generated key-pair

final rsaPublic = pair.publicKey as RSAPublicKey;
final rsaPrivate = pair.privateKey as RSAPrivateKey;

print('Generated ${keyGen.algorithmName} key:');
print('  Public:');
print('    e = ${rsaPublic.exponent}'); // public exponent
print('    n = ${rsaPublic.modulus}');
print('  Private: n.bitlength = ${rsaPrivate.modulus.bitLength}');
print('    n = ${rsaPrivate.modulus}');
print('    d = ${rsaPrivate.exponent}'); // private exponent
print('    p = ${rsaPrivate.p}'); // the two prime numbers
print('    q = ${rsaPrivate.q}');

Since this is an RSA key pair, the numbers have these properties: