#Openssl - getting IV and key from user

7 messages · Page 1 of 1 (latest)

willow kayak
#

hey, im trying to write a program where a user previously gets IV and key from encrypting a file and when the user wants to decrypt the file he needs to put in the IV and key to encrypt it...
im having trouble on how to get the iv and key from the user using scanf because for example the IV is supposed to be 16 Bytes (unsigned char) am im trying to get it while using scanf("%32s", temp_buf) but I have to keep in mind the \0 and the spaces between Bytes and now i kinda dont know what to do

IV looks like this

d5 93 4a 67 c2 16 e4 01-18 db 01 45 2e 8d 76 84

the part of program for getting the IV & key

else if (volba == 2)
        {
            unsigned char used_encrypt_decrypt_key[32];
            unsigned char used_IV_initialization_vector[16];
            unsigned char temp_iv[33];
            unsigned char temp_key[65]; 

            printf("\nvas IV?\n");                 // (64s pro string a ze se nacte max 64 znaku vcetne \0)
            
            scanf("%32s", temp_iv);
            hex_to_bin(temp_iv, used_IV_initialization_vector, 32);
            printf("vas encryption&decryption key?");

            scanf("%65s", temp_key);
            hex_to_bin(temp_key, used_encrypt_decrypt_key, 64);

            decipheredtext_len = decrypt(p_to_ciphertext, ciphertext_len, used_encrypt_decrypt_key, used_IV_initialization_vector, p_to_decipheretext);
            if (decipheredtext_len < 0)
            {
                printf("nejspise se stala chyba pri desifrovani souboru");
                return -1;
            }
            p_to_decipheretext[decipheredtext_len] = '\0';
            printf("%s\n", p_to_decipheretext);
            return 0;
        }

converting hex to binary to feed it into decrypt function

void hex_to_bin(const char *hex_string, unsigned char *bin_buffer, size_t hex_string_size)
{
    for (size_t index = 0; index < hex_string_size; index++)
    {
        sscanf(hex_string + 2 * index, "%2hhx", &bin_buffer[index]);
    }
}
fallen cedarBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

willow kayak
#

how i should get the information because I still keep getting an error "wrong final block lenght"
thank you

fallen cedarBOT
#

@willow kayak Has your question been resolved? If so, type !solved :)

fathom verge
vital eagle
fallen cedarBOT
# vital eagle !homework

@fathom verge

Luis
We Don't Do Your Homework

Welcome to Together C & C++ :wave:

We won't do your homework for you (#rules) but we are happy to help you learn and point you in the right direction.

Please send what you have so far and ask a specific question.