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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:13:09+00:00 2026-06-11T21:13:09+00:00

I’m new here so please excuse if I’m doing something wrong. I attempt to

  • 0

I’m new here so please excuse if I’m doing something wrong.

I attempt to make an array with encrypted strings, I’m using the EVP API for the encryption. This works fine, but wHen I try to use the encrypt function in a foor loop the console gives me nothing.

Here is my encrypt function:

char *encrypt(char *key, char *iv, char * source){

    //char *target;
    int in_len, out_len;
    EVP_CIPHER_CTX ctx;
    in_len=strlen((const char *)source);
    unsigned char *target = (unsigned char *) malloc(in_len);
        //printf("This is the text before ciphering: %s\n",source);
        //printf("The length of the string is: %d\n",in_len);
        //starting the encryption process
        EVP_CIPHER_CTX_init(&ctx);
        EVP_EncryptInit_ex(&ctx,EVP_aes_128_cbc(),NULL,(unsigned char*) key,(unsigned char*)iv);
        EVP_EncryptUpdate(&ctx,target,&out_len,(unsigned char*)source,in_len);
        EVP_EncryptFinal_ex(&ctx,target,&out_len);
        target[out_len] = '\0';

        //EVP_CIPHER_CTX_cleanup(&ctx);


        return ((char *)target);
}

and in main the loop:

int main(){
    char source[17]="Shahababamamaaaa";
    char key[17]="ahardtobreakkey1";
    char iv[17] = "veryinterestingv";
     int rows = 1280;
     int cols = (3*800)/16;
        char *encrypted=encrypt(key, iv, source);
        printf("encrypted: %s\n", encrypted);
        char *encrypted2;
        encrypted2=encrypt(key, iv, encrypted);
        printf("encrypted2: %s\n", encrypted2);
        char *mx[rows];
        char *in, *temp;
        in = (char *) malloc ( cols * sizeof(char) );
        temp =(char *) malloc ( strlen(encrypted) );
        int i, j;

        for (i=0; i<5; i++){
            strcpy(in,encrypted);
            for(j=0;j<3;j++){
                    printf("in: %s\n", in);
                    strcpy(temp, encrypted2);
                    printf("temp: %s\n", temp);
                    memset(encrypted2,0x00, strlen(encrypted));
                    encrypted2=encrypt(key, iv,temp);
                    printf("encrypted2 nach j=%d : %s\n",j, encrypted2);

                    mx[i]=in;
            }

        }
        printf("Stele 0 Inhalt %s\n",mx[0]);
        printf("Laenge von 1 %d\n", strlen(mx[0]));

        //system ("PAUSE");
        free(in);
        return 0;

     }

What am I missing? Is it imposible to use encrypt2 again?
Thank you very much.

  • 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-11T21:13:10+00:00Added an answer on June 11, 2026 at 9:13 pm

    As you said, the main problem is in your encrypt() function, but also how you call it. You are using malloc() to allocate memory inside your function, and never freeing it, which is a memory leak (and malloc is a no-no in c++ anyway). You are also not running the cleanup function for your ctx. And your encrypt_final is overwriting the first part of your output buffer. So, here’s a cleaned up encrypt(), and a matching decrypt():

    int encrypt(unsigned char *key, 
            unsigned char *iv, 
            unsigned char * source, 
            unsigned char* target, 
            int in_len) // Need an in length.  Not all input is going to be
                        // zero-terminated, for example if we're reading from a file
    
    {
    
        int out_len; // Return the output length.  Because it also won't be null
                     // terminated, and may contain null characters inline
    
        int final_out_len; // So that we don't overwrite out_len with the final call
        EVP_CIPHER_CTX ctx;
    
        EVP_CIPHER_CTX_init(&ctx);
        EVP_EncryptInit_ex(&ctx,EVP_aes_128_cbc(),NULL,key,iv);
        EVP_EncryptUpdate(&ctx,target,&out_len,source,in_len);
        EVP_EncryptFinal_ex(&ctx,target+out_len,&final_out_len);
        EVP_CIPHER_CTX_cleanup(&ctx);
        return out_len+final_out_len; // need to sum these together, because both
                                      // encrypt calls wrote data
    }
    

    And to decrypt:

    int decrypt(unsigned char *key, 
            unsigned char *iv, 
            unsigned char * source, 
            unsigned char* target, 
            int in_len)
    {
    
        int out_len=0,final_out_len=0;
        EVP_CIPHER_CTX ctx;
        EVP_CIPHER_CTX_init(&ctx);
        EVP_DecryptInit_ex(&ctx,EVP_aes_128_cbc(),NULL,key,iv);
        EVP_DecryptUpdate(&ctx,target,&out_len,source,in_len);
        EVP_DecryptFinal_ex(&ctx,target+out_len,&final_out_len);
        EVP_CIPHER_CTX_cleanup(&ctx);
        //Just to be nice, we'll add a zero at the end of the decrypted string
        target[out_len+final_out_len] = 0;
        return out_len+final_out_len;
    }
    

    Pulling it all together (in a loop, to prove your concept):

    int _tmain(int argc, _TCHAR* argv[])
    {
        unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        unsigned char ivec[] = {1,2,3,4,5,6,7,8};
        char *raw_buffer = "This is a test string";
        int raw_count = strlen(raw_buffer);
        for (int i=0; i<5; i++){
            unsigned char *decrypted_buffer = new unsigned char[raw_count+64];
            unsigned char *encrypted_buffer = new unsigned char[raw_count+64];
            int final_len = encrypt(key,ivec,(unsigned char*)raw_buffer,(unsigned char*)encrypted_buffer,raw_count);
            int dec_len = decrypt(key,ivec,(unsigned char*)encrypted_buffer,(unsigned char*)decrypted_buffer,final_len);
            printf("raw_count: %i\nfinal_len: %i\ndec_len: %i\n",raw_count,final_len,dec_len);
            printf("Original str: \n%s\n",raw_buffer);
            printf("Encrypted: \n%s\n", encrypted_buffer);
            printf("Decrypted:\n%s\n\n\n", decrypted_buffer);
            delete[] decrypted_buffer;
            delete[] encrypted_buffer;
        }
        char c;
        c=getchar();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.