如何在保存和读取 NSDocumentDirectory 时加密/解密 (.pdf,.txt) 等文件 [英] How to encrypt/decrypt the files such as (.pdf,.txt) while saving and reading from the NSDocumentDirectory

查看:35
本文介绍了如何在保存和读取 NSDocumentDirectory 时加密/解密 (.pdf,.txt) 等文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 iOS 设备上保存 PDF 文件.我不希望人们能够进入并阅读内容,因此我需要能够在写入时加密文件并在再次加载时解密.

I'm currently saving a PDF file on the iOS device. I don't want people to be able to get in and read the contents so I need to be able to encrypt the file on writing and decrypt when loading it back again.

我目前正在像这样保存文件.我得到了一个 Base64 字符串,其中包含来自服务的 pdf 内容和密码,我正在做的是我正在转换为 NSData 并保存到 NSDocumentDirectory 并使用它在 UIDocumentInteractionController 中打开的文件路径 在打开 pdf 时,我在 UIDocumentInteractionController 中收到密码提示并按预期工作:

I'm currently saving the file like this.I am getting a Base64 String which has pdf contents plus password from the service, what I am doing is I am converting to NSData and saving to NSDocumentDirectory and I use that file path to open in UIDocumentInteractionController While opening pdf I get a password prompt in UIDocumentInteractionController and is working as expected:

 NSData *pdf = [[NSData alloc]initWithBase64EncodedString:baseText options:0];
 NSString *documnet = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
 NSString *filebaseURL = [[NSString alloc]initWithString:[documnet stringByAppendingPathComponent:@"samplemade.pdf"]];
 [pdf writeToFile: filebaseURL atomically:YES];

为了增加安全性我想要的是我想在存储在 NSDocumentDirectory 时加密 PDF,然后在我想在 UIDocumentInteraction 控制器中显示它时解密它.如何做到这一点?

我从这个链接文件保护得到了使用 FileProtection 选项的建议,但这仅在设备锁定时提供保护.相反,我想以加密格式存储此 PDF,在我的应用程序中显示此 PDF 时,我应该对其进行解密.我正在使用 UIDocumentInteractionController.知道如何做到这一点吗?我想要的是我想在存储在 NSDocumentDirectory 时加密 PDF,然后在我想在 UIDocumentInteraction 控制器中显示它时解密它

I got a suggestion to use FileProtection options from this link File Protection but this only protects when the device is locked. Instead I want to store this PDF in an encrypted format and while showing this PDF in my app I should decrypt it . I am using UIDocumentInteractionController. Any idea how to do this? What I want is I want to encrypt PDF while storing in NSDocumentDirectory then decrypt it when I want to show it in UIDocumentInteraction controller

推荐答案

在我的实践中,writeToFile 方法不能用于生成 pdf.如果需要制作pdf,需要写在Core Foundation中,制作时可以设置密码,使用UIDocumentInteractionController读取simple.pdf方法interactionControllerWithURL:不需要密码,如果需要使用密码,需要使用PDFDocument打开或者其他方法

In my practice, the writeToFile method cannot be used to generate a pdf. If you need to make a pdf, you need to write it in Core Foundation ,you can set the password when making it, and use the UIDocumentInteractionController to read simple.pdf method interactionControllerWithURL: no password is required, if need to use a password, need to use PDFDocument to open or other methods

这篇关于如何在保存和读取 NSDocumentDirectory 时加密/解密 (.pdf,.txt) 等文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆