SwiftRSA is a simple wrapper of Apple Security API which makes RSA encryption and decryption easy to use. It is motivated by creating the app of boxueio.com.
- Fully tested.
- Inituitive interface.
- Updated to Swift 5.
let pemPrivate = """
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA0HRZppSvYCAFTo+ie5z7EXmzFJ9rSpuUJUAAvikOq/lGVqaK
...
2bhAIUk7eWJTorZYujzXO+HmDt+8/ha+RBAtgQPDFPGHG/QaZik8
-----END RSA PRIVATE KEY-----
"""
let pemPublic = """
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0HRZppSvYCAFTo+ie5z7
...
mwIDAQAB
-----END PUBLIC KEY-----
"""
guard let privateKey = PrivateKey(pemEncoded: pem) else {
// Invalid pem string
}
guard let publicKey = PublicKey(pemEncoded: pemPublic) else {
// Invalid pem string
}
guard let privateKey = PrivateKey(der: data) else {
// Invalid DER data
}
guard let publicKey = PublicKey(der: data) else {
// Invalid DER data
}
let ct = ClearText(string: "Hello world")
let encrypted = try ct.encrypted(with: publicKey, by: .rsaEncryptionOAEPSHA512)
/// `encrypted` is an `EncryptedText` object.
/// `originText` is a `ClearText` object
let originText = try encrypted.decrypted(with: privateKey)
let data = encrypted.data
let string = originText.stringValue
To integrate SwiftRSA into your Xcode project using Carthage, speicify the following line in your Cartfile
:
github "puretears/SwiftRSA" ~> 0.1
- iOS 10.0+
- Swift 4.0+
- Cocoapods and SPM support;
- More RSA algorithm support;
- Add X.509 certificate support;
- Add sign and verify support;
- 0.1
- Initial release
SwiftRSA is released under the MIT license. See LICENSE for details.