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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:49:57+00:00 2026-06-18T04:49:57+00:00

Hello I’m doing an encryption algorithm which reads bytes from file (any type) and

  • 0

Hello I’m doing an encryption algorithm which reads bytes from file (any type) and outputs them into a file. The problem is my encryption program takes only blocks of 16 bytes so if the file is bigger it has to be split into blocks of 16, or if there’s a way to read 16 bytes from the file each time it’s fine.

The algorithm is working fine with hard coded input of 16 bytes. The ciphered result has to be saved in a list or array because it has to be deciphered the same way later. I can’t post all my program but here’s what I do in main so far and cannot get results

static void Main(String[] args)
{
    byte[] bytes = File.ReadAllBytes("path to file");
    var stream = new StreamReader(new MemoryStream(bytes));
    byte[] cipherText = new byte[16];
    byte[] decipheredText = new byte[16];

    Console.WriteLine("\nThe message is: ");
    Console.WriteLine(stream.ReadToEnd());

    AES a = new AES(keyInput);
    var list1 = new List<byte[]>();
    for (int i = 0; i < bytes.Length; i+=16)
    {
        a.Cipher(bytes, cipherText);
        list1.Add(cipherText);
    }

    Console.WriteLine("\nThe resulting ciphertext is: ");
    foreach (byte[] b in list1)
    {       
        ToBytes(b);
    }
}

I know that my loops always add the first 16 bytes from the byte array but I tried many ways and nothing work. It won’t let me index the bytes array or copy an item to a temp variable like temp = bytes[i]. The ToBytes method is irrelevant, it just prints the elements as bytes.

  • 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-18T04:49:59+00:00Added an answer on June 18, 2026 at 4:49 am

    I would like to recommend you to change the interface for your Cipher() method: instead of passing the entire array, it would be better to pass the source and destination arrays and offset – block by block encryption.

    Pseudo-code is below.

    void Cipher(byte[] source, int srcOffset, byte[] dest, int destOffset)
    {
        // Cipher these bytes from (source + offset) to (source + offset + 16),
        // write the cipher to (dest + offset) to (dest + offset + 16)
        // Also I'd recommend to check that the source and dest Length is less equal to (offset + 16)!
    }
    

    Usage:

    1. For small files (one memory allocation for destination buffer, block by block encryption):

      // You can allocate the entire destination buffer before encryption!
      byte[] sourceBuffer = File.ReadAllBytes("path to file");
      byte[] destBuffer = new byte[sourceBuffer.Length];
      
      // Encrypt each block.
      for (int offset = 0; i < sourceBuffer.Length; offset += 16)
      {
          Cipher(sourceBuffer, offset, destBuffer, offset);
      }
      

      So, the main advantage of this approach – it elimitates additional memory allocations: the destination array is allocated at once. There is also no copy-memory operations.

    2. For files of any size (streams, block by block encryption):

          byte[] inputBlock = new byte[16];
          byte[] outputBlock = new byte[16];
          using (var inputStream = File.OpenRead("input path"))
          using (var outputStream = File.Create("output path"))
          {
              int bytesRead;
              while ((bytesRead = inputStream.Read(inputBlock, 0, inputBlock.Length)) > 0)
              {
                  if (bytesRead < 16)
                  {
                      // Throw or use padding technique.
                      throw new InvalidOperationException("Read block size is not equal to 16 bytes");
      
                      // Fill the remaining bytes of input block with some bytes.
                      // This operation for last block is called "padding".
                      // See http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Padding
                  }
      
                  Cipher(inputBlock, 0, outputBlock, 0);
                  outputStream.Write(outputBlock, 0, outputBlock.Length);
              }
          }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have simply function to read from file while(fscanf(fp, %255[a-zA-Z],test) == 1) {
Hello there am trying to save news tweets into three different array which are
Hello i'm trying create an application allowing me host any kind of file. In
Hello i want to make a friendly url from the url which is really
hello all i am working on a project in which i have a webpage
Hello i'm doing a application in php, and i have a list of items
Hello I've got this query to get users by email, which is an unique
hello evrey body i have try really hard to write a file to android
Hello I'm making a php script to extract videos URL from Youtube result. I
Hello if i have txt file containing lines like: newmtl material0 Ka 1.000000 1.000000

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.