Sha*_*hun 9 android cryptography public-key-encryption dart flutter
我需要在我的颤振应用程序中生成一个密钥对,但似乎没有任何库可以这样做。有一个名为RSA 的库,它可以解析一对公钥/私钥,并能够使用它们加密和解密字符串,但它不能生成新的 KeyPair(最好从给定的字符串)。
我如何首先生成密钥?我错过了什么吗?
有一个Dart2和颤振兼容的预发布Pointycastle可用。
默认 README.md 指向第一个非预发布版本,因此首页显示“DART 2 INCOMPATIBLE”,但这仅适用于 version < 0.11.1。
只需添加到 pubspec.yaml
dependencies:
pointycastle: ^1.0.0-rc4
Run Code Online (Sandbox Code Playgroud)
我开始使用一些示例代码来使用尖城堡生成公钥/私钥对。经过长时间的调试和编辑代码,我最终得到了以下代码:
var keyParams = new RSAKeyGeneratorParameters(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();
print(new RsaKeyHelper().encodePublicKeyToPem(keyPair.publicKey) );
print(new RsaKeyHelper().encodePrivateKeyToPem(keyPair.privateKey) );
AsymmetricKeyParameter<RSAPublicKey> keyParametersPublic = new PublicKeyParameter(keyPair.publicKey);
var cipher = new RSAEngine()..init(true, keyParametersPublic);
var cipherText = cipher.process(new Uint8List.fromList("Hello World".codeUnits));
print("Encrypted: ${new String.fromCharCodes(cipherText)}");
AsymmetricKeyParameter<RSAPrivateKey> keyParametersPrivate = new PrivateKeyParameter(keyPair.privateKey);
cipher.init( false, keyParametersPrivate )
;
var decrypted = cipher.process(cipherText);
print("Decrypted: ${new String.fromCharCodes(decrypted)}");
Run Code Online (Sandbox Code Playgroud)
我使用了这个人在这个 git 问题页面中的编辑(在这里查看更多功能,例如从 PEM 编码/解码
| 归档时间: |
|
| 查看次数: |
10823 次 |
| 最近记录: |