該文章出自:guohuaden.com
這里加密使用的是三方庫(kù)RNCryptor,它是一個(gè)跨語(yǔ)言AES加密/解密庫(kù)。
主要目標(biāo)是Swift和Objective-C,但C,C ++,C#,Erlang,Go,Haskell,Java,PHP,Python,Javascript和Ruby中都有實(shí)現(xiàn)。
RNCryptor地址:
RNCryptor :https://github.com/RNCryptor/RNCryptor
以及OC專(zhuān)用的地址:
RNCryptor-objc :https://github.com/RNCryptor/RNCryptor-objc
下面就用到的圖片和PDF文件加密做一下簡(jiǎn)單的介紹。
1、圖片加解密
這個(gè)沒(méi)有什么思想可言,直接看下代碼
#pragma mark --Image加解密
-(void)imageEncryptionAndDecryption
{
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"default.jpg" ofType:nil]];
NSError *error;
//加密
NSData *encryptedData = [RNEncryptor encryptData:data withSettings:kRNCryptorAES256Settings password:aPassword error:&error ];
if (!error) {
NSLog(@"^_^ 加密成功 ……——(^_^)\n");
// NSLog(@"encryptedData==%@",encryptedData);
}
//解密
NSData *decryptedData = [RNDecryptor decryptData:encryptedData withPassword:aPassword error:&error];
if (!error) {
NSLog(@"^_^ 解密成功 ……——(^_^)\n");
// NSLog(@"decryptedData==%@",decryptedData);
self.imageView.image = [UIImage imageWithData:decryptedData];
}
}
2、PDF加解密
考慮到PDF文件可能較大的原因,這里在加解密時(shí)使用了子線程,以避免加解密過(guò)程耗時(shí)。
另:PDF查看需要提供路徑,而這也是關(guān)鍵。
思路:
- 將網(wǎng)絡(luò)請(qǐng)求下來(lái)的數(shù)據(jù)流(NSData)直接進(jìn)行加密,加密成功后存入沙盒目錄中。
- 在查看PDF時(shí),先對(duì)加密的PDF進(jìn)行解密,再將解密的PDF存入沙盒目錄中(區(qū)分加解密PDF文件)。
- 獲取解密的PDF文件路徑,查看PDF文件。
- 退出查看當(dāng)前的PDF文件時(shí),刪除解密后的PDF文件緩存,保留加密的PDF緩存。
查看代碼:
加密
#pragma mark --PDF加密
-(void)PDFEncryption
{
__block NSData *encryptedData;
__block NSError *error;
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"11.pdf" ofType:nil];
NSString *fileEncryPath = [NSHomeDirectory()stringByAppendingPathComponent:@"/Documents/TKAMC.qgh"];
NSFileManager *fileManager = [NSFileManager defaultManager];
//判斷是否已存在加密文件,若存在直接執(zhí)行解密過(guò)程。
if ([fileManager fileExistsAtPath:fileEncryPath]) {
[self PDFDecryptedData:[NSData dataWithContentsOfFile:fileEncryPath]];
return;
}
//異步去加密,防止占用太多內(nèi)存
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSData *data = [NSData dataWithContentsOfFile:filePath];
//加密
encryptedData = [RNEncryptor encryptData:data withSettings:kRNCryptorAES256Settings password:aPassword error:&error ];
if (!error) {
NSLog(@"^_^ PDF加密成功 ……——(^_^)\n");
// NSLog(@"encryptedData==%@",encryptedData);
}
//在主線程上寫(xiě)入文件
dispatch_sync(dispatch_get_main_queue(), ^{
BOOL yes = [encryptedData writeToFile:fileEncryPath atomically:NO];
if (yes) {
NSLog(@"加密文件寫(xiě)入成功");
}else{
NSLog(@"加密文件寫(xiě)入失敗");
}
NSLog(@"寫(xiě)入PDF路徑:%@",fileEncryPath);
[self PDFDecryptedData:encryptedData];
});
});
}
解密
#pragma mark ---PDF解密
-(void)PDFDecryptedData:(NSData *)encryptedData{
NSString *fileDecryPath = [NSHomeDirectory()stringByAppendingPathComponent:@"/Documents/TKAMC"];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 解密
NSError *error;
if (encryptedData != nil || aPassword != nil) {
NSData *decryptedData = [RNDecryptor decryptData:encryptedData
withPassword:aPassword
error:&error];
dispatch_sync(dispatch_get_main_queue(), ^{
BOOL yes = [decryptedData writeToFile:fileDecryPath atomically:NO];
if (yes) {
NSLog(@"解密文件寫(xiě)入成功");
NSLog(@"寫(xiě)入解密PDF路徑:%@",fileDecryPath);
self.filepath = fileDecryPath;
[self pushVC];
}else{
NSLog(@"解密文件寫(xiě)入失敗");
}
});
}else{
NSLog(@"加密數(shù)據(jù)為空");
}
});
}
注: 這里加解密時(shí)并沒(méi)有具體實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求模塊,只是簡(jiǎn)單的對(duì)本地文件進(jìn)行了實(shí)踐,但大體實(shí)現(xiàn)過(guò)程已經(jīng)實(shí)現(xiàn)。
退出PDF時(shí),刪除解密的PDF文件
#pragma mark - ReaderViewControllerDelegate methods
- (void)dismissReaderViewController:(ReaderViewController *)viewController
{
//MARK:退出查看PDF時(shí)刪除解密存儲(chǔ)文件。
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:self.filepath error:nil];
[self dismissViewControllerAnimated:YES completion:nil];
}
這里提供一個(gè)個(gè)人測(cè)試使用的一個(gè)Demo,僅供參考
FileEncryption_Demo :https://github.com/Wheat-Qin/FileEncryption_Demo