cocoa - Cocoa 中的 NSData-AES 类加密/解密

标签 cocoa nsdata encryption

我正在尝试在文本编辑器中加密/解密纯文本文件。加密似乎工作正常,但解密不起作用,文本被加密。我确信我已经使用加密的单词解密了文本 - 有人可以查看下面的代码片段并帮助我吗?

谢谢:)

加密:

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];

最佳答案

为什么不使用内置的加密算法?这是 NSData+AES我写的使用 CCCrypt带有用于 AES256 加密的 256it key 。

您可以像这样使用它:

NSData *data = [[NSData dataWithContentsOfFile:@"/etc/passwd"] 
                             encryptWithString:@"mykey"];

并解密它:

NSData *file = [data decryptWithString:@"mykey"];

免责声明:不能保证我的 NSData+AES没有错误:) 这是相当新的。我欢迎代码审查。

关于cocoa - Cocoa 中的 NSData-AES 类加密/解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2579453/

相关文章:

swift - 如何获取 NSRunningApplication 的参数?

macos - 从子目录加载视网膜图像

nsdata - 问题让 NSData 请求在 Swift 2.0 中工作

用户数据的 PHP、MySQL 和 AES 加密/解密

c# - 使用 ProtectedData.Protect 时的 DataProtectionScope

ios - 归档应用程序时 Xcode 提示丢失文件

macos - cocoa 的评级控制(Mac OS)

ios - 取消归档带有 subview 的 UIImageView

ios - NSData 缓存 iOS

python - 十六进制转pem文件