如何通过AirDrop传输加密数据

时间:2014-10-24 22:22:23

标签: ios objective-c encryption airdrop

我想转移我的NSObject" RND_PatientData"使用AirDrop使用应用程序从我的应用程序加密到另一个人。 目前,我的数据使用以下代码保存:

 - (RND_PatientData *)data {
if (_data != nil) return _data;

NSString *dataPath = [_docPath stringByAppendingPathComponent:kDataFile];
NSData *codedData = [[NSData alloc] initWithContentsOfFile:dataPath];
if (codedData == nil) return nil;
NSString *deviceName = [[UIDevice currentDevice] name];


NSData *decryptedData = [RNDecryptor decryptData:codedData withSettings:kRNCryptorAES256Settings password:deviceName error:nil];


NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:decryptedData];
_data = [unarchiver decodeObjectForKey:kDataKey];
[unarchiver finishDecoding];
return _data;
}

 - (void)saveData {

if (_data == nil) return;

[self createDataPath];

NSString *dataPath = [_docPath stringByAppendingPathComponent:kDataFile];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:_data forKey:kDataKey];
[archiver finishEncoding];
NSError *error;
NSString *deviceName = [[UIDevice currentDevice] name];

NSData *encryptedData = [RNEncryptor encryptData:data
                                    withSettings:kRNCryptorAES256Settings
                                        password:deviceName
                                           error:&error];

[encryptedData writeToFile:dataPath atomically:YES];

在保存之前,还使用设备UDID和RNDecryptor库对此数据进行加密。 现在,我希望perosn能够通过AirDrop传输数据。 解密发送方电话上的数据然后将其转换为未加密并使用相同的框架在接收方电话上对其进行加密或使用发送方设备UDID对其进行加密和解密是否更好?

我将当前数据加载为NSMutableArray:

   _patients = [RND_PatientDB loadDocs];

方法如下:

   + (NSMutableArray *)loadDocs {

// Get private docs dir
NSString *documentsDirectory = [RND_PatientDB getPrivateDocsDir];
NSLog(@"Loading patients from %@", documentsDirectory);

// Get contents of documents directory
NSError *error;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
if (files == nil) {
    NSLog(@"Error reading contents of documents directory: %@", [error localizedDescription]);
    return nil;
}
// Create Patients for each file
NSMutableArray *retval = [NSMutableArray arrayWithCapacity:files.count];
for (NSString *file in files) {
    if ([file.pathExtension compare:@"patients" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:file];
        RND_Patient *doc = [[RND_Patient alloc] initWithDocPath:fullPath];
        [retval addObject:doc];
    }
}

return retval;

  }
  + (NSString *)nextPatientDocPath {

// Get private docs dir
NSString *documentsDirectory = [RND_PatientDB getPrivateDocsDir];

// Get contents of documents directory
NSError *error;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
if (files == nil) {
    NSLog(@"Error reading contents of documents directory: %@", [error localizedDescription]);
    return nil;
}
// Search for an available name
int maxNumber = 0;
for (NSString *file in files) {
    if ([file.pathExtension compare:@"patients" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
        NSString *fileName = [file stringByDeletingPathExtension];
        maxNumber = MAX(maxNumber, fileName.intValue);
    }
}
// Get available name
NSString *availableName = [NSString stringWithFormat:@"%d.patients", maxNumber+1];
return [documentsDirectory stringByAppendingPathComponent:availableName];

}

我已尝试按照此处的说明通过AirDrop发送NSArray,但我无法理解证书的工作原理。任何人都有资源我可以用来了解这个程序的更多信息?我也是以正确的方式做到这一点,还是有一种更简单的方法将使用我的应用程序将由RND_PATIENT NSObject组成的_patients的NSMutableArray发送给另一个人?

0 个答案:

没有答案