1

当我的应用程序不活动时,我将所有内容设置为在后台运行一些代码。我使用 BGProcessingTask。

当我暂停程序执行并输入命令e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.myApp.processing"] 时,它会测试进程并运行我的代码,但是当它到达解密我的数据的代码时它并没有完成工作。我使用RNCryptor来解密数据,但是当我的应用程序在后台时,它似乎不想运行。

这是我运行的代码:

- (void)nvmDoubleDecryptedAES256DataWithSecurityKey:(nonnull NSString*)theSecurityKey WithMasterPasswordKey:(nonnull NSString*)theMasterPassword WithCompletion:(void (^_Nullable)(NSData* _Nullable decryptedData, NSError* _Nullable error))completionBlock {
NSError *firstStatus, *secondStatus;
NSLog(@"AR - 111");
NSData *mpDecryptedData = [RNDecryptor decryptData:self withPassword:theMasterPassword error:&firstStatus];
NSLog(@"AR - 112");
if ((mpDecryptedData) && (!firstStatus)) {
    NSData *skDecryptedData = [RNDecryptor decryptData:mpDecryptedData withPassword:theSecurityKey error:&secondStatus];
    NSLog(@"AR - 113");
    if ((skDecryptedData) && (!secondStatus)) {
        if (completionBlock) completionBlock(skDecryptedData, nil);
    }
    else {
        NSError *errorSecurityKeyIncorrect = [NSError errorWithDomain:[NVMFunctions getBundleIdentifier] code:PRLErrorCodeSecurityKeyIncorrect userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"Security Key is incorrect | Decrypt error: %@", firstStatus]}]; //Keep this error code
        if (completionBlock) completionBlock(nil, errorSecurityKeyIncorrect);
    }
}
else {
    NSError *errorMasterPasswordIncorrect = [NSError errorWithDomain:[NVMFunctions getBundleIdentifier] code:PRLErrorCodeMasterPasswordKeyIncorrect userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"Master Password is incorrect | Decrypt error: %@", secondStatus]}]; //Keep this error code
    if (completionBlock) completionBlock(nil, errorMasterPasswordIncorrect);
}

}

但是在日志中我看到我一直在坚持AR - 111。一旦我重新打开我的应用程序,就会继续使用AR - 112. 如果我不能在后台解密我的数据,这似乎是一个大问题......

有谁知道修复?

完整日志:

 (lldb) e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.novem.Peral.processing"]
2020-06-03 21:38:06.120568+0200 Peral[14608:3145844] Simulating launch for task with identifier com.novem.Peral.processing
2020-06-03 21:38:14.539735+0200 Peral[14608:3146386] Starting simulated task: <BGProcessingTask: com.novem.Peral.processing>
2020-06-03 21:38:14.580935+0200 Peral[14608:3146383] AR - 111

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [self registerBackgroundTaks];

    return YES;
}

- (void)registerBackgroundTaks {
    [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:@"com.novem.Peral.processing" usingQueue:nil launchHandler:^(__kindof BGTask * _Nonnull task) {
        [self handleAppRefresh:task];
    }];
}

- (void)handleAppRefresh:(BGProcessingTask *)task {
    [self scheduleAppRefresh];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperationWithBlock: ^{
        NSString *theSecurityKey = @"key";
        NSString *theMasterPassword = @"passord";
        [self nvmDoubleDecryptedAES256DataWithSecurityKey:theSecurityKey WithMasterPasswordKey:theMasterPassword WithCompletion:^(NSData * _Nullable decryptedData, NSError * _Nullable error) {
            [task setTaskCompletedWithSuccess:!queue.isSuspended];
        }];
    }];


    [task setExpirationHandler:^{
        [queue cancelAllOperations];
    }];
}

- (void)scheduleAppRefresh {
    BGProcessingTaskRequest *request = [[BGProcessingTaskRequest alloc] initWithIdentifier:@"com.novem.Peral.processing"];
    [request setRequiresExternalPower:YES];
    [request setRequiresNetworkConnectivity:YES];
    //[request setEarliestBeginDate:[NSDate dateWithTimeIntervalSinceNow:2 * 60]]; //Start after 2 minutes

    NSError *requestError;
    [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&requestError];
    if (requestError) NSLog(@"Exception: %@", requestError);
}

编辑

查看 RNCryptor 后,如果发现它在到达某一行时会冻结。

   + (NSData *)keyForPassword:(NSString *)password salt:(NSData *)salt settings:(RNCryptorKeyDerivationSettings)keySettings
{
    NSLog(@"RN - 124831");
  NSMutableData *derivedKey = [NSMutableData dataWithLength:keySettings.keySize];
  NSLog(@"RN - 124832");

  // See Issue #77. V2 incorrectly calculated key for multi-byte characters.
  NSData *passwordData;
  NSLog(@"RN - 124833");
  if (keySettings.hasV2Password) {
      NSLog(@"RN - 1248331");
    passwordData = [NSData dataWithBytes:[password UTF8String] length:[password length]];
  }
  else {
      NSLog(@"RN - 1248332");
    passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
  }

  // Use the built-in PBKDF2 if it's available. Otherwise, we have our own. Hello crazy function pointer.
  NSLog(@"RN - 124834");
  int result;
  NSLog(@"RN - 124835");
  int (*PBKDF)(CCPBKDFAlgorithm algorithm, const char *password, size_t passwordLen,
               const uint8_t *salt, size_t saltLen,
               CCPseudoRandomAlgorithm prf, uint rounds,
               uint8_t *derivedKey, size_t derivedKeyLen);

    NSLog(@"RN - 124836");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
  PBKDF = CCKeyDerivationPBKDF ?: RN_CCKeyDerivationPBKDF;
#pragma clang diagnostic pop

    NSLog(@"RN - 124837");
  result = PBKDF(keySettings.PBKDFAlgorithm,         // algorithm
                 passwordData.bytes,                 // password
                 passwordData.length,                // passwordLength
                 salt.bytes,                         // salt
                 salt.length,                        // saltLen
                 keySettings.PRF,                    // PRF
                 keySettings.rounds,                 // rounds
                 derivedKey.mutableBytes,            // derivedKey
                 derivedKey.length);                 // derivedKeyLen

    NSLog(@"RN - 124838");
  // Do not log password here
  NSAssert(result == kCCSuccess, @"Unable to create AES key for password: %d", result);

  return derivedKey;
}

这是我得到的日志:

2020-06-04 21:37:32.238187+0200 Peral[16824:3659587] RN - 124831
2020-06-04 21:37:32.238507+0200 Peral[16824:3659587] RN - 124832
2020-06-04 21:37:32.238818+0200 Peral[16824:3659587] RN - 124833
2020-06-04 21:37:32.239046+0200 Peral[16824:3659587] RN - 1248332
2020-06-04 21:37:32.239419+0200 Peral[16824:3659587] RN - 124834
2020-06-04 21:37:32.239663+0200 Peral[16824:3659587] RN - 124835
2020-06-04 21:37:32.240287+0200 Peral[16824:3659587] RN - 124836
2020-06-04 21:37:32.240575+0200 Peral[16824:3659587] RN - 124837

所以它冻结了 result = PBKDF ....

4

0 回答 0