I'm trying to export Diffie-Hellman(modular version) keys to PEM format, using PointyCastle, since it offers ASN1 serialization/deserialization.
This is my method for the conversion (for DhPublicKey):
Uint8List toSpki() {
ASN1Sequence outer = ASN1Sequence();
ASN1Sequence algorithmIdentifier = ASN1Sequence();
algorithmIdentifier.add(ASN1ObjectIdentifier([1, 2, 840, 10046, 2, 1]));
ASN1Sequence parameters = ASN1Sequence();
parameters.add(ASN1Integer(parameterSpec.p));
parameters.add(ASN1Integer(parameterSpec.g));
parameters.add(ASN1Integer.fromtInt(parameterSpec.length));
algorithmIdentifier.add(parameters);
outer.add(algorithmIdentifier);
outer.add(ASN1BitString(stringValues: ASN1Integer(key).encode()));
return outer.encode();
}
Then the result will be base64 encoded and inserted into the PEM header and footer:
String toPem() {
return '-----BEGIN PUBLIC KEY-----\r\n${base64Encode(toSpki())}\r\n-----END PUBLIC KEY-----';
}
And this is the result (without the blank line after the header and before the footer):
-----BEGIN PUBLIC KEY-----
MIIBIjCBmAYIKoZIhvcNAQMwgYsCgYEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxObIlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjftawv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5lOB//////////8CAQICAgEAA4GEAAKBgFRUUmCOBTZJGPda+86/cqlvlbQ9pU2xMzLJPID7gqcUb29aUnRtx3iqKER6SbbAqJJ17gjWsIR4kIyX/RnU+FiNfSlinoXTWOrHGdO30FMlMr6JopFprXzW48dEZYayjXEMluEJ+JpbUY+ey4O3gNEM+L5Zy19e7XCMn6SXRvQG
-----END PUBLIC KEY-----
Which is clearly not right.
What am I doing wrong?