Hy. I have to make a program to crypt a text entered from the keyboard whith aes 256 , but i have problems when i try to split the text in two.I get a bad_ptr when i declare
unsigned char** intext=new unsigned char* [16];

#include<iostream>
#include <fstream>
#include<conio.h>
#include <string>
#include "aes.h"
using namespace std;

int main(int argc, char **argv)
{
unsigned char key32[32];
unsigned char iv[16];
unsigned char* inbuf=new unsigned char[128];
unsigned char** intext=new unsigned char* [16];
unsigned char outbuf[128];


//memset(inbuf, 0, sizeof(inbuf));
cout<<"enter text:";
cin>>inbuf;
int lenght=strlen(reinterpret_cast<char*>(inbuf));

ifstream keyfile("file.key");
if(keyfile.is_open())
	{
		keyfile>>key32;
		cout<<key32<<endl;
	}
ifstream ivfile("file.iv");
if(ivfile.is_open())
	{
		ivfile>>iv;
		cout<<iv<<endl;
	}

ofstream criptfile("file.out");
memset(outbuf, 0, sizeof(outbuf));
AES_KEY aeskey;
AES_set_encrypt_key(key32, 32*8, &aeskey);
const int n=lenght/16+1;
cout<<lenght<<endl;
cout<<n<<endl;
int rest=lenght%16;
cout<<rest;



int l=0;
int m=0;
for(int j=0;j<lenght;j++)
{
	intext[l][m]=inbuf[j];
	m++;
	if(m==16)
	{
		m=0;
		l++;
	}
}

AES_cbc_encrypt(inbuf, outbuf, 16, &aeskey, iv, AES_ENCRYPT);
	if(criptfile.is_open())
	{
		criptfile<<outbuf;
		cout<<outbuf;
	}

getch();
}

Edited 6 Years Ago by flaviusilaghi: n/a

Define "bad pointer".

This line is fine and will give you a perfectly good pointer to an array of 16 pointers to characters.

unsigned char** intext=new unsigned char* [16];

Note that it allocates absolutely no characters.

So unless you something else between the line above and this line:

intext[l][m]=inbuf[j];

expect to have problems because you are trying to dereference a pointer to an actual character. If you are looking to make intext a 2-dimensional array of characters, you need to allocate characters:

const int MAX_STRING_LENGTH = 10;
const int NUM_STRINGS = 21;

char** array = new char*[NUM_STRINGS];
for (int i = 0; i < NUM_STRINGS; i++)
{
    array[i] = new char[MAX_STRING_LENGTH + 1]; // add 1 for null term
}

If you are using AES, you probably have no use for strings, but the point is the same. A pointer is a pointer and an array is array and a pointer can point to an array and be dereferenced, but if you try to dereference a pointer to an array element and it doesn't point to an array element, it won't work.

the problem was that is didn't allocate memory to each row. I am trying to break a long text into chunks of 128 bit. That is were i have problems.

the problem was that is didn't allocate memory to each row. I am trying to break a long text into chunks of 128 bit. That is were i have problems.

Right, so allocate the memory here:

const int CHUNK_SIZE = 128;
int numChunks = 16;
 
unsigned char** intext = new unsigned char*[numChunks];
for (int i = 0; i < numChunks; i++)
{
    intext[i] = new unsigned char[CHUNK_SIZE];
}

Edit : I guess if the chunk size is 128 BITS, then you don't have a chunk size of 128, you have a chunk size of 128 / 8 = 16, assuming that an unsigned char is 1 byte and there are 8 bits in a byte (can't always assume this). But the concept of allocating a 2-D array is the same.

Edited 6 Years Ago by VernonDozier: n/a

This question has already been answered. Start a new discussion instead.