AES是对称加密
Swift开发是使用CryptoSwift第三方库
配置方法:在Podfile文件中添加
pod ‘CryptoSwift’, ‘~> 1.6.0’
pod install即可
swift代码
/// AES加密
/// aes_key : key
/// iv : 偏移量
static func aes_encrypt(_ str: String, aes_key: String, iv: String) -> String{
var encryptedStr = ""
do {// AES encrypt
//SNOfSjDz3LXjoXbN
//key :Ocwgvv9RE2EqzQX1
let encrypted = try AES(key:aes_key , iv:iv , padding: .pkcs7).encrypt(str.bytes)
let data = Data(bytes: encrypted, count: encrypted.count)
//加密结果从data转成string 转换失败 返回""
encryptedStr = data.base64EncodedString(options: .lineLength64Characters)
} catch {
print(error.localizedDescription)
}
return encryptedStr
}