[SOLVED] How to Implement File Encryption with SSCrypto?

  • Thanks to Tom Hageman. I've figured out the problem and thanks for the tip
    about not coding in Mail. :)
    After getting my encrypted data from the file, I'm now doing the following
    and it works well. I need to clean it up a bit but my main problem was that
    I needed to use a symmetric cipher (instead of what I was using before).

    // Get password from user, make SHA1 from it and set key

    int returnCode = [NSApp runModalForWindow: passwordWindow];

    NSData *data = [self encryptedData];

    NSData *sha1 = [SSCrypto getSHA1ForData:[docPassword dataUsingEncoding:
    NSUTF8StringEncoding ]];

    SSCrypto *crypto = [[SSCrypto alloc] initWithSymmetricKey:sha1];

    // Setup crypto, decrypt, set data

    [crypto setCipherText:encryptedData];

    NSData *decryptedData = [crypto decrypt:@"aes256"];

    NSMutableArray *newArray;

    newArray = [NSKeyedUnarchiver unarchiveObjectWithData:decryptedData];

    [self setEmployees:newArray];

    By the way, I've moved this code out of the dataOfType function into the
    function that fires immediately after when the nib is loaded (I forgot the
    name of it, it may be loadFromNib or something similar). Also, this give me
    an opportunity to use a sheet/dialog to collect the password.

    On Dec 7, 2007 2:43 AM, <cocoa-dev-request...> wrote:

    >
    > Message: 4
    > Date: Thu, 6 Dec 2007 22:04:05 -0800
    > From: "Nat Edar" <natedar...>
    > Subject: How to Implement File Encryption with SSCrypto?
    > To: <cocoa-dev...>
    > Message-ID:
    > <11bba2620712062204k771309f8x432069d5e758568f...>
    > Content-Type: text/plain; charset=ISO-8859-1
    >
    > I'm using the SSCrypto framework (http://septicus.com/products/opensource/
    > )
    > which is pretty straightforward and I can encrypt/decrypt NSStrings fine.
    > Apparently it is also capable of encrypting NSData.
    > My idea for encrypting a file before it was written, was to encrypt the
    > output of " NSKeyedArchiver" in my implementation of dataOfType. Here's my
    > code:
    >
    > - (NSData *)dataOfType:(NSString *)typeName error:( NSError **)outError
    >
    > {
    >
    > NSString *publicKeyPath  = [[NSBundle mainBundle] pathForResource
    > :@"PUBKEY"
    > ofType:@"pem" inDirectory:@"../.."];
    >
    > NSString *privateKeyPath = [[NSBundle mainBundle] pathForResource :
    > @"Privatekey" ofType:@"pem" inDirectory:@"../.."];
    >
    > NSData *publicKeyData  = [NSData dataWithContentsOfFile:publicKeyPath];
    >
    > NSData *privateKeyData = [NSData dataWithContentsOfFile:privateKeyPath];
    >
    >
    > SSCrypto *crypto = [[SSCrypto alloc] initWithPublicKey:publicKeyData
    > privateKey:privateKeyData];
    >
    >
    > [personController commitEditing];
    >
    >
    > NSData *clearData = [NSKeyedArchiver
    > archivedDataWithRootObject:employees];
    >
    > [crypto setClearTextWithData:clearData];
    >
    > NSData *encryptedData = [crypto encrypt];
    >
    >
    > if ( outError != NULL ) {
    >
    > *outError = [ NSError errorWithDomain:NSOSStatusErrorDomain code :unimpErr
    > userInfo:NULL];
    >
    > }
    >
    > return encryptedData bytes;
    >
    > }
    >
    >
    > This doesn't work, but my question is two-fold:
    >
    >
    > 1. Where is my logic flawed and why won't this work?
    >
    >
    > 2. Can this code be functional or should I encrypt individual NSStrings in
    > my models (I would rather obfuscate the entire file)?
    >
    >
    > Thanks in advance!
    >
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today