-1
    static char* test_encrypt_ecb_verbose(char* plain_text_char, char* key_char)
{
uint8_t i,j, buf[64];
uint8_t plain_text[64];
uint8_t* outstr;
outstr = '\0';
memcpy(key,key_char,16) ;
memcpy(plain_text, plain_text_char, 64);
memset(buf, 0, 64);

printf("ECB encrypt verbose:\n\n");
printf("plain text:\n");
for(i = (uint8_t) 0; i < (uint8_t) 4; ++i)
{
    phex(plain_text + i * (uint8_t) 16);
}
printf("\n");

printf("key:\n");
phex(key);
printf("\n");

// print the resulting cipher as 4 x 16 byte strings
printf("ciphertext:\n");
for(i = 0; i < 4; ++i)
{
    AES128_ECB_encrypt(plain_text + (i*16), key, buf+(i*16));
    phex(buf + (i*16));
//function to encrypt
}
printf("decryptedtext:\n");
for (i = 0; i < 4; ++i)
{
    AES128_ECB_decrypt(buf + (i * 16), key, plain_text + (i * 16));
    phex(plain_text + (i * 16));
//function to decrypt
}
//memcpy(outstr, buf, 64);

for (i = 0; i < 4; i++)
{
    for (j = 0; j < 16; j++)
    {
        outstr[j] = buf + (i * 16);
    }
}

In the above code snippet I want to return the output array after encryption as string . Two of my attempts are there at the end. But those aren't correct. Can anyone suggest the correct way?

2
  • Do not use ECB mode, it is not secure, see ECB mode, scroll down to the Penguin. Instead use CBC mode with a random IV, just prefix the encrypted data with the IV for use in decryption, it does not need to be secret. Commented May 12, 2017 at 9:29
  • ok. yeah , point to be noted. I will see to it next time Commented May 18, 2017 at 5:26

2 Answers 2

0

a char array and a char pointer is not the same thing.

If you need more details you should refer to this post

and it will gives you a solution to get a char * from a char array

char* p = &a[0];

a is your char array and p your destination pointer then return your pointer. Using your code you can also directly use the char * you get as function parameters

Sign up to request clarification or add additional context in comments.

Comments

0

To get it back you should add an additional argument:

static void test_encrypt_ecb_verbose(char* plain_text_char, char* key_char, char** cipher_text_char)
{
    ... your function ...

    *cipher_text_char = malloc(64);
    memcpy(*cipher_text_char, buf, 64);
}

From the caller you just do

char* cipher_text_char = NULL;

test_encrypt_ecb_verbose(plain_text_char, key_char, &cipher_text_char);

After test_encrypt_ecb_verbose has been executed, cipher_text_char will point to the memory allocated inside the function.

As an example consider this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* myfunc(char* src, char** dst, int len)
{
    *dst = (char*)malloc(len);

    memcpy(*dst, src, len);

    return *dst;
}

int main(int argc, char* argv[])
{
    char* src = "MyFuncTest";
    char* dst = NULL;
    char* p = NULL;

    p = myfunc(src, &dst, strlen(src) + 1);

    printf("dst = %s\n", dst);
    printf("p = %s\n", p);

    return 0;
}

The output is:
dst = MyFuncTest
p = MyFuncTest

9 Comments

and how to write return statement with *cipher_text_char ?
should "return *cipher_text_char; " be added as return statement in the definition?
it is not priniting the thing required. what could be changed?
What do you mean for "the thing required"? You should get the same result of your last printf. Isn't it? How have you changed your code?
Is there some way I can show you the complete code? then I can tell you a little better. thanks for such prompt replies.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.