Created
April 22, 2022 12:08
-
-
Save astrokin/c4192d60d278f0b31bd1bd20eeee4869 to your computer and use it in GitHub Desktop.
Decrypt on iOS (CBC, Pkcs7, EVP_BytesToKey)
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
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