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 8133987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:51:16+00:00 2026-06-06T09:51:16+00:00

Assume that we have a large file which can be read in chunks of

  • 0

Assume that we have a large file which can be read in chunks of 1024 bytes or so, how can I encrypt and decrypt each chunk using Salsa or Chacha 20?

Also, where would I specify the number of rounds (i.e. 8, 12, or 20)?

So far, I haven’t been able to figure it out by looking at the eSTREAM test package 🙁

I’ve downloaded the following files via eSTREAM and the Salsa20 homepage:

  • chacha.c
  • ecrypt-config.h
  • ecrypt-machine.h
  • ecrypt-portable.h
  • ecrypt-sync.h

And I see the comments in encrypt-sync.h talk about calling functions in this order:

  • ECRYPT_keysetup();
  • ECRYPT_ivsetup();
  • ECRYPT_encrypt_bytes();

But I have absolutely no idea exactly what I’m supposed to be supplying as parameters to make this work…

Here’s my best attempt so far, starting with one small string of plaintext (my C is rusty… it’s possible I’ve made some basic mistake, though I can’t see it):

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "ecrypt-sync.h"

#define CHUNKSIZE 1024

void getRandomBytes(u8 **str, u32 len);
void showData(u8 *data, u8 *header);

int main(int argc, char** argv)
{
    char plaintext[CHUNKSIZE] = "THIS IS A TEST";
    ECRYPT_ctx ctx;
    u8 *key, *IV, *ciphertext, *result;

    /*
    Don't use random values till we get it working with zeroes at least
    getRandomBytes(&key, ECRYPT_MAXKEYSIZE/8);
    getRandomBytes(&IV, ECRYPT_MAXIVSIZE/8);
    */

    key = (u8 *)calloc((size_t)ECRYPT_MAXKEYSIZE/8, sizeof(u8));
    IV = (u8 *)calloc((size_t)ECRYPT_MAXIVSIZE/8, sizeof(u8));

    printf("Encrypting [%s] using random %d bit key and %d bit IV:\n", plaintext, ECRYPT_MAXKEYSIZE, ECRYPT_MAXIVSIZE);

    ECRYPT_init();
    ECRYPT_keysetup(&ctx, key, ECRYPT_MAXKEYSIZE, ECRYPT_MAXIVSIZE);
    ECRYPT_ivsetup(&ctx, IV);

    ciphertext = (u8 *)calloc((size_t)CHUNKSIZE, sizeof(u8));

    ECRYPT_encrypt_bytes(&ctx, plaintext, ciphertext, CHUNKSIZE);

    //showData(ciphertext, "CIPHERTEXT");

    result = (u8 *)calloc((size_t)CHUNKSIZE, sizeof(u8));

    ECRYPT_decrypt_bytes(&ctx, ciphertext, result, CHUNKSIZE);
    printf("And now decrypting back: [%s]\n", result);

    return 0;
}

void showData(u8 *data, u8 *header) {
    printf("\n-----BEGIN %s-----\n%s\n-----END %s-----\n", header, data, header);

}

void getRandomBytes(u8 **str, u32 len) {
    int fd = open("/dev/random", O_RDONLY);
    char *ptr = malloc((size_t) + 1);

    read(fd, ptr, len);
    close(fd);

    ptr[len] = '\0';
    *str = ptr; 
}

Results are like:

Encrypting [THIS IS A TEST] using random 256 bit key and 64 bit IV:
And now decrypting back: [(bunch of random characters)]

Where it should be:

And now decrypting back: [THIS IS A TEST]

Feel free to provide your solution in either C or C++

Thank you!

  • 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-06T09:51:19+00:00Added an answer on June 6, 2026 at 9:51 am

    If you are going to use Salsa20 in real code and you are asking questions like this, you probably want to use the NaCl library with nice friendly C++ wrappers.

    See The NaCl website.

    To answer your actual question: you need to set the IV up again for the decryption operation. The IV consists of your nonce and a block offset. The encrypt/decrypt functions increment the offset, giving your code a different IV for the encryption and decryption functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume that on a hard drive I have some very large data file
I have a very large file that looks like this (see below). I have
I have a large disk file (around 8 GB) containing several million records that
So let's assume that we have a large project with a lot of pages
I have a VB.Net program that reads in a flat file, and then parses
I have large text files upon which all kinds of operations need to be
I need to parse a large XML file (>1 GB) which is located on
I have an Excel file with a large set of VBA code. There are
Suppose you have a large file made up of a bunch of fixed size
I have a legacy file format which I'm converting into XML for processing. The

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.