Skip to content

Instantly share code, notes, and snippets.

@astrokin
Created April 22, 2022 12:11
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/df61a8c1aa34e60625b8ee4928ef3103 to your computer and use it in GitHub Desktop.
Save astrokin/df61a8c1aa34e60625b8ee4928ef3103 to your computer and use it in GitHub Desktop.
PKCS5.PBKDF
import CryptoSwift
static func decrypt(_ base64String: String, passwordUtf8: String) throws -> String {
let salt = Array<UInt8>.init(hex: String(base64String.prefix(32)))
let key = try PKCS5.PBKDF2(password: passwordUtf8.bytes, salt: salt, iterations: 100, keyLength: 16, variant: .md5).calculate()
let iv = Array<UInt8>.init(hex: String(base64String.prefix(64).suffix(32)))
let aes = try AES(key: key, blockMode: CBC(iv: iv), padding: .pkcs7)
let stringToDecrypt = String(base64String.suffix(base64String.count - 64))
let encrypted: [UInt8] = Array(Data(base64Encoded: stringToDecrypt)!)
let decrypted = try aes.decrypt(encrypted)
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