Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
thank u so much.

everything is working fine.

but one problem is occurred when i reopen the file after closing previous one and again start writing with encryption.

and same when i restart my application and again trying to encryption/decryption of last saved file.

i m using a WriteEncFile class that is derived from NSObject to write file,here diffrent function for initialize,open,write and close the file.

i m initialize MyTransformer in open function where i open the file,and release it in close function where i close the file.

and executing the these function from different class.

so when i close the file so MyTransformer is release and again initialize when i open the file.

like:

Code:
- (void) open
{ 
  mTransformer = [[MyTransformer alloc] initWithKey:@"ABCDEFG"];

  mFile = fopen([filename fileSystemRepresentation], "a");
  
}

- (void) close
{
 
  if (mFile)
  {
    fclose(mFile);
    mFile = NULL;
	  
	[mTransformer release];
  }
}

now i dont understand now where i m wrong please guide me for this

thanks in advance.
 
Last edited by a moderator:
thank u so much.

everything is working fine.

but one problem is occurred when i reopen the file after closing previous one and again start writing with encryption.

and same when i restart my application and again trying to encryption/decryption of last saved file.

i m using a WriteEncFile class that is derived from NSObject to write file,here diffrent function for initialize,open,write and close the file.

i m initialize MyTransformer in open function where i open the file,and release it in close function where i close the file.

and executing the these function from different class.

so when i close the file so MyTransformer is release and again initialize when i open the file.

like:

Code:
- (void) open
{ 
  mTransformer = [[MyTransformer alloc] initWithKey:@"ABCDEFG"];

  mFile = fopen([filename fileSystemRepresentation], "a");
  
}

- (void) close
{
 
  if (mFile)
  {
    fclose(mFile);
    mFile = NULL;
	  
	[mTransformer release];
  }
}

now i dont understand now where i m wrong please guide me for this

thanks in advance.

If you want to append to an encrypted file, you'll need to reinitialise theKeyIndex in MyTransformer to the correct value after you re-open the file.

Firstly remove the reset method in MyTransformer. You don't need it, and it's about to be broken.

Add a new initialiser to MyTransformer and add a declaration for it the header file.
Code:
- (id)initWithKey:(NSString *)aKey keyIndex:(NSUInteger)aKeyIndex
{
    NSAssert([aKey length] > 0, @"aKey is nil or has zero length");
    NSAssert(aKeyIndex >= 0 && aKeyIndex < [aKey length], @"aKeyIndex is out of range");
    self = [super init];
    if (self) {
        theKeyAsUTF8 = [[aKey dataUsingEncoding:NSUTF8StringEncoding] retain];
        NSAssert(theKeyAsUTF8, @"failed to encode aKey in UTF-8");
        theKeyIndex = aKeyIndex;
    }
    return self;
}

Change initWithKey: in MyTransformer to:
Code:
- (id)initWithKey:(NSString *)aKey
{
    return [self initWithKey:aKey keyIndex:0];
}

Then in your open method:
Code:
- (void) open
{ 
  mFile = fopen([filename fileSystemRepresentation], "a");
  // TODO: Handle error when fopen returns NULL
  NSString *key = @"ABCDEFG";
  long fileLength = ftell(mFile);
  // TODO: Handle error when ftell returns -1
  NSUInteger keyIndex = fileLength % [key length];
  mTransformer = [[MyTransformer alloc] initWithKey:key keyIndex:keyIndex];
}

This uses the length of the file to calculate what theKeyIndex was before the file was last closed.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.