从KeyPair Generation Flutter获取RSA公钥或私钥

时间:2019-03-08 16:08:43

标签: dart flutter public-key-encryption pointycastle

我添加了pointycastle并生成了一个密钥对,对试用版“ Hello World”字符串进行了加密。由此,我想获取私钥和​​公钥的值。它们存储在任何地方,因为每当我尝试打印get_beam_parses的值时,它都会返回keyPair.privateKey

这是我使用的代码

Instance of 'RSAPrivateKey

1 个答案:

答案 0 :(得分:0)

确保导入package:pointycastle/asymmetric/api.dart,然后使用:

  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

从各个部分重新创建:

  RSAPrivateKey foo = RSAPrivateKey(
    privateKey.n,
    privateKey.d,
    privateKey.p,
    privateKey.q,
  );
  RSAPublicKey bar = RSAPublicKey(publicKey.n, publicKey.e);