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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:40:39+00:00 2026-06-04T06:40:39+00:00

I have a some code below. My problem that i have a strange ending

  • 0

I have a some code below. My problem that i have a strange ending of encrypted and decrypted char array.

#include <iostream>
#include <openssl\evp.h>

#define AES_BLOCK_SIZE 16

static EVP_CIPHER_CTX Encrypt_Context, Decrypt_Context;

int aes_init(unsigned char *key_data, int key_data_len, unsigned char *salt, EVP_CIPHER_CTX *e_ctx, 
             EVP_CIPHER_CTX *d_ctx)
{
  int i, nrounds = 1;
  unsigned char key[16], iv[16];

  i = EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(), salt, key_data, key_data_len, nrounds, key, iv);
  if (i != 16) {
    printf("Key size is %d bits - should be 128 bits\n", i);
    return -1;
  }

  EVP_CIPHER_CTX_init(e_ctx);
  EVP_EncryptInit_ex(e_ctx, EVP_aes_128_cbc(), NULL, key, iv);
  EVP_CIPHER_CTX_init(d_ctx);
  EVP_DecryptInit_ex(d_ctx, EVP_aes_128_cbc(), NULL, key, iv);

  return 0;
}

unsigned char *aes_encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext, int *len)
{
  int c_len = *len + AES_BLOCK_SIZE, f_len = 0;
  unsigned char *ciphertext = (unsigned char *)malloc(c_len);
  EVP_EncryptInit_ex(e, NULL, NULL, NULL, NULL);
  EVP_EncryptUpdate(e, ciphertext, &c_len, plaintext, *len);
  EVP_EncryptFinal_ex(e, ciphertext+c_len, &f_len);
  *len = c_len + f_len;
  return ciphertext;
}

unsigned char *aes_decrypt(EVP_CIPHER_CTX *e, unsigned char *ciphertext, int *len)
{
  int p_len = *len, f_len = 0;
  unsigned char *plaintext = (unsigned char*)malloc(p_len);
  EVP_DecryptInit_ex(e, NULL, NULL, NULL, NULL);
  EVP_DecryptUpdate(e, plaintext, &p_len, ciphertext, *len);
  EVP_DecryptFinal_ex(e, plaintext+p_len, &f_len);
  *len = p_len + f_len;
  return plaintext;
}

void main()
{
    unsigned char * data_to_encrypt = (unsigned char*)"aaaaaaaaabbbbbbbbbbbbbbccccccccc";
    unsigned char *key_data = (unsigned char *)"aaaaaaaaaaaaaaaa";
    int key_data_len = strlen((const char*) key_data);
    char * ciphertext;
    char * plaintext;

    aes_init(key_data, 16, NULL, &Encrypt_Context, &Decrypt_Context);

    int length = strlen((const char*) data_to_encrypt)-1;

    printf("Clear text :%s\n", data_to_encrypt);

    ciphertext = (char *)aes_encrypt(&Encrypt_Context, data_to_encrypt, &length);
    printf("Crypted text :%s\n", ciphertext);

    plaintext = (char *)aes_decrypt(&Decrypt_Context, (unsigned char*)ciphertext, &length);
    printf("Decrypted text :%s\n", plaintext);
}

With this code i getting in console:

Clear text :aaaaaaaaabbbbbbbbbbbbbbccccccccc
Crypted text :°ЁТ☼#єз▬├^^,♦Ёфм:ЇъШ╙y╒КzukЩu@8¤═══════════════¤¤¤¤
Decrypted text :aaaaaaaaabbbbbbbbbbbbbbcccccccc☺¤¤¤¤

What is ═══════════════¤¤¤¤ in the end of crypted text and ☺¤¤¤¤ in the end of decrypted? What’s wrong with my code?

Thanks!

  • 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-04T06:40:40+00:00Added an answer on June 4, 2026 at 6:40 am

    The encryption routines deal with binary data, not with C strings, the arrays they’re returning pointers to are not null-terminated, so when printing with printf (which expects a null-terminated string) you’re getting random garbage that happens to be located right after these arrays.

    To solve this, ensure null-termination, use something like fwrite or iterate over the characters in order to print the arrays.

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

Sidebar

Related Questions

I have some problem with my cfml website. I have used the below code
Edit: original question below, but I revise it now that I have some code
I have some code as shown below: - (IBAction)startButtonPressed:(id)sender { statusText.text = @Processing...; //here
I have some really funky code. As you can see from the code below
UPDATE - I have fixed some mistakes in the code below and the images
I'm wondering about the .childNodes property, I have the code below, and for some
I have some code using a System.Transactions.TransactionScope , that creating a new instance of
I am getting some strange behavior involving database queries that I have never seen
I have some code below showing my JQUERY & HTML. Basically I have tried
I have just hit a confusing problem when trying to compile some code using

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.