Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. Encryption is a decent method of scrambling the data but i don't know how to implement encription concept.
  2.  
  3. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
  4. NSArray *arrData = [[NSArray alloc]initWithContentsOfFile:filePath];
  5.  
  6. NSData *datas = [NSKeyedArchiver archivedDataWithRootObject:arrData];
  7. [datas writeToFile:filePath atomically:YES];
  8.  
  9. @implementation TestViewController
  10.  
  11. - (void)viewDidLoad
  12. {
  13. [super viewDidLoad];
  14.  
  15. NSString *path = [[NSBundle mainBundle] pathForResource:@"Encryption-Test" ofType:@"enc"];
  16. NSData *passEncryptedData =[[NSData alloc] initWithContentsOfFile:path];
  17. NSString *pass = @"asdasd";
  18.  
  19. NSData *dataDecrypted = [RNOpenSSLDecryptor decryptData:passEncryptedData withSettings:kRNCryptorAES256Settings password:pass error:nil];
  20. id plist = [NSPropertyListSerialization propertyListFromData:dataDecrypted mutabilityOption:NSPropertyListImmutable format:nil errorDescription:nil];
  21.  
  22. assert(plist);
  23. self.text.text = [plist description];
  24. }
  25.  
  26. @end
  27.  
  28. NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"questions.enc"];
  29.  
  30. if ([fileManager fileExistsAtPath:txtPath] == NO) {
  31. NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"enc"];
  32. [fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
  33. }
  34.  
  35. NSString *filePath1 = [documentsDirectory stringByAppendingPathComponent:@"questions.encr"];
  36.  
  37. NSData *passEncryptedData =[[NSData alloc] initWithContentsOfFile:filePath1] ;
  38. NSString *pass = @"asdf"; // Insert your password from step 1
  39.  
  40. NSData *dataDecrypted = [RNOpenSSLDecryptor decryptData:passEncryptedData withSettings:kRNCryptorAES256Settings password:pass error:&error];
  41.  
  42. NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"questionsDECRYPTED.plist"]; //The Decrypted file saved here
  43. [dataDecrypted writeToFile:appFile atomically:YES];
  44.  
  45. NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"questions.enc"];
  46.  
  47. if ([fileManager fileExistsAtPath:txtPath] == NO) {
  48. NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"enc"];
  49. [fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
  50. }
  51.  
  52. NSString *filePath1 = [documentsDirectory stringByAppendingPathComponent:@"questions.encr"];
  53.  
  54. NSData *passEncryptedData =[[NSData alloc] initWithContentsOfFile:filePath1] ;
  55. NSString *pass = @"asdf"; // Insert your password from step 1
  56.  
  57. NSData *dataDecrypted = [RNOpenSSLDecryptor decryptData:passEncryptedData withSettings:kRNCryptorAES256Settings password:pass error:&error];
  58.  
  59. NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"questionsDECRYPTED.plist"]; //The Decrypted file saved here
  60. [dataDecrypted writeToFile:appFile atomically:YES];
  61.  
  62. NSDictionary *Your_NSDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  63. @"Obj1", @"Key1",
  64. @"Obj2", @"Key2", nil];
  65. //store dictionary
  66. NSMutableData *yourData = [[NSMutableData alloc] init];
  67. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  68. [archiver encodeObject:Your_NSDictionary forKey: @"key"];
  69. [archiver finishEncoding];
  70. [yourData writeToFile:@"FilePath" atomically:YES];
  71.  
  72. NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Data"
  73. ofType:@"plist"];
  74. NSDictionary* data = [NSDictionary dictionaryWithContentsOfFile:filePath];
  75. NSMutableDictionary * rootObject;
  76. rootObject = [NSMutableDictionary dictionary];
  77. [rootObject setValue: data forKey:@"accounts"];
  78. [NSKeyedArchiver archiveRootObject: rootObject toFile: path];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement