Swift RSA Public key Encryption

I would like to generate a cryptographically random key, use that to encrypt the data with a symmetric cypher (AES typically) and then encrypt the random key with RSA.

How can I do it in swift? Is it possible with CryptoKit or CommonCrypto ?

Thanks

Answered by DTS Engineer in 683924022

CryptoKit does not support RSA, because its focus is on algorithms that are efficient and secure by default, and RSA fits neither of those categories. If you insist on using RSA, you’ll have to use Security framework, and specifically:

  • SecKeyCreateRandomKey to generate a private key.

  • SecKeyCopyPublicKey to get the public key from that.

  • SecKeyCreateEncryptedData to encrypt with the public key.

  • SecKeyCreateDecryptedData to decrypt with the private key.


On the AES front, it depends on mode you’re looking for:

  • CommonCrypto supports AES-ECB and AES-CBC.

  • CryptoKit supports AES-GCM [1].


The CryptoCompatibility sample code shows many of these APIs in action.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Because the other AES modes are not secure by default.

Accepted Answer

CryptoKit does not support RSA, because its focus is on algorithms that are efficient and secure by default, and RSA fits neither of those categories. If you insist on using RSA, you’ll have to use Security framework, and specifically:

  • SecKeyCreateRandomKey to generate a private key.

  • SecKeyCopyPublicKey to get the public key from that.

  • SecKeyCreateEncryptedData to encrypt with the public key.

  • SecKeyCreateDecryptedData to decrypt with the private key.


On the AES front, it depends on mode you’re looking for:

  • CommonCrypto supports AES-ECB and AES-CBC.

  • CryptoKit supports AES-GCM [1].


The CryptoCompatibility sample code shows many of these APIs in action.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Because the other AES modes are not secure by default.

Swift RSA Public key Encryption
 
 
Q