0

I have a project on who want to generate a public and private key with RSA and I use SwiftyRSA, but it just can export PEM, base64 and data format. Now how can I convert those formate to XML?

   do {
        let keyPair = try SwiftyRSA.generateRSAKeyPair(sizeInBits: 2048)
        let privateKey = keyPair.privateKey
        let publicKey = keyPair.publicKey

        print("pemString:==============\npr-> \(try! privateKey.pemString())")
        print("base64String:==============\npr-> \(try! privateKey.base64String())")
        print("data:==============\npr-> \(try! privateKey.data())")

    } catch {
        print("error -> No key generated")
    }
Ibrahim
  • 1
  • 3

1 Answers1

0

The XML file is simply a text with some structure. In your case it's:

<RSAKeyValue>
    <Modulus>
        ...
    </Modulus>
    <Exponent>
        ...
    </Exponent>
</RSAKeyValue>

So you can simply extract modulus and exponent from string returned by pemString() and put it in your XML template.

Can I get the modulus or exponent from a SecKeyRef object in Swift?

Andrey Gershengoren
  • 896
  • 1
  • 5
  • 18