How to encrypt strings, images, and videos in IOS
1,Decodes a string using gtmbase64 Encoding
Gtmdefines. hgtmbase64.hgtmbase64. m
You can find these three files here (gtmdefines. H is on the second page, click Next in the upper right corner to jump to the second page)Http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation? R = 87
2. codec functions (you can change the codec string, image, and video to the corresponding filepath ):
Read the file from the drivers path of the simulator and the real machine, encode it, and write it into the file. Read it and decode it.
// Encryption function
-(Void) func_encodefile
{
// Nsstring * Path = [nshomedirectory () stringbyappendingformat: @ "/documents/test.png"];
Nsstring * filepath = [nshomedirectory () stringbyappendingformat: @ "/documents/iphone4.mov"];
// Convert the file path to nsdata
Nsdata * imagedataorigin = [nsdata datawithcontentsoffile: filepath];
// Exception or handling of the first 1000 bits
Unsigned char * cbyte = (unsigned char *) [imagedataorigin bytes];
For (INT Index = 0; (index <[imagedataorigin length]) & (index <1000); index ++,
Cbyte ++)
{
* Cbyte = (* cbyte) ^ arrayforencode [Index];
}
// Base64 encoding nsdata
Nsdata * imagedataencode = [gtmbase64 encodedata: imagedataorigin];
[Imagedataencode writetofile: filepath atomically: Yes];
}
// Decryption Function
-(Void) func_decodefile
{
// Nsstring * filepath = [nshomedirectory () stringbyappendingformat: @ "/documents/test.png"];
Nsstring * filepath = [nshomedirectory () stringbyappendingformat: @ "/documents/iphone4.mov"];
// Read the data corresponding to the encrypted file
Nsdata * dataencoded = [nsdata datawithcontentsoffile: filepath];
// Base64 decoding nsdata
Nsdata * datadecode = [gtmbase64 decodedata: dataencoded];
// Exception or handling of the first 1000 bits
Unsigned char * cbyte = (unsigned char *) [datadecode bytes];
For (INT Index = 0; (index <[datadecode length]) & (index <10); index ++, cbyte ++)
{
* Cbyte = (* cbyte) ^ arrayforencode [Index];
}
[Datadecode writetofile: filepath atomically: Yes];
}