objective c - How to convert NSData to NSString and revert the NSString to NSData? -
my task encrypt file(.png, .txt, any..)
in order achieve doing
encryption:
- read file , store nsdata.
- convert nsdata nsstring.
- encrypt nsstring of aescrypt
- store nsstring in file
decryption
- read encrypted string
- decrypt of aescrypt
- convert nsdata
- save location
below code doing in order convert file nsstring:
nsstring* sourcefile = @"/users/vikas/desktop/thehulk.png"; nsdata *data = [[nsfilemanager defaultmanager] contentsatpat h:sourcefile]; nsstring *dataasstring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];
problem:
the above code able read , store file nsdata when converting nsdata nsstring, value getting nil
research
s.p: if have better suggestion file encryption please let me know newbie.
according this blog string data conversion forces trailing \0
byte, might have remove. can done follows:
data = [data subdatawithrange:nsmakerange(0, [data length] - 1)];
try see if works you.
Comments
Post a Comment