2010-04-05 15 views
9

我試圖在我的文本編輯器中加密/解密純文本文件。加密似乎工作正常,但解密不起作用,文本加密。我確定我已經使用加密的文字解密了文本 - 有人能通過下面的代碼片段查看並幫助我嗎?可可中的NSData-AES類加密/解密

謝謝:)

加密:

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

'-AESEncryptWithPassphrase:'和'-AESDecryptWithPassphrase:'方法來自哪裏? – 2010-04-06 04:21:55

+0

嗨,羅布,我從這裏得到了NSData + AES類(包括這些方法):http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html – Pripyat 2010-04-06 05:53:39

+0

問題似乎在將keybits值更改爲128後自行修復。 – Pripyat 2010-04-06 10:44:19

回答

20

爲什麼不使用內置的加密算法?這是我寫的NSData+AES,它使用CCCrypt和256位密鑰進行AES256加密。

你可以用它喜歡:

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

,並與解密:

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

免責聲明:有沒有保證我的NSData+AES是無bug :)這是相當新的。我歡迎代碼評論。

+0

謝謝。我已經編寫了類似於此的內容,也編碼了一個與NSMutableDictionary一起使用的代碼 - 對於許可證屬性文件非常棒;) – Pripyat 2010-11-26 08:25:50

+0

在提供的@nicerobot加密中是否存在鹽分? – 2011-10-11 16:17:17

+0

@TwoDumpling支持CCCrypt提供的初始向量,但鹽很容易添加到要加密的文本中。 – nicerobot 2011-10-11 20:47:44