我已经生成了公钥和私钥
Uint8List generatePrivateKey(String input1, String input2) {
// Concatenate the inputs to create the seed
String seed = input1 + input2;
// Create a SHA-256 digest of the seed
Digest sha256Digest = SHA256Digest();
Uint8List seedBytes = Uint8List.fromList(seed.codeUnits);
Uint8List digest = sha256Digest.process(seedBytes);
// Create an ECC private key using the secp256k1 curve
ECCurve_secp256k1 curve = ECCurve_secp256k1();
ECPrivateKey privateKey =
ECPrivateKey(BigInt.parse(hex.encode(digest), radix: 16), curve);
// Make sure the private key is within the valid range for the curve
BigInt n = curve.n;
BigInt privateKeyInt = privateKey.d!;
privateKeyInt = privateKeyInt % n;
String privateKeyHex = privateKeyInt.toRadixString(16).padLeft(64, '0');
Uint8List privateKeyBytes = Uint8List.fromList(hex.decode(privateKeyHex));
return privateKeyBytes;
}
ECPublicKey generatePublicKey(Uint8List privateKeyBytes) {
ECCurve_secp256k1 curve = ECCurve_secp256k1();
ECPrivateKey privateKey = ECPrivateKey(
BigInt.parse(hex.encode(privateKeyBytes), radix: 16), curve);
ECPoint? publicKeyPoint = curve.G * privateKey.d!;
return ECPublicKey(publicKeyPoint, curve);
}
这就是我使用 pointycastle 库生成密钥的方式,现在我想创建一个 X509 证书。
我尝试过X509库,dart:io,密码学库。for rg
import 'dart:io';
import 'dart:typed_data';
import 'package:asn1lib/asn1lib.dart';
import 'package:cryptography/cryptography.dart';
Future<X509Certificate> generateSelfSignedCertificate(Uint8List privateKeyBytes,final publicKey) async {
// final publicKey = generatePublicKey(privateKeyBytes);
final certificate = X509Certificate(
subject: X509DistinguishedName(commonName: 'Self-Signed'),
issuer: X509DistinguishedName(commonName: 'Self-Signed'),
` serialNumber: DateTime.now().millisecondsSinceEpoch,
validity: X509Validity(
notBefore: DateTime.now(),
notAfter: DateTime.now().add(Duration(days: 365)),
),
subjectPublicKey: await publicKey.extractRawSubjectPublicKey(),
issuerPrivateKey: privateKeyBytes,
);
return certificate;
}
我在
X509DistinguishedName
上收到错误,它未定义
为了生成证书,过程是:
当证书是自签名时,用于签署证书的私钥与证书的公钥相匹配。
如果您只想生成自签名证书,可以使用 basic_utils 包,它提供了 CryptoUtils 和
String generateSelfSignedCertificate() {
var pair = CryptoUtils.generateEcKeyPair();
var privKey = pair.privateKey as ECPrivateKey;
var pubKey = pair.publicKey as ECPublicKey;
var dn = {
'CN': 'Self-Signed',
};
var csr = X509Utils.generateEccCsrPem(dn, privKey, pubKey);
var x509PEM = X509Utils.generateSelfSignedCertificate(
privKey,
csr,
365,
);
return x509PEM;
}
generateSelfSignedCertificate
代码 来编写它