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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:27:40+00:00 2026-05-25T14:27:40+00:00

I have the following code: [test.cpp] #include <mcrypt.h> #include <string> #include <iostream> #include <vector>

  • 0

I have the following code:

[test.cpp]

#include <mcrypt.h>
#include <string>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

#include <string>
using namespace std;
int main()
{
  char algo[] = "rijndael-256";
  char mode[] = "cbc";
   char *block_buffer=(char*)"HELLO!! MY NAME IS: ";
cout<<"here"<<endl;
string s;
char key="1234-5678-9654-7512-7895-2543-12";


  char iv[]  = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};;

  MCRYPT td = mcrypt_module_open(algo, NULL, mode, NULL);
  if (td == MCRYPT_FAILED) { cout<<"error"<<endl;}

int keysize=32;
  int r =  mcrypt_generic_init(td, key, keysize, iv);
if (r<0)
{
    cout<<"error2"<<endl;
    mcrypt_perror(r);
            return 1;

}

 //while ( fread (&block_buffer, 1, 1, stdin) == 1 ) {
   int j=          mcrypt_generic (td, &block_buffer, sizeof(block_buffer));
if (j!=0){std::cout<<"error encrypting"<<std::endl;} // I HAVE ERROR HERE J==0


 //how to print the encrypted string??

 cout<<"buffer "<<block_buffer<<endl; //this is not the encriperd string. why?

    mcrypt_generic_deinit(td);

  mcrypt_module_close(td);
}

I am testing the code:

$: g++ test.cpp -o tst -lmcrypt  
                      $: ./tst

WHERE SHOULT I ADD THE PKCS 7?

I have the following method:

std::string add_pkcs7_padding(std::string s, std::size_t n)
{
  const std::size_t fill = n - (s.length() % n);
  s.append(fill, static_cast<char>(fill));
  return s;
}

std::string strip_pkcs7_padding(std::string s, std::size_t n)
{
  const std::size_t pad = static_cast<unsigned char>(*s.rbegin());
  return s.substr(0, s.length() - pad);
}

I din’t know when should i run it and where in my code.

NEED SOME HELP. APPRECIATE A LOT!!

EDIT:

I have error at: mcrypt_generic (td, &block_buffer, sizeof(block_buffer)); The compiler prints
that the value j=0;

  • 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-05-25T14:27:41+00:00Added an answer on May 25, 2026 at 2:27 pm

    You should invoke mcrypt_generic() with a char*, not a char** as you do:

    mcrypt_generic(td, block_buffer, sizeof(block_buffer));
                       ^^^           ^^^^^^^^^^^^^^^^^^^^
                                          ouch!
    

    Also, the length is wrong, as sizeof(block_buffer) is just be the size of the pointer, not the string; if anything you need strlen(block_buffer).

    But this is still going to be wrong in general because you need your message to be a multiple of the block size. Use the padding function:

    std::string s = add_pkcs7_padding(block_buffer, mcrypt_enc_get_block_size(td));
    std::vector<char> v(s.begin(), s.end());  // this will hold the encrypted data
    mcrypt_generic(td, v.data(), v.size());
    

    By the way, your plaintext should be declared like this:

    const char * block_buffer = "HELLO!! MY NAME IS: ";
    ^^^^^                       ^^^^
      constness!                  no explicit cast!
    

    But why so clumsy, it’s better to just use a string:

    std::string plaintext = "HELLO!! MY NAME IS: ";
    

    I think you might benefit from picking up a good C++ book and familiarizing yourself with the basics of the language a bit – it’s good to have a project to work on, but most of your problems aren’t really related to encryption or mcrypt, but just general C++ programming.

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

Sidebar

Related Questions

I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>
The following code will cause compile errors in g++4.4: // File test.cpp namespace A
I have the following code: [test.h] class MyClass { public: string Name; MyClass(); void
I have the following code: public class Test { public static void Main() {
Very simply put, I have the following code snippet: FILE* test = fopen(C:\\core.u, w);
I have the following code snippet. $items['A'] = Test; $items['B'] = Test; $items['C'] =
I have the following code <html> <head> <title>Test</title> <style type=text/css> <!-- body,td,th { color:
I have a unit test which contains the following line of code Site.objects.get(name=UnitTest).delete() and
When I try and compile the following code... #include <vector> template <class T> void
Following code fails with a error message : t.cpp: In function `void test()': t.cpp:35:

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.