HELP!
Im using xcode 8 and swift. I made this app which can upload data to firebase where some of the data is encrypted. Before I made the encryption, I made a search function which worked fine searching through all the data. Now that I have added the encryption to some of the data, I can´t search for the encrypted data. Therefore I need to decrypt it before search, however I don´t know how to do that.
If we take it step by step...
For the encryption Im using RNCryptor and Cryptoswift. In one of my viewControllers, I have made an extension string in which the 2 function are:
extension String{
func aesEncrypt(key: String, iv: String) throws -> String {
let data = self.data(using: .utf8)!
let encrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt([UInt8](data))
let encryptedData = Data(encrypted)
return encryptedData.base64EncodedString()
}
func aesDecrypt(key: String, iv: String) throws -> String {
let data = Data(base64Encoded: self)!
let decrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).decrypt([UInt8](data))
let decryptedData = Data(decrypted)
return String(bytes: decryptedData.bytes, encoding: .utf8) ?? "Could not decrypt"
}
}
First I encrypt and upload the data:
//Krypterer data
let input = Personnr.text
let input2 = Korekort.text
let key = key1
let iv = key
let en = try! input!.aesEncrypt(key: key!, iv: iv!)
ePersonnr = en
let en2 = try! input2!.aesEncrypt(key: key!, iv: iv!)
eKorekortnr = en2
//Samler alt data om bruger
let data = ["Navn": Navn.text!,
"Telefonnr": Telefonnr.text!,
"Personnr": ePersonnr!,
"Kørekortnr": eKorekortnr!,
"Bil": Bil.text!,
"Interesse": Interesse.text!]
//Oploader til database
self.ref.child("Buyers").child(ePersonnr!).setValue(data)
The key and iv for the encryption I get from the database, but that is not the problem. After the encryption the output is as shown below..
As you can see, some of the data is encrypted and some it is not. Just as planned.
Now I need to search in the app for the data. For this I made a search function which includes:
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if searchController.isActive && searchController.searchBar.text != "" {
return filteredUsers.count
}
return self.usersArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
// Configure the cell...
let user : NSDictionary?
if searchController.isActive && searchController.searchBar.text != "" {
user = filteredUsers[indexPath.row]
}
else
{
user = self.usersArray[indexPath.row]
}
cell.textLabel?.text = user?["Navn"] as? String
cell.detailTextLabel?.text = user?["Telefonnr"] as? String
return cell
}
func filterUsers (searchText:String) {
self.filteredUsers = self.usersArray.filter{ user in
var fNavn = false
var personNr = false
var searchBil = false
var telefonNr = false
var korekortNr = false
if let Navn = user!["Navn"] as? String {
fNavn = Navn.lowercased().contains(searchText.lowercased())
}
if let Bil = user!["Bil"] as? String {
searchBil = Bil.lowercased().contains(searchText.lowercased())
}
//Decryption of data
if let Personnr = user![self.Personnr!] as? String {
personNr = Personnr.lowercased().contains(searchText.lowercased())
}
if let Kørekortnr = user!["Kørekortnr"] as? String {
korekortNr = Kørekortnr.lowercased().contains(searchText.lowercased())
}
if let Telefonnr = user!["Telefonnr"] as? String {
telefonNr = Telefonnr.lowercased().contains(searchText.lowercased())
}
return fNavn || personNr || searchBil || korekortNr || telefonNr
}
tableView.reloadData()
}
The problem is that because the data is encrypted I can only search for the encrypted string which is now the value for whatever was encrypted. Therefore I need to do something, which allows me to decrypt the data before the search?
And for that, I need help.
If you need any other information or maybe don´t understand some of the code, please let me know.
I hope you can help.
HELP!
