Skip to content

Instantly share code, notes, and snippets.

@astrokin
Created April 22, 2022 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astrokin/c4192d60d278f0b31bd1bd20eeee4869 to your computer and use it in GitHub Desktop.
Save astrokin/c4192d60d278f0b31bd1bd20eeee4869 to your computer and use it in GitHub Desktop.
Decrypt on iOS (CBC, Pkcs7, EVP_BytesToKey)
static func decrypt(_ base64String: String, passwordUtf8: String) throws -> String {
let encrypted = Data(base64Encoded: base64String)!
let salt = [UInt8](encrypted[8 ..< 16])
let evp = try EVP_KDF_Util.generate_evp_kdf_aes256cbc_key_iv(pass: passwordUtf8, saltData: salt) // key + iv
let aes = try AES(key: Array<UInt8>.init(hex: evp.0),
blockMode: CBC(iv: Array<UInt8>.init(hex: evp.1)),
padding: .pkcs7)
let data = [UInt8](encrypted[16 ..< encrypted.count])
let decrypted = try aes.decrypt(data)
guard let decryptedStr = String(bytes: decrypted, encoding: .utf8) else {
throw AES.Error.invalidData
}
return decryptedStr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment