I am porting my Android app over to iOS and need to integrate encryption for communication with an existing server.
I need to be able to use AES-CBC and RSA-ECB.
My research has led me to the CommonCrypto library, but I have been stuck on this for days now, not finding how to integrate the library into my XCode project.
I am using XCode version 12.2 (not sure what version of swift comes with that though).
The methods I have tried to get CommonCrypto into the project are adding import CommonCrypto into the swift file, or adding #import <CommonCrypto/CommonCrypto.h> into the bridging header. Both of these makes XCode complain saying it cannot compile the Obective-C module.
In addition, I have not been able to find documentation explaining the correct way of using the library.
I need to :
- Generate public and private keys for AES
- Generate public and private keys for RSA
- Encrypt and decrypt with AES-CBC with PKCS5 padding
- Encrypt and decrypt with RSA-EBC with PKCS1 padding
Please note that I cannot change the encryption standards used.
Should I stick with CommonCrypto, go with OpenSSL, another fairly future proof solution ?
I would really appreciate help and guidance with this, please.
(Sorry, could not find any better tags)
CommonCrypto is an Apple Open Source C library (no lib file ?) which requires an Objective-C wrapper, hence the CryptoCompatibility sample code.
You’re a bit off in the weeds here:
-
While Common Crypto is open source, the open source code shows a lot of stuff that’s not API. If you’re using Common Crypto in a product, stick with the APIs in your platform SDK.
-
The implementation of Common Crypto is within the System framework, aka libSystem. Xcode automatically links you to that.
-
It’s always been possible to call Common Crypto from Swift.
-
Historically it was tricky to use from Swift because there was no module map, and so the obvious thing,
import CommonCrypto, didn’t work. We fixed that a while back. I don’t know if the fix is in Xcode 12, but it’s definitely in all versions of Xcode 13. -
Having said that, it’s still not easy to use from Swift, because it requires a detailed understanding of how to use C APIs from Swift. Folks who have that experience can use Common Crypto from Swift just fine. If you don’t have that experience, it would be better to start by using it from Objective-C and then work out how to interact with that code from Swift.
-
I wrote CryptoCompatibility long before Swift was a thing, which is why it doesn’t have Swift support.
I already mix C++/Objective-C++ and have a bridging header
Cool. Then do this:
-
Grab the operation from CryptoCompatibility that best matches your requirements and add its
.hand.mfiles to your product. -
Include the
.hfile in your bridging header. -
Use that operation from your Swift code.
All the CryptoCompatibility operations work in terms of types that bridge reasonably well into Swift, like NSData bridging to Data. The same can’t be said for Common Crypto (-:
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"