To encrypt data using the AES algorithm in Swift, you can use the CryptoKit
framework provided by Apple. Here's an example:
main.swift402 chars13 lines
In the example above, we define a function called encrypt
that takes in the plaintext data to be encrypted, as well as a symmetric key that will be used to encrypt the data. We use the AES.GCM.seal
function to perform the encryption, which returns a SealedBox
object that contains both the encrypted data and a tag that can be used to verify the integrity of the data. Finally, we return just the combined
property of the SealedBox
, which contains both the encrypted data and the tag.
Note that the size of the key used for encryption is specified as 256
bits in the example above, but other sizes are also supported.
gistlibby LogSnag