通过 CryptoKit 的介绍,这可以使用以下代码来完成:
import CryptoKit
//generate Symmetric key
let key = SymmetricKey(size: .bits256)
以下是使用 AES-GCM 方法使用 32 字节对称密钥进行加密和解密的示例:
//generate Symmetric key
let key = SymmetricKey(size: .bits256)
//encrypt plaintext
let encryptedData = try! AES.GCM.seal("plain-text".data(using: .utf8)!, using: key)
print("Encrypted data using symmetric key:\(encryptedData)")
//decrypt data
let sealedBoxRestored = try! AES.GCM.SealedBox(nonce: encryptedData.nonce, ciphertext: encryptedData.ciphertext, tag: encryptedData.tag)
let decryptedData = try! AES.GCM.open(sealedBoxRestored, using: key)
print("Decrypted:\n\(String(data: decryptedData, encoding: .utf8)!)")