Cocoa中的NSData AES类加密/解密
我正在尝试在我的文本编辑器中加密/解密纯文本文件。加密似乎工作正常,但解密不起作用,文本被加密了。我确信我已经用我加密过的单词解密了文本-有人能看看下面的代码片段并帮我解决吗 谢谢:) 加密:Cocoa中的NSData AES类加密/解密,cocoa,nsdata,encryption,Cocoa,Nsdata,Encryption,我正在尝试在我的文本编辑器中加密/解密纯文本文件。加密似乎工作正常,但解密不起作用,文本被加密了。我确信我已经用我加密过的单词解密了文本-有人能看看下面的代码片段并帮我解决吗 谢谢:) 加密: NSAlert *alert = [NSAlert alertWithMessageText:@"Encryption" defaultButton:@"Set"
NSAlert *alert = [NSAlert alertWithMessageText:@"Encryption"
defaultButton:@"Set"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"Please enter a password to encrypt your file with:"];
[alert setIcon:[NSImage imageNamed:@"License.png"]];
NSSecureTextField *input = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
[alert setAccessoryView:input];
NSInteger button = [alert runModal];
if (button == NSAlertDefaultReturn) {
[[NSUserDefaults standardUserDefaults] setObject:[input stringValue] forKey:@"password"];
NSData *data;
[self setString:[textView textStorage]];
NSMutableDictionary *dict = [NSDictionary dictionaryWithObject:NSPlainTextDocumentType
forKey:NSDocumentTypeDocumentAttribute];
[textView breakUndoCoalescing];
data = [[self string] dataFromRange:NSMakeRange(0, [[self string] length])
documentAttributes:dict error:outError];
NSData*encrypt = [data AESEncryptWithPassphrase:[input stringValue]];
[encrypt writeToFile:[absoluteURL path] atomically:YES];
解密:
NSAlert *alert = [NSAlert alertWithMessageText:@"Decryption"
defaultButton:@"Open"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"This file has been protected with a password.To view its contents,enter the password below:"];
[alert setIcon:[NSImage imageNamed:@"License.png"]];
NSSecureTextField *input = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
[alert setAccessoryView:input];
NSInteger button = [alert runModal];
if (button == NSAlertDefaultReturn) {
NSLog(@"Entered Password - attempting to decrypt.");
NSMutableDictionary *dict = [NSDictionary dictionaryWithObject:NSPlainTextDocumentType
forKey:NSDocumentTypeDocumentOption];
NSData*decrypted = [[NSData dataWithContentsOfFile:[self fileName]] AESDecryptWithPassphrase:[input stringValue]];
mString = [[NSAttributedString alloc]
initWithData:decrypted options:dict documentAttributes:NULL
error:outError];
为什么不使用内置的加密算法呢?这是我写的一篇文章,它使用256it密钥进行AES256加密 您可以像这样使用它:
NSData *data = [[NSData dataWithContentsOfFile:@"/etc/passwd"]
encryptWithString:@"mykey"];
并使用以下命令对其进行解密:
NSData *file = [data decryptWithString:@"mykey"];
免责声明:不能保证我的电脑没有bug:)这是相当新的。我欢迎代码审查。将密钥位值更改为128后,
-aesnyptwithpassphrase:
和-aesnyptwithpassphrase:
方法从何而来?嗨,Rob,我从这里获得了NSData+AES类(包括这些方法):问题似乎已经解决了。David Schiefer:这是一个类别,不是一个类。请参阅Objective-C编程语言文档:您可能想看看包装CommonCrypto的。不久前,我编写了类似的代码,还编写了一个可以与NSMutableDictionary一起使用的代码—对于许可证属性文件来说,这太棒了;)您提供的@nicerobot?@TwoDumpling加密中有盐吗?CCCrypt提供了对初始向量的支持,但盐很容易添加到要加密的文本中。您是否可以生成一些随机数据,并将其附加到密钥中进行加密,然后将盐存储在密文中?“我对如何使用一个有盐的库感到有点困惑。”NedSchneebly我推送了更新。我用Xcode 5完成了所有的项目,修复了它们,并构建了它们。他们都建造得很成功。然后,我更新了一个测试,以测试它的工作(确实如此),并展示它是如何工作的。