10

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];
Pripyat
  • 2,937
  • 2
  • 35
  • 69
  • Where do the `-AESEncryptWithPassphrase:` and `-AESDecryptWithPassphrase:` methods come from? – Rob Keniger Apr 06 '10 at 04:21
  • Hi Rob,I got the NSData+AES class (which includes these methods) from here:http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html – Pripyat Apr 06 '10 at 05:53
  • the issue seems to have fixed itself after changing the keybits value to 128. – Pripyat Apr 06 '10 at 10:44
  • David Schiefer: That's a category, not a class. See the Objective-C Programming Language document: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/ – Peter Hosey Apr 06 '10 at 12:24
  • 1
    You might want to look at the [RNCryptor library](https://github.com/rnapier/RNCryptor) that wraps CommonCrypto. – Basil Bourque Oct 17 '13 at 05:31

1 Answers1

20

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.

nicerobot
  • 9,145
  • 6
  • 42
  • 44
  • thanks. I've coded something like this a while back, also coded one that works with NSMutableDictionary - awesome for License Property Files ;) – Pripyat Nov 26 '10 at 08:25
  • Are there salts in the encryption your provided @nicerobot? –  Oct 11 '11 at 16:17
  • @TwoDumpling There is support for an initial vector as provided by CCCrypt but salt is easily just added to the text to be encrypted. – nicerobot Oct 11 '11 at 20:47
  • Would you just generate some random data, and append it to the key for encryption, then store the salt in the cipher text? I am a bit confused on how I would go about using a library that does have a salt. –  Oct 11 '11 at 21:17
  • @TwoDumpling Not the key. They key must be known to be able to decrypt the data. Add salt to the data being encrypted. As long as you have some way to distinguish the salt from the data, that's a fine approach. And salt doesn't _have_ to be random, just unique enough from call to call so a time is useful as salt too. Salt is just intended to ensure the same data/key pairs never generates the same cipher. – nicerobot Oct 12 '11 at 22:49
  • @nicerobot, When I try and decrypt I receive an error `Insufficent buffer provided for specified operation.`. How do I set up a buffer to read the data back? – Ned Schneebly Mar 03 '14 at 03:09
  • @NedSchneebly I haven't looked at this code in years. You're doing the same as the example above? If so, it's possible something has changed over the years. I'll see if i can make some time over the next week to try this code with current tools and see what happens. – nicerobot Mar 03 '14 at 19:35
  • 1
    @NedSchneebly I pushed updates. I went through all the project with Xcode 5, fixed them, and built them. They all built successfully. I then updated a [test-case](https://github.com/nicerobot/objc/blob/master/NSData/NSData%2BAES/t/t.m) to test it works (it does) and show how it works. – nicerobot Mar 09 '14 at 02:46