Você está na página 1de 2

EvernoteExport

IOS Dev - Encrypting Images and Saving Themin App Sandbox.html[25.05.2014 17:39:30]
One of the requirements in my latest project was to encrypt an image and save it on the device in the applications sandbox, then decrypt is during
runtime and upload it to the server. I looked at the documentation for Apples CommonCrypto Framework, but it was taking me plenty of time to
implement it so instead i decided to use some wrappers that would get the job done for me. I found the RNCryptor library on Github that uses AES
encrypton. It was pretty simple to implement it. First, download the files from its github url and include all the files that are relevant to you present
inside the RNCryptor folder on your project. In my case, i just imported all of them for now.
Then use the following code to encrypt the image.
1
2
3
4
5
6
/ / Code f or encr ypt i ng and savei ng i mage UI I mage *i mageToEncr ypt = [ UI I mage i mageNamed: @" SomeI mage"] ; NSSt r i ng *i magePat h = [ NSHomeDi r ect or y( ) st r i
IOS Dev - Encrypting Images and Saving Them in App Sandbox
Source: http://highaltitudehacks.com/2013/09/26/ios-dev-encrypted-images-and-saving-them-in-app-sandbox/
IOS Dev - Encrypting Images and Saving Them
in App Sandbox
Sep 26th, 2013
Posed by Prateek Gianchandani
EvernoteExport
IOS Dev - Encrypting Images and Saving Themin App Sandbox.html[25.05.2014 17:39:30]
7
8
9
1
0
1
1
ngByAppendi ngPat hComponent : @" Document s/ encr ypt edI mage. png"] ; NSDat a *dat a = UI I magePNGRepr esent at i on( f et chedI mage) ; NSEr r or *er r or ; NSDat a *encr ypt
edDat a = [ RNEncr ypt or encr ypt Dat a: dat a wi t hSet t i ngs: kRNCr ypt or AES256Set t i ngs
passwor d: @" ABC123" er r or : &er r or ] ; [ encr ypt edDat a wr i t eToFi l e: i magePat h at omi cal l y: YES] ;
Note that encryting and decrypting the image requires a passcode (ABC123). To decrypt the image, use the following code ..
1
2
3
4
5
6
7
/ / Code f or l oadi ng i mage by decr ypt i on NSSt r i ng *i magePat h = [ NSHomeDi r ect or y( ) st r i ngByAppendi ngPat hComponent : @" Document s/ encr ypt edI mage. png"] ; N
SDat a *encr ypt edDat a = [ NSDat a dat aWi t hCont ent sOf Fi l e: i magePat h] ; NSDat a *decr ypt edDat a = [ RNDecr ypt or decr ypt Dat a: encr ypt edDat a
wi t hPasswor d: @" ABC123" er r or : &er r or ] ; UI I mage *i mage = [ UI I mage i mageWi t hDat a: decr ypt edDat a]
;
See, its pretty simple.

Você também pode gostar