-2

I have a small problem when inserting the key in a c + + project in visual studio 2010 proffessional.

When I put the key only accepts the first two keys and this can be a problem when you put a similar key that begins with the first two characters.

However when I put the key directly in hexadecimal characters validates all.

I make it clear in advance know very little I am learning c + + This is what I have done for now.

    //****************** AES decryption ********************
const int size = 32;
unsigned char aesKey[size];
char* p;

for (int i = 1; i < argc || i < size; ++i)
{
    aesKey[i] = (unsigned char)strtol(argv[2], &p, 16);
} 

unsigned char *buf;

aes256_context ctx;
aes256_init(&ctx, aesKey);

for (unsigned long i = 0; i < lSize/16; i++) {
    buf = text + (i * 16);
    aes256_decrypt_ecb(&ctx, buf);
}

aes256_done(&ctx);
//******************************************************

where I have the argument argv[2] is because that I have to use the argument 2

Any suggestions or ideas, thanks

aleksander haugas
  • 93
  • 1
  • 2
  • 10

1 Answers1

1

This code can have many fixes, but this is the basic I can see

//****************** AES decryption ********************
const int size = 32;
unsigned char aesKey[size];
char* p;

//check you have argv[2]
if (argc < 3)
{
    //TODO: return or handle the error as you wish...
}

//i need to start from 0 (it's a zero base index)
//argc = argument count. and this should not be here
//you have 3 arguments and this is why it read 2 chars...
for (int i = 0;i < size; ++i)
{
    aesKey[i] = (unsigned char)strtol(argv[2], &p, 16);
} 

unsigned char *buf;

aes256_context ctx;
aes256_init(&ctx, aesKey);

//I don't know where lsize is coming from but I would calculate the division out side:
unsigned long myMax = lSize/16;
for (unsigned long i = 0; i < myMax; i++) {
    buf = text + (i * 16);
    aes256_decrypt_ecb(&ctx, buf);
}

aes256_done(&ctx);
//******************************************************
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94