iOS里加密字符串、图片、视频方法

来源:互联网 发布:假学生证淘宝怎么搜 编辑:程序博客网 时间:2024/06/06 07:43

iOS里加密字符串、图片、视频方法

1、使用GTMBase64编码解码字符串

GTMDefines.hGTMBase64.hGTMBase64.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 < 1000); index++, cByte++)

    {

        *cByte = (*cByte) ^ arrayForEncode[index];

    }

    

    [dataDecode writeToFile:filePath atomically:YES];

}



下面是根据上面2中的代码实现成功而来:


#define Key_Count (10)//加密字符串长度,上文2中设置1000,此处便于理解测试设置为10
static char arrayForEncode[Key_Count] = "2014.Year+";


-(BOOL)func_encodeFile:(NSString *)filePath withNewName:(NSString*)newFilePath
{
    if (nil == filePath || NO == [[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        return NO;
    }
    //文件路径转换为NSData
    NSData *imageDataOrigin = [NSData dataWithContentsOfFile:filePath];
    

    // 对前1000位进行异或处理
    unsigned char * cByte = (unsigned char*)[imageDataOrigin bytes];
    for (int index = 0; (index < [imageDataOrigin length]) && (index < Key_Count); index++, cByte++)
    {
        *cByte = (*cByte) ^ arrayForEncode[index];
    }
    
    //对NSData进行base64编码
    NSData *imageDataEncode = [GTMBase64 encodeData:imageDataOrigin];
    return [imageDataEncode writeToFile:newFilePath atomically:YES];
    
}
-(BOOL)func_decodeFile:(NSString *)filePath withNewName:(NSString*)newFilePath
{
    if (nil == filePath || NO == [[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        return NO;
    }
    // 读取被加密文件对应的数据
    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 < Key_Count); index++, cByte++)
    {
        *cByte = (*cByte) ^ arrayForEncode[index];
    }
    return [dataDecode writeToFile:newFilePath atomically:YES];
}




测试代码段:


    NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/test/1.jpg"];
    NSString *filePath1 = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/test/11.jpg"];
    NSString *filePath2 = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/test/12.jpg"];
    [self func_encodeFile:filePath withNewName:filePath1];
    
    [self func_decodeFile:filePath1 withNewName:filePath2];

原创粉丝点击