I am trying to get public key in DER format from base64 encoded public key using swift. Is there any way to do this?
Asked
Active
Viewed 72 times
0
-
1Which DER format and which base64 format? There's about 5 of the former and more than 10 of the latter. PKCS10 is not a key format, but does _contain_ a publickey in the same SPKI format used by X.509/PKIX, and can be represented either in binary or base64 although the latter is more common because it can be cut&pasted. However PKCS10 is also signed, so it cannot be created without the privatekey. – dave_thompson_085 Jun 19 '23 at 06:02
-
Actually, I am getting the base64 encoded public key string from a native code. Then in my application, I need to get the public key in DER format. When i wanted to do this using Swift's Data, I have created a SecKey with provided Data. But When i revert the SecKey to Data, I found that both are not the same. If both be the same, my problem is resolved. – ahad alam Jun 19 '23 at 07:27
-
Firstly you need decode base64 into data. – Cy-4AH Jun 19 '23 at 09:51
-
1Agree with @dave_thompson_085 here, but in general, if you have something like `-----BEGIN
-----` followed by base64-encoded data followed by `-----END – President James K. Polk Jun 19 '23 at 16:14-----` then to get the DER format you simply strip off the BEGIN and END lines and any whitespace and base64-decode the data. -
@PresidentJamesK.Polk: unless it's `BEGIN/END PGP PUBLIC KEY BLOCK` which is almost completely unlike DER. Or `SSH2 PUBLIC KEY` per RFC4716. Or `OPENSSH PRIVATE KEY`, which isn't DER and also isn't (just) a publickey. OTOH `RSA PUBLIC KEY` _is_ DER but it's DER-PKCS1 not DER-SPKI and I don't know if Swift accepts that. – dave_thompson_085 Jun 20 '23 at 01:31