1、使用GTMBase64编码解码字符串
GTMDefines.h GTMBase64.h GTMBase64.m
你可以在这里找到这三个文件(GTMDefines.h在第二页,点击右上角的next按钮即可跳转到第二页)http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/?r=87
2、编解码函数(可以编解码字符串、图片、视频:filePath换成相应的即可):
从模拟器和真机的Documents路径下读取文件,编码后写入文件;读出来解码
// 加密函数
-(void)func_encodeFile
{
//NSString *path = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/test.png"];
NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/iphone4.mov"];
//文件路径转换为NSData
NSData *imageDataOrigin = [NSData dataWithContentsOfFile:filePath];
// 对前1000位进行异或处理
unsigned char * cByte = (unsigned char*)[imageDataOrigin bytes];
for (int index = 0; (index < [imageDataOrigin length]) && (index < 1000); index++, cByte++)
{
*cByte = (*cByte) ^ arrayForEncode[index];
}
//对NSData进行base64编码
NSData *imageDataEncode = [GTMBase64 encodeData:imageDataOrigin];
[imageDataEncode writeToFile:filePath atomically:YES];
}
// 解密函数
-(void)func_decodeFile
{
//NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/test.png"];
NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/iphone4.mov"];
// 读取被加密文件对应的数据
NSData *dataEncoded = [NSData dataWithContentsOfFile:filePath];
// 对NSData进行base64解码
NSData *dataDecode = [GTMBase64 decodeData:dataEncoded];
// 对前1000位进行异或处理
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];
}