Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:22:33+00:00 2026-06-17T23:22:33+00:00

This is a follow-up to Asynchronously decrypt a large file with RNCryptor on iOS

  • 0

This is a follow-up to Asynchronously decrypt a large file with RNCryptor on iOS

I’ve managed to asynchronously decrypt a large, downloaded file (60Mb) with the method described in this post, corrected by Calman in his answer.

It basically goes like this:

int blockSize = 32 * 1024;
NSInputStream *cryptedStream = [NSInputStream inputStreamWithFileAtPath:...];
NSOutputStream *decryptedStream = [NSOutputStream output...];

[cryptedStream open];
[decryptedStream open];

RNDecryptor *decryptor = [[RNDecryptor alloc] initWithPassword:@"blah" handler:^(RNCryptor *cryptor, NSData *data) {
    NSLog("Decryptor recevied %d bytes", data.length);
    [decryptedStream write:data.bytes maxLength:data.length];
    if (cryptor.isFinished) {
        [decryptedStream close];
        // call my delegate that I'm finished with decrypting
    }
}];

while (cryptedStream.hasBytesAvailable) {
    uint8_t buf[blockSize];
    NSUInteger bytesRead = [cryptedStream read:buf maxLength:blockSize];
    NSData *data = [NSData dataWithBytes:buf length:bytesRead];

    [decryptor addData:data];
    NSLog("Sent %d bytes to decryptor", bytesRead);
}

[cryptedStream close];
[decryptor finish];

However, I’m still facing a problem: the whole data is loaded in memory before being decrypted. I can see a bunch of “Sent X bytes to decryptor”, and after that, the same bunch of “Decryptor recevied X bytes” in the console, when I’d like to see “Sent, received, sent, receives, …”.

That’s fine for small (2Mb) files, or with large (60Mb) files on simulator; but on a real iPad1 it crashes due to memory constraints, so obviously I can’t keep this procedure for my production app.

I feel like I need to send the data to the decryptor by using dispatch_async instead of blindly sending it in the while loop, however I’m completely lost. I’ve tried:

  • creating my own queue before the while, and using dispatch_async(myQueue, ^{ [decryptor addData:data]; });
  • the same, but dispatching the whole code inside of the while loop
  • the same, but dispatching the whole while loop
  • using RNCryptor-provided responseQueue instead of my own queue

Nothing works amongst these 4 variants.

I don’t have a complete understanding of dispatch queues yet; I feel the problem lies here. I’d be glad if somebody could shed some light on this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:22:35+00:00Added an answer on June 17, 2026 at 11:22 pm

    If you only want to process one block at a time, then only process a block when the first block calls you back. You don’t need a semaphore to do that, you just need to perform the next read inside the callback. You might want an @autoreleasepool block inside of readStreamBlock, but I don’t think you need it.

    When I have some time, I’ll probably wrap this directly into RNCryptor. I opened Issue#47 for it. I am open to pull requests.

    // Make sure that this number is larger than the header + 1 block.
    // 33+16 bytes = 49 bytes. So it shouldn't be a problem.
    int blockSize = 32 * 1024;
    
    NSInputStream *cryptedStream = [NSInputStream inputStreamWithFileAtPath:@"C++ Spec.pdf"];
    NSOutputStream *decryptedStream = [NSOutputStream outputStreamToFileAtPath:@"/tmp/C++.crypt" append:NO];
    
    [cryptedStream open];
    [decryptedStream open];
    
    // We don't need to keep making new NSData objects. We can just use one repeatedly.
    __block NSMutableData *data = [NSMutableData dataWithLength:blockSize];
    __block RNEncryptor *decryptor = nil;
    
    dispatch_block_t readStreamBlock = ^{
      [data setLength:blockSize];
      NSInteger bytesRead = [cryptedStream read:[data mutableBytes] maxLength:blockSize];
      if (bytesRead < 0) {
        // Throw an error
      }
      else if (bytesRead == 0) {
        [decryptor finish];
      }
      else {
        [data setLength:bytesRead];
        [decryptor addData:data];
        NSLog(@"Sent %ld bytes to decryptor", (unsigned long)bytesRead);
      }
    };
    
    decryptor = [[RNEncryptor alloc] initWithSettings:kRNCryptorAES256Settings
                                             password:@"blah"
                                              handler:^(RNCryptor *cryptor, NSData *data) {
                                                NSLog(@"Decryptor recevied %ld bytes", (unsigned long)data.length);
                                                [decryptedStream write:data.bytes maxLength:data.length];
                                                if (cryptor.isFinished) {
                                                  [decryptedStream close];
                                                  // call my delegate that I'm finished with decrypting
                                                }
                                                else {
                                                  // Might want to put this in a dispatch_async(), but I don't think you need it.
                                                  readStreamBlock();
                                                }
                                              }];
    
    // Read the first block to kick things off    
    readStreamBlock();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am tring to follow this post post and combine it with the ShootThemUp
I have this follow code in my javascript. I call this function when the
I follow this tutorial online exactly but somehow it's giving me errors. Saying there
I follow this rule but some of my colleagues disagree with it and argue
I just follow this DIHQuickStart , try to import data to solr from mysql.
Sorry to follow this topic http://stackoverflow.com/questions/11044825/javascript-run-on-ie-7 I want to use document.GetElementByClassName ON INTERNET EXPLORE
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
If you follow this link to one of the pages on my site and
I tried to follow this but the default modelbinder let my array null on
I am trying to follow this article to do the same for adding a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.