1

I work with corporate date which must be protected.

I have data for example:

let userName = String()
let password = String()
let profileImage = UIImage()

So this data i want to encrypt this data and send to server.

Important: to decrypt i want to use special key which will store at the app?

So it's possible to do this?

Andrew
  • 372
  • 2
  • 5
  • 25
  • 1
    HTTPS does the encryption for free, for you, just use that protocol when sending data to the server. – Cristik Oct 16 '18 at 06:04
  • To encrypt short pieces of text, Google how to use the keychain from Security Framework. To encrypt images: https://stackoverflow.com/questions/45206786/image-encryption-in-swift – Jasper Blues Oct 16 '18 at 06:05
  • @Cristik i want when i send data to server, at the column will storage something like this "kjfkjhh212ki121#122", i don't want to crypt HTTPS – Andrew Oct 16 '18 at 06:16
  • @Andrew, take a look at CommonCrypto, then – Cristik Oct 16 '18 at 06:17
  • In the case of a password you don't want to encrypt the data; Rather you should store a salted hash of the password. In the future you compute the same hash and compare the hashed values rather than having a situation where the password can be decrypted back to plain text – Paulw11 Oct 16 '18 at 06:34
  • She this URL https://medium.com/@shyamalranjana/swift-encryption-aes-256-ecb-sha512-41fd76779ded I think help you – Sabbir Ahmed Oct 16 '18 at 06:50

1 Answers1

0

For string the Encryption and Decryption can be done using RNCryptor.

import Foundation
import RNCryptor

extension String {

    func encrypt(encryptionKey: String) -> String {
        let messageData = self.data(using: .utf8)!
        let cipherData = RNCryptor.encrypt(data: messageData, withPassword: encryptionKey)
        return cipherData.base64EncodedString()
    }

    func decrypt(encryptionKey: String) -> String? {
        let encryptedData = Data.init(base64Encoded: self)!
        if let value = try? RNCryptor.decrypt(data: encryptedData, withPassword: encryptionKey) {
            let decryptedString = String(data: value, encoding: .utf8)!
            return decryptedString
        } else{
            return nil
        }
    }
}

Usage:-

let encyptionKey = "password"
let unencriptedMessage = "Hello World"
print (unencriptedMessage)
let encryptedMessage = unencriptedMessage.encrypt(encryptionKey: encyptionKey)
print (encryptedMessage)
if let decryptedMessage = encryptedMessage.decrypt(encryptionKey: encyptionKey) {
    print (decryptedMessage)
}

Output:-

enter image description here

Similar Answer

Hope this Helps. Happy Coding.

Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45