0

I have a huge encrypted file(mp4) about 700MB. Header of file is encrypted about (1MB+dummy bytes). The decryption of this file is done successfully.

Now i want to remove the (1MB+dummy) encrypted bytes in file (same file) & replace them with decrypted bytes.

How can i achieve the same? Any help in this regard will be appreciated. Thanks in advance.

NSFileHandle *encVideoFile=[NSFileHandle  fileHandleForUpdatingAtPath:**encVideoFilePath**];  

//the data is read from file by using readDataOfLength method & decrypted (decryptedData). 

[encVideoFile seekToFileOffset:0];
[encVideoFile writeData:decryptedData];

[encVideoFile closeFile];

The problem that i'm facing here is I've (1MB+ DummyBytes) encrypted & when I Decrypt 1MB+ DummyBytes I Get around 0.9MB (Decrypted bytes).

So Still my problem persists(0.9MB+ Some Unwanted Bytes + Unencrypted Bytes).

Suggestions on how to overcome will be helpful.

Pranav Jaiswal
  • 3,752
  • 3
  • 32
  • 50

2 Answers2

2

I assume your goal is to avoid reading the entire file into memory, correct? There are two solutions, depending on how you want to attack it:

First would be a memory mapped file. Keep in mind that the size of the header can't change.

Alternately, you can use NSFileHandle, as you suggest. Open it for "updating," seek to the beginning of the file, and write the new bytes. Then close. Again, the size of the header can't change.


Your edit suggests that you are trying to "compact" the file to remove the "unwanted bytes." This is impossible without rewriting the file. The iPhone file system has no mechanism for removing data from anywhere but the end of a file.

The best solution usually would be to decrypt the header before writing it in the first place. Or modifying your reading algorithm to skip over the unwanted bytes.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Thanks. I've used NSFileHandler but I'm facing an issue after writing decrypted data(bytes) to file (Code updated in my Question). Kindly, help me out in resolving the same. – Pranav Jaiswal Feb 07 '12 at 07:38
0

Did you try using the below function? It may help you to get rid of the header bytes. Then you can have the decrypted bytes in a variable and append the latter's value.

- (NSData *)subdataWithRange:(NSRange)range;
cocoakomali
  • 1,346
  • 1
  • 8
  • 7