Skip to content

Instantly share code, notes, and snippets.

@DineshKachhot
Last active May 18, 2020 12:12
Show Gist options
  • Save DineshKachhot/b24853e1e2b074815b8a80076d553ebd to your computer and use it in GitHub Desktop.
Save DineshKachhot/b24853e1e2b074815b8a80076d553ebd to your computer and use it in GitHub Desktop.
AES Encryption
fileprivate func encryptionAES(data: Data, key: Data, IV: Data) -> String? {
var numberOfBytesEncrypted : size_t = 0
let size = data.count + kCCKeySizeAES128
var encrypted = Data(count: size)
let cryptStatus = IV.withUnsafeBytes { ivBytes in
encrypted.withUnsafeMutableBytes { encryptedBytes in
data.withUnsafeBytes { dataBytes in
key.withUnsafeBytes { keyBytes in
CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionPKCS7Padding), keyBytes, key.count, ivBytes, dataBytes, data.count, encryptedBytes, size, &numberOfBytesEncrypted)
}
}
}
}
if cryptStatus == Int32(kCCSuccess) {
encrypted.count = numberOfBytesEncrypted
return encrypted.base64EncodedString()
} else {
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment