Created
April 22, 2022 12:11
-
-
Save astrokin/df61a8c1aa34e60625b8ee4928ef3103 to your computer and use it in GitHub Desktop.
PKCS5.PBKDF
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
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