Recently I have been using the integrated AES API (aes.h) and I am having the following issue:
1) I encrypt a simple, fixed length string and write it to a file.
2) When I read the file, decrypt the string and write the decrypted plain text to another file, I have an empty space at the location where I should have a digit - most of the time the '0' digit is abscent.
Here is a sample output:
Code: Select all
time=2112-01-01%2000:00:18&000,000,000,000,249,030,249,113,000,000,053,254,000,000,000,000?php
time=2112- 1-01%2000:00:19&000,000,000,000,249,028,249,113,000,000,053,254,000,000,000,000?php
time=2112-01-01%2000:00:19&000,000,000,000,249,020,249,001,000,000,053,254,000,000,000,000?php
Code: Select all
BOOL aesEncryptString(char *input, char *output, int aes_length, unsigned int stringlen)
{
//Will be private members of the crypto class
const char *aes_key = "The Decode Key..";
aes_context ctx;
unsigned char encodedresult[16] = { 0x00 };
unsigned int aes_length_bytes = aes_length / 8;
if ( strlen(aes_key) != aes_length_bytes)
{
iprintf("Invalid key length!\r\n");
return FALSE;
}
// Create the key
aes_set_key( &ctx, (unsigned char *)aes_key, aes_length);
// Encrypt
unsigned int len = stringlen;
unsigned int chunks = len / aes_length_bytes;
if (( len % aes_length_bytes) != 0 )
{
chunks++;
}
for ( unsigned int i = 0; i < chunks; i++ )
{
aes_encrypt( &ctx, ((unsigned char *)input) + (i * aes_length_bytes), encodedresult);
memcpy(output + (i * aes_length_bytes), encodedresult, 16);
}
return TRUE;
}
BOOL aesDecryptString(char *input, char *output, int aes_length, unsigned int stringlen)
{
const char *aes_key = "The Decode Key..";
aes_context ctx;
unsigned char decodedresult[16] = { 0x00 };
unsigned int aes_length_bytes = aes_length / 8;
if ( strlen(aes_key) != aes_length_bytes )
{
iprintf("Invalid key length!\r\n");
return FALSE;
}
//Create the key
aes_set_key( &ctx, (unsigned char *)aes_key, aes_length);
//Decrypt
unsigned int len = stringlen;
unsigned int chunks = len / aes_length_bytes;
if (( len % aes_length_bytes) != 0 )
{
chunks++;
}
for ( unsigned int i = 0; i < chunks; i++ )
{
aes_decrypt( &ctx, ((unsigned char *)input) + (i * aes_length_bytes), decodedresult);
strncat(output, (const char *)decodedresult, sizeof(decodedresult));
}
return TRUE;
}
Thanks,
Varban