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];
Phương thức '-AESEncryptWithPassphrase:' và '-AESDecryptWithPassphrase:' xuất phát từ đâu? –
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
vấn đề dường như đã cố định chính nó sau khi thay đổi giá trị keybits thành 128. – Pripyat