0

I'm trying to encrypt/decrypt image using CryptoSwift.

I converted the image to JPEG format then to Data then to String to be the input of CryptoSwift. I got the encryptedString and decrypted it into decryptedString.

Is there a way to convert the decryptedString into JPEG format so I can view it in UIImageView?

Here is my code:

guard let image = UIImage(named: "img") else { return } 
let data = UIImageJPEGRepresentation(image, 1.0)  
let character = CharacterSet () 
let str = NSData.init(data: data!) 
let text = str.description.trimmingCharacters(in:character).replacingOccurrences(of: " ", with: "")          
let plainText = text  
let key = "simplekey"  
let iv = "1234123412341234"    
let cryptoLib = CryptLib();  
let encryptedString = cryptoLib.encryptPlainText(with: plainText, key: key, iv: iv)  
let decryptedString = cryptoLib.decryptCipherText(with: encryptedString , key: key , iv: iv)` 
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • You should convert you jpeg data to base64encoded String instead of using the Data description – Leo Dabus Sep 04 '17 at 17:11
  • 1
    Never use the `description` method for anything except for debugging. Its output is not documented and it could change in a future OS update. – rmaddy Sep 04 '17 at 17:32
  • See https://stackoverflow.com/questions/43849382/how-to-convert-data-received-from-uiimagepngrepresentation-to-string-and-vice for the proper way to convert the image data to and from a String. – rmaddy Sep 04 '17 at 17:37
  • 1
    @LeoDabus Thanks a lot .. it works – Issam Abo Alshamlat Sep 04 '17 at 17:42
  • 1
    Possible duplicate of [How to convert Data received from UIImagePNGRepresentation() to String and vice versa?](https://stackoverflow.com/questions/43849382/how-to-convert-data-received-from-uiimagepngrepresentation-to-string-and-vice) – George Alexandria Sep 04 '17 at 17:50
  • It is best to avoid using CryptoSwift, amoung other things it is 500 to 1000 times slower than Common Crypto based implementations. Apple's Common Crypto is FIPS certified and as such has been well vetted, using CryptoSwift is taking a chance on correctness and security. – zaph Sep 04 '17 at 20:43

0 Answers0