Skip to content

Instantly share code, notes, and snippets.

@yxjxx
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yxjxx/d65ebc8c33c64a23ea42 to your computer and use it in GitHub Desktop.
Save yxjxx/d65ebc8c33c64a23ea42 to your computer and use it in GitHub Desktop.
- (NSDictionary *)encrypt:(NSData *)fileData forSuffixFile:(NSString *)fileSuffix{
NSMutableData *afterFile = [[NSMutableData alloc] init];
[afterFile appendData:fileData];
NSMutableData *keyContainer = [[NSMutableData alloc] init];
int PERCENTAGE = 1;
int ex_location = 0;
int ex_length = 0;
ex_length = PERCENTAGE;
int fileHeaderLength = 0;
int locationStepLength = 0;
NSArray *JPEGS = @[@"jfi", @"jif", @"jfif", @"jpe", @"jpeg", @"jpg"];
NSArray *TEXT = @[@"txt", @"c", @"h", @"cpp", @"java", @"py", @"m"];
if([JPEGS containsObject:fileSuffix]){
fileHeaderLength = 16;
locationStepLength = 250;
}
else if([TEXT containsObject:fileSuffix]){
fileHeaderLength = 0;
locationStepLength = 100;
}
for(ex_location += fileHeaderLength; ex_location+ex_length < fileData.length; ex_location += locationStepLength){
[keyContainer appendData:[NSData dataWithBytes:&ex_location length:sizeof(ex_location)]];
[keyContainer appendData:[NSData dataWithBytes:&ex_length length:sizeof(ex_length)]];
[keyContainer appendData:[fileData subdataWithRange:NSMakeRange(ex_location, ex_length)]];
[afterFile resetBytesInRange:NSMakeRange(ex_location, ex_length)];
}
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];
[myDict setValue:afterFile forKey:@"afterFileData"];
[myDict setValue:keyContainer forKey:@"keyData"];
return myDict;
}
- (NSData *)decrypt:(NSData *)fileData withKey:(NSData *)keyContainer{
NSMutableData *afterFile = [[NSMutableData alloc] init];
[afterFile appendData:fileData];
NSMutableArray *myArray = [[NSMutableArray alloc] init];
for(int i=0;i<keyContainer.length;){
int offset = 0;
int snippetLen = 0;
NSData *snippetContent = [[NSData alloc] init];
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];
offset = *(int*)[[keyContainer subdataWithRange:NSMakeRange(i, 4)] bytes];
i+=4;
snippetLen = *(int*)[[keyContainer subdataWithRange:NSMakeRange(i, 4)] bytes];
i+=4;
snippetContent = [keyContainer subdataWithRange:NSMakeRange(i, snippetLen)];
i+=snippetLen;
[myDict setValue:[[NSString alloc] initWithFormat:@"%d",offset] forKey:@"offset"];
[myDict setValue:snippetContent forKey:@"snippetContent"];
[myArray addObject:myDict];
}
for (NSDictionary *myDict in myArray) {
int ex_loc = [[myDict valueForKey:@"offset"] intValue];
NSMutableData *ex_data = [myDict valueForKey:@"snippetContent"];
[afterFile replaceBytesInRange:NSMakeRange(ex_loc, ex_data.length) withBytes:[ex_data bytes] length:ex_data.length];
}
return afterFile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment