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

  • Home
  • SEARCH
  • 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 8823867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:27:55+00:00 2026-06-14T06:27:55+00:00

I download a sample code of AES encryption from https://gist.github.com/2436364 I modified part of

  • 0

I download a sample code of AES encryption from https://gist.github.com/2436364
I modified part of the source code to meet my project requirement as below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mcrypt.h>
#include <math.h>
#include <stdint.h>

int encrypt(void* buffer, int buffer_len, char* IV, char* key, int key_len){
  MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
  int blocksize = mcrypt_enc_get_block_size(td);
  if( buffer_len % blocksize != 0 )
    return 1;

  mcrypt_generic_init(td, key, key_len, IV);
  mcrypt_generic(td, buffer, buffer_len);
  mcrypt_generic_deinit (td);
  mcrypt_module_close(td);

  return 0;
}

int decrypt(void* buffer, int buffer_len, char* IV, char* key, int key_len){
  MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
  int blocksize = mcrypt_enc_get_block_size(td);
  if( buffer_len % blocksize != 0 )
    return 1;

  mcrypt_generic_init(td, key, key_len, IV);
  mdecrypt_generic(td, buffer, buffer_len);
  mcrypt_generic_deinit (td);
  mcrypt_module_close(td);

  return 0;
}

void display(char* ciphertext, int len){
  int v;
  for (v=0; v<len; v++){
    if(v % 16 == 0) putchar('\n');
    printf("%02X ", (unsigned char)ciphertext[v]);
  }
  printf("\n");
}

int main()
{
  unsigned char plaintext[256];
  unsigned char IV[16] = {0xFA,0x8D,0x46,0x54,0x83,0xC6,0xED,0xD8,
    0x37,0x5A,0x9D,0xC1,0x3E,0x69,0x1B,0x04};
  unsigned char key[16] = {0x53, 0x75, 0x7a, 0x79, 0x26, 0x52, 0x69, 0x63,
    0x68, 0x42, 0x65, 0x72, 0x67, 0x69, 0x6e, 0x20};
  int keysize = 16; /* 128 bits */
  char* buffer;
  int buffer_len = 256;
  FILE *fp;

  fp = fopen("readbuf.bin", "r");
  fread(plaintext, 1, 256, fp);
  fread(plaintext, 1, 256, fp);
  fclose(fp);

  buffer = calloc(1, buffer_len);
  strncpy(buffer, plaintext, buffer_len);

  //printf("plain:   %s\n", plaintext);
  encrypt(buffer, buffer_len, IV, key, keysize); 
  printf("cipher:  "); display(buffer , buffer_len);
  //decrypt(buffer, buffer_len, IV, key, keysize);
  //printf("decrypt: %s\n", buffer);

  return 0;
}

I need to encrypt byte 255~511 of readbuf.bin
I get the result byte 0 ~ 0x6F is correct, but since byte 0x70 the result is incorrect
Is there mistake in the source code?

  • 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-14T06:27:56+00:00Added an answer on June 14, 2026 at 6:27 am

    Thanks everyone who take care this question, I found the root cause through review source code again and again…

    the root cause is the function call:

    strncpy(buffer, plaintext, buffer_len);
    

    Everything going well, while I replace strncpy() with memcpy()

    memcpy(buffer, plaintext, buffer_len);
    

    Also, I follow WhozCraig suggestion modifying the source code as below:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <mcrypt.h>
    #include <math.h>
    #include <stdint.h>
    
    int encrypt(void* buffer, int buffer_len, char* IV, char* key, int key_len)
    {
      MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
      int blocksize = mcrypt_enc_get_block_size(td);
      int n_blocks = buffer_len / blocksize;
      if( buffer_len % blocksize != 0 )
        return 1;
    
      mcrypt_generic_init(td, key, key_len, IV);
      for (int i = 0; i < n_blocks; i++) 
        mcrypt_generic(td, ((unsigned char* )buffer) + (i * blocksize), blocksize);
      mcrypt_generic_deinit (td);
      mcrypt_module_close(td);
    
      return 0;
    }
    
    int decrypt(void* buffer, int buffer_len, char* IV, char* key, int key_len){
      MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
      int blocksize = mcrypt_enc_get_block_size(td);
      int n_blocks = buffer_len / blocksize;
      if( buffer_len % blocksize != 0 )
        return 1;
    
      mcrypt_generic_init(td, key, key_len, IV);
      for (int i = 0; i < n_blocks; i++) 
        mdecrypt_generic(td, ((unsigned char *)buffer) + (i * blocksize), blocksize);
      mcrypt_generic_deinit (td);
      mcrypt_module_close(td);
    
      return 0;
    }
    
    void display(char* ciphertext, int len){
      int v;
      for (v=0; v<len; v++){
        if(v % 16 == 0) putchar('\n');
        printf("%02X ", (unsigned char)ciphertext[v]);
      }
      printf("\n");
    }
    
    int main()
    {
      unsigned char plaintext[256];
      unsigned char IV[16] = {0xFA,0x8D,0x46,0x54,0x83,0xC6,0xED,0xD8,
        0x37,0x5A,0x9D,0xC1,0x3E,0x69,0x1B,0x04};
      unsigned char key[16] = {0x53, 0x75, 0x7a, 0x79, 0x26, 0x52, 0x69, 0x63,
        0x68, 0x42, 0x65, 0x72, 0x67, 0x69, 0x6e, 0x20};
      int keysize = 16; /* 128 bits */
      char* buffer;
      int buffer_len = 256;
      FILE *fp;
    
      fp = fopen("readbuf.bin", "r");
      fread(plaintext, 1, 256, fp);
      fread(plaintext, 1, 256, fp);
      fclose(fp);
    
      buffer = calloc(1, buffer_len);
      //strncpy(buffer, plaintext, buffer_len);
      memcpy(buffer, plaintext, buffer_len);
    
      printf("plain:   %s\n", plaintext);
      encrypt(buffer, buffer_len, IV, key, keysize); 
      printf("cipher:  "); display(buffer , buffer_len);
      decrypt(buffer, buffer_len, IV, key, keysize);
      printf("decrypt: %s\n", buffer);
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I download DnDNS from http://dndns.codeplex.com/ and get it work at local IIS. sample code:
I'm trying to run the sample code from this Sun tutorial: http://download.oracle.com/javase/6/docs/technotes/guides/rmi/hello/hello-world.html I've copied
When I have download sample code from this link and than try to build
I have completed client side code by download sample from git for push notification
I download some sample code from deitel, but it refuse to build. I already
I have download sample tablelayout source code it's working fine. I am trying to
I have the following sample code where I download a pdf from the European
I'm practicing drawing polyline on the Map. I've adopted Sample code from download here.
I was trying to make a sample code run download by the link http://www.magtek.com/support/software/downloads/sw/99510108.zip
I'm looking at Haacks article here - http://haacked.com/archive/2009/07/31/single-project-areas.aspx When I download the sample I

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.