NSData-AES Class Encryption/Decryption in Cocoa

Posted by David Schiefer on Stack Overflow See other posts from Stack Overflow or by David Schiefer
Published on 2010-04-05T16:11:33Z Indexed on 2010/04/05 16:13 UTC
Read the original article Hit count: 708

Filed under:
|
|
|

hi,

I am attempting to encrypt/decrypt a plain text file in my text editor. encrypting seems to work fine, but the decrypting does not work, the text comes up encrypted. I am certain i've decrypted the text using the word i encrypted it with - could someone look through the snippet below and help me out?

Thanks :)

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

Decrypting:

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

© Stack Overflow or respective owner

Related posts about encryption

Related posts about decryption