用objectC完成的一个有关压缩,加解密的粗糙之作

来源:互联网 发布:淘宝战网点 编辑:程序博客网 时间:2024/06/05 04:24


平台、工具

采用Xcode平台,objectiveC语言完成。

 

设计

数据流图:

 

 

 

 

 

 

 

以上的数据流,从左往右依次为:

1:加压,加密完成后的文件流

2:原文件流(这里12的逻辑上应该反过来的,一时间没注意画反了。)

3:加压,加密完成后的文件流

4:解压,解密后的文件刘

5:日记文字加密后的数据流

6:加密后的数据流

 

步骤

步骤1:引导界面,由于该界面没有任何数据上的处理,所以只有一个视图,而storyborad生成的单纯视图是没有代码的,所以这里就不贴了。

步骤2:第二步做的是关于文件的加密,压缩部分

首先是将目录下的文件列出:

 

//设置tableview各种属性    DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, 320, 420)];    [DataTable setDelegate:self];    [DataTable setDataSource:self];//这里指定了dataSource为myListArray[self.view addSubview:DataTable];    // Documents文件夹的路径写法    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;    //文件管理控件NSFileManager * fm = [NSFileManager defaultManager];    //将目录下的文件名导入一个数组中    myListArray = [fm directoryContentsAtPath:documentPath];


接下来就是对选中的文件进行加密,加压

NSString *textValue = [NSString stringWithFormat:@"%@",_textField.text];//获取textfile里的内容      NSFileManager *defaultManager;//创建该实例用于删除文件    defaultManager = [NSFileManager defaultManager];        ZipArchive *zip = [[ZipArchive alloc] init];    FZEasyFile *easyFile = [FZEasyFile sharedInstance];    NSString *heroSelected=[myListArray objectAtIndex:indexPath.row];//indexPath.row得到选中的行号,提取出在数组中的内容。    // 原文件路径    NSString* filePath = [easyFile fullFileName:heroSelected];    NSString* filePath2 = [filePath stringByAppendingString:@".zipp"];      // 开始压缩    [zip CreateZipFile2:filePath2];    // 添加文件    [zip addFileToZip:filePath newname:heroSelected];    //... 此处可以add多个文件    // 结束压缩    if( ![zip CloseZipFile2])    {        filePath2 = @""; // 如果压缩失败,则压缩文件路径设置为空。    }    //将文件读入    NSString *astring = [[NSString alloc] initWithContentsOfFile:filePath2];    NSLog(@"astring:%@",astring);    //AES加密    NSString *message = astring;    NSString *password = textValue;    NSString *encryptedData = [AESCrypt encrypt:message password:password];    [encryptedData writeToFile:filePath2 atomically:YES ];        //删除原文件    [defaultManager removeItemAtPath:filePath error:nil];

 

 

 

 

 

 

 

步骤3:对加压加密过的文件进行解密,解压

首先,是将文件列表导入,这部分和步骤2基本一致(后面的也基本一致,为了报告页数,将不再赘叙)

其次是对文件的解压,解密代码

步骤4:做关于日记(备忘录)的加密部分

这部分有了上面的步骤很好理解,就是在文本框内写下日记,加密后写入文件当中,文件以时间命名。

 

NSString *textValue = [NSString stringWithFormat:@"%@",_textField.text];//获取textfile里的内容        NSFileManager *defaultManager;//创建该实例用于删除文件    defaultManager = [NSFileManager defaultManager];    ZipArchive *zip = [[ZipArchive alloc] init];    FZEasyFile *easyFile = [FZEasyFile sharedInstance];        NSString *heroSelected=[myListArray objectAtIndex:indexPath.row];//indexPath.row得到选中的行号,提取出在数组中的内容。    // 原文件路径    NSString* filePath = [easyFile fullFileName:heroSelected];    NSString* filePath2 = [filePath stringByAppendingString:@".ok"];        //将文件读入    NSString *astring = [[NSString alloc] initWithContentsOfFile:filePath];    //解密     NSString *password = textValue;    NSString* message;    message = [AESCrypt decrypt:astring password:password];    [message writeToFile:filePath atomically: YES ];        //解压    BOOL result;    if ([zip UnzipOpenFile:filePath]) {        result = [zip UnzipFileTo:filePath2 overWrite:YES];        if (!result) {            NSLog(@"解压失败");        }        else        {            NSLog(@"解压成功");        }        [zip UnzipCloseFile];    }    //删除原文件    [defaultManager removeItemAtPath:filePath error:nil];

 

步骤5:基本上是步骤4的逆过程,只是加上了步骤2中的文件列表,由于篇幅的原因,这里不再赘述。

结果讨论


                     

 

                      

0 0
原创粉丝点击