Last active
May 18, 2020 12:12
-
-
Save DineshKachhot/b24853e1e2b074815b8a80076d553ebd to your computer and use it in GitHub Desktop.
AES Encryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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