在使用 RSA 算法的 FLUTTER - DART 中,我得到了一个私钥。我有这种方法可以将 privateKey 编码为 PKCS#1 格式的 PEM,但我需要一种将 privateKey 编码为 PKCS#8 格式的方法。我在互联网上找不到任何相关信息。
Uint8List _encodePrivateKeyToPemPKCS1(RSAPrivateKey privateKey) {
var topLevel = new ASN1Sequence();
var version = ASN1Integer(BigInt.from(0));
var modulus = ASN1Integer(privateKey.n!);
var publicExponent = ASN1Integer(privateKey.exponent!);
var privateExponent = ASN1Integer(privateKey.privateExponent!);
var p = ASN1Integer(privateKey.p!);
var q = ASN1Integer(privateKey.q!);
var dP = privateKey.privateExponent! % (privateKey.p! - BigInt.from(1));
var exp1 = ASN1Integer(dP);
var dQ = privateKey.privateExponent! % (privateKey.q! - BigInt.from(1));
var exp2 = ASN1Integer(dQ);
var iQ = privateKey.q!.modInverse(privateKey.p!);
var co = ASN1Integer(iQ);
topLevel.add(version);
topLevel.add(modulus);
topLevel.add(publicExponent);
topLevel.add(privateExponent);
topLevel.add(p);
topLevel.add(q);
topLevel.add(exp1);
topLevel.add(exp2);
topLevel.add(co);
return topLevel.encodedBytes;
}
最佳答案
这段代码对我有用。谢谢托帕科
https://raw.githubusercontent.com/Ephenodrom/Dart-Basic-Utils/master/lib/src/CryptoUtils.dart
https://raw.githubusercontent.com/Ephenodrom/Dart-Basic-Utils/master/lib/src/StringUtils.dart
关于flutter - 在 Dart 中将 privateKey 转换为 PKCS#8 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71492016/