2010-04-05 41 views
9

Tôi đang cố gắng mã hóa/giải mã một tệp văn bản thuần túy trong trình soạn thảo văn bản của mình. mã hóa dường như hoạt động tốt, nhưng giải mã không hoạt động, văn bản được mã hóa. Tôi chắc chắn tôi đã giải mã văn bản bằng cách sử dụng từ tôi đã mã hóa nó - có thể ai đó xem qua đoạn trích dưới đây và giúp tôi không?Mã hóa/giải mã lớp NSData-AES trong Cocoa

Cảm ơn :)

Encrypting:

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

giải mã:

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

Phương thức '-AESEncryptWithPassphrase:' và '-AESDecryptWithPassphrase:' xuất phát từ đâu? –

+0

Xin chào Rob, tôi đã nhận được lớp NSData + AES (bao gồm các phương thức này) từ đây: http: //iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html – Pripyat

+0

vấn đề dường như đã cố định chính nó sau khi thay đổi giá trị keybits thành 128. – Pripyat

Trả lời

20

Tại sao không sử dụng được xây dựng trong các thuật toán mã hóa? Đây là NSData+AES tôi đã viết sử dụng CCCrypt với khóa 256it để mã hóa AES256.

Bạn có thể sử dụng nó như:

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

và giải mã nó với:

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

SỰ TỪ BỎ: Không có gì đảm bảo tôi NSData+AES là lỗi-miễn phí :) Đó là khá mới. Tôi hoan nghênh đánh giá mã.

+0

cảm ơn. Tôi đã mã hóa một cái gì đó như thế này một khi trở lại, cũng được mã hóa một trong đó làm việc với NSMutableDictionary - tuyệt vời cho các tập tin bất động sản Giấy phép;) – Pripyat

+0

Có muối trong mã hóa @nicerobot cung cấp của bạn? –

+0

@TwoDumpling Có hỗ trợ cho một vector ban đầu được cung cấp bởi CCCrypt nhưng muối dễ dàng chỉ được thêm vào văn bản được mã hóa. – nicerobot

Các vấn đề liên quan