The Encryption
framework is a Swift package that provides data encryption and decryption using the AES
and RSA
algorithms.
To install the framework using the Swift Package Manager
, follow these steps:
- Open your project in Xcode.
- Select the
xcodeproject
file in the navigator. - In the "Frameworks, Libraries, and Embedded Content" section, press the
+
button. - Select
Add Other...
, thenAdd package dependency
. - Enter the framework URL in the search field: https://github.com/qodeca/Encryption
- Press
Add package
and confirm in the dialog window by pressingAdd package
.
To use the encryption features in your code, you need to import the Encryption
framework using import Encryption
.
To use AES
or RSA
encryption, you will need to provide encryption keys to these algorithms. You can load these keys from a String
or from a file in the application bundle. Be sure not to store sensitive keys in unencrypted form, as they could be retrieved from the application.
import Encryption
import CommonCrypto
class AESWrapper {
private let aes: AES
init(aesKey: AESKey) {
self.aes = .init(key: aesKey, options: CCOptions(ccPKCS7Padding))
}
func encrypt(message: String) {
let decrypted = try! DecryptedValue(decryptedString: message)
let encrypted = try! decrypted.encrypted(using: aes)
print(encrypted)
}
func decrypt(message: String) {
let encrypted = try! EncryptedValue(encryptedString: message)
let decrypted = try! encrypted.decrypted(using: aes)
print(try! decrypted.string())
}
}
let aesKey = AESKey(
secret: "t6w9z$C&F)J@NcRf",
initialVector: "6CF105AB-4D16-44"
)
let aesWrapper = AESWrapper(aesKey: aesKey)
aesWrapper.encrypt(message: "Hello World!")
aesWrapper.decrypt(message: "aE0Pq1ddC6a1agsa0RI2NQ==")
import Encryption
import CommonCrypto
class RSAWrapper {
private let rsa: RSA
init(privateKey: RSAKey, publicKey: RSAKey) {
self.rsa = .init(publicKey: publicKey, privateKey: privateKey, padding: .OAEP)
}
func encrypt(message: String) {
let decrypted = try! DecryptedValue(decryptedString: message)
let encrypted = try! decrypted.encrypted(using: rsa)
print(encrypted.base64String)
}
func decrypt(message: String) {
let encrypted = try! EncryptedValue(encryptedString: message)
let decrypted = try! encrypted.decrypted(using: rsa)
print(try! decrypted.string())
}
}
let rsaPublicKeyClient = try! RSAKey(
publicKey: """
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB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9APnfB41tCAGTz0eGdCqErSNveLbzCxgsdlJhoprvhm9p1v22wJATutu
D1RRloffjCWbP6518AZx/vnZrCxJACEec0n3UFh/cF/NMa9sRc51+L7KlXYW5xfr
EZqnaEDfBM1GDnzi+wJBAML1lFZoKg0cPNAqKXGIvrfI9jPUgI4G8ITLPNkioZ0t
bma0mK2oKM5Y3IISCtf99yJswvf4FFf521vftr+s7yg=
"""
)
let rsaPrivateKeyServer = try! RSAKey(
privateKey: """
MIICXgIBAAKBgQCTI9m6COiT141l0ROqI548PDlU5IfkAI2VzKsC3AbXU8SVky1S
VAbHPYqTWxJA5qpZgqO402XojmEIr2dk752udgB5IB3PTzPT5D27QZTRKmTnZ7kB
ovdQIJnTDvnU6F3Nu2PPh+dosi6R7qzKLm5LgSOFL4JLwC5rCy2+kvk4hwIDAQAB
AoGAImHUNKZ0QmeyAMK0R6N/DDA+bVnhbyO58fEbXNWxO4u1egYkJwK/ersksH4t
a8D6uWPPghbTz13FytPB41IilBBtpJGYz/9A2EMz6+wdQkBvmx130Wbnlr27jQPv
rXXJM9UVRXMFjnspE5u+OnhWPwFAaiB8SAEoeUZkVGVg+VECQQDZr4hSaWt2HsCW
gGzARiWgPwhpiTSqWctBacElcCx9pNpdGLzV18CjoV8D0Jo62gvJnazLFUhbWTF0
pr9dp0UbAkEArQmtYYY5p2LZh6Slu37GUy+KdIaLw4ozTjclNkGEwJoZE5We6Rv3
4ksFbyi2gv8ljACOdLN7mLaxZifdlD+NBQJBAJMyAXEQfayqkLlz75V4GVspJCwQ
rf7+ptT9iLAjEMKI5WsMHixPLqC2roPq208uP8g+CShtpLa4MhvZ4Q6X278CQQCo
Ke55B+RB+zwiqe1zIQqGz34ELrnniAjCa69bYiMsttXGBbORImAuaPBYDj4JYwNP
Yz8OxVtJl8sh135s07ItAkEAij7iUcAjHrdMYrfAzhL190fm5G0v7ji7SG8ix38U
FPMsJKfgpYJ/1zHdhJw57R86zbizpfaF06VFLm7x6XKV7g==
"""
)
let rsaWrapperClient = RSAWrapper(privateKey: rsaPrivateKeyClient, publicKey: rsaPublicKeyClient)
let rsaWrapperServer = RSAWrapper(privateKey: rsaPrivateKeyServer, publicKey: rsaPublicKeyServer)
rsaWrapperServer.encrypt(message: "Hello World!")
rsaWrapperClient.decrypt(message: "aNlCRLZa1msSqQkCm0INRvgx++TKZ695pMwXPgmTOhmJceaJf4nEikyh5jbdH9cYEx8wQLbsraE16Ri86luoFd02mcRT0EBEXan2mRYnJVZhbRkdxgFJVipvTAt/fAp6+JcvoquN7boJvH2zzX3RxPkNbsGNqhUNaVk58p87YWc=")