Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSData-AES Class Encryption/Decryption in Cocoa

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];
like image 710
Pripyat Avatar asked Apr 05 '10 16:04

Pripyat


People also ask

How encryption and decryption is done in AES?

AES includes three block ciphers: AES-128 uses a 128-bit key length to encrypt and decrypt a block of messages. AES-192 uses a 192-bit key length to encrypt and decrypt a block of messages. AES-256 uses a 256-bit key length to encrypt and decrypt a block of messages.

Can we decrypt AES?

Only those who have the special key can decrypt it. AES uses symmetric key encryption, which involves the use of only one secret key to cipher and decipher information.

Can AES 128 be decrypted?

Yes, with AES-128-CBC, it is possible to decrypt just a single block of cyphertext. Each block is 128 bits (16 bytes).


1 Answers

Why not use the built-in encryption algorithms? Here's an NSData+AES i wrote which uses CCCrypt with a 256it key for AES256 encryption.

You can use it like:

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

and decrypt it with:

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

DISCLAIMER: There no guarantee my NSData+AES is bug-free :) It's fairly new. I welcome code reviews.

like image 69
nicerobot Avatar answered Nov 07 '22 14:11

nicerobot