Skip to content

Instantly share code, notes, and snippets.

@astrokin
Created April 22, 2022 12:06
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/5662bf0b823f61837236e3d6fb6f3684 to your computer and use it in GitHub Desktop.
Save astrokin/5662bf0b823f61837236e3d6fb6f3684 to your computer and use it in GitHub Desktop.
Decrypt on iOS (CBC, Pkcs7, EVP_BytesToKey)
public class EVP_KDF_Util {
public class func generate_evp_kdf_aes256cbc_key_iv(pass: String, saltData: [UInt8]) throws -> (String, String) {
let passData = [UInt8](pass.data(using: .utf8)!)
let keySize: Int = 32
let keyPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: keySize)
keyPointer.initialize(repeating: 0, count: keySize)
let ivSize: Int = 16
let ivPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: ivSize)
ivPointer.initialize(repeating: 0, count: ivSize)
let err = gen_evp_kdf_aes256cbc(passData, saltData, keyPointer, ivPointer)
if err != ECE_OK {
throw PushCryptoError.decryptionError(errCode: err)
}
let key = Data(bytes: keyPointer, count: keySize).map({ String(format: "%02hhx", $0) }).joined()
let iv = Data(bytes: ivPointer, count: ivSize).map({ String(format: "%02hhx", $0) }).joined()
return (key, iv)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment