Kapil Sharma
Kapil Sharma

Reputation: 10417

Convert uint8_t array (Hexadecimal) to string in C

Requirements

I'm a developer but with near-zero experience with C until a week ago. For a project, I need to encrypt/decrypt source-code/text files and need to use C for that.

What I did till now?

I'm using Kokke/tony-AES-c library for it and implementing it with pkcs7 padding as explained in this Gist.

In my code (given below), I have a function my_encrypt, which is encrypting contents and returning uint8_t array (which contains hex). I need to save it and read it later from a file for decryption. I know how to save a string into a file but how can I save uint8_t in a file.

For this, I need to convert uint8_t to string but after trying multiple solution, I am unable to do it.

Question

How can I convert uint8_t array (which contains hex) to string in C.

My code

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

#define CBC 1
#define DECRYPT 0;
#define ENCRYPT 1;

#include "aes.h"
#include "pkcs7_padding.h"

const uint8_t * my_encrypt(char *report) {
    //Initialization Vector
    uint8_t iv[]  = { 0x75, 0x52, 0x5f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x21, 0x21 };

    uint8_t i;
//    char* report = "my super secret thing that needs to remain that way!";
    char* key = "thisIstheKey";
    int dlen = strlen(report);
    int klen = strlen(key);

    printf("THE PLAIN TEXT STRING = ");
    for (i=0; i<dlen;i++){
        printf("%c", report[i]);
    }
    printf("\n");

    //Proper Length of report
    int dlenu = dlen;
    if (dlen % 16) {
        dlenu += 16 - (dlen % 16);
        printf("The original length of the STRING = %d and the length of the padded STRING = %d\n", dlen, dlenu);
    }

    //Proper length of key
    int klenu = klen;
    if (klen % 16) {
        klenu += 16 - (klen % 16);
        printf("The original length of the KEY = %d and the length of the padded KEY = %d\n", klen, klenu);
    }

    // Make the uint8_t arrays
    uint8_t hexarray[dlenu];
    uint8_t kexarray[klenu];

    // Initialize them with zeros
    memset( hexarray, 0, dlenu );
    memset( kexarray, 0, klenu );

    // Fill the uint8_t arrays
    for (int i=0;i<dlen;i++) {
        hexarray[i] = (uint8_t)report[i];
    }
    for (int i=0;i<klen;i++) {
        kexarray[i] = (uint8_t)key[i];
    }

    int reportPad = pkcs7_padding_pad_buffer( hexarray, dlen, sizeof(hexarray), 16 );
    int keyPad = pkcs7_padding_pad_buffer( kexarray, klen, sizeof(kexarray), 16 );

    printf("The padded STRING in hex is = ");
    for (i=0; i<dlenu;i++){
        printf("%02x",hexarray[i]);
    }
    printf("\n");

    printf("The padded key in hex is = ");
    for (i=0; i<klenu;i++){
        printf("%02x",kexarray[i]);
    }
    printf("\n");

    // In case you want to check if the padding is valid
    int valid = pkcs7_padding_valid( hexarray, dlen, sizeof(hexarray), 16 );
    int valid2 = pkcs7_padding_valid( kexarray, klen, sizeof(kexarray), 16 );
    printf("Is the pkcs7 padding valid  report = %d  |  key = %d\n", valid, valid2);

    //start the encryption
    struct AES_ctx ctx;
    AES_init_ctx_iv(&ctx, kexarray, iv);

    // encrypt
    AES_CBC_encrypt_buffer(&ctx, hexarray, dlenu);

    printf("the encrypted STRING = ");
    for (i=0; i<dlenu;i++){
        printf("%02x",hexarray[i]);
    }
    printf("\n");

    return hexarray;
}

int main(int argc, char *argv[]) {
    // File pointer
    FILE *input_file;

    int operation;

    char * input_file_buffer = 0;
    long input_file_length;

    // ////////////////////////////////////////////////////////
    // Read command line arguments
    // ////////////////////////////////////////////////////////

    // Make sure proper command params are supplied
    if (argc != 3) {
        printf("Usage: %s encrypt/decrypt filename\n", argv[0]);
        return -1;
    }

    // Define operation type
    if (strcmp(argv[1], "encrypt") == 0) {
        operation = ENCRYPT;
    } else if (strcmp(argv[1], "decrypt") == 0) {
        operation = DECRYPT;
    }

    // ////////////////////////////////////////////////////////
    // Open File contents
    // ////////////////////////////////////////////////////////

    // Open input_file to encrypt/decrypt.
    input_file = fopen(argv[2], "rb");
    if (!input_file) {
        fprintf(stderr, "ERROR: '%s' file fopen error: %s\n", argv[2], strerror(errno));
        return errno;
    }

    // Read contents of file in buffer
    fseek(input_file, 0, SEEK_END);
    input_file_length = ftell (input_file);
    fseek(input_file, 0, SEEK_SET);
    input_file_buffer = malloc (input_file_length);
    if (input_file_buffer)
    {
        fread(input_file_buffer, 1, input_file_length, input_file);
    }

    // Close input_file
    fclose(input_file);

    // We have contents of file in input_file_buffer, let's print them.
    printf("File contents:\n-------\n%s\n", input_file_buffer);

    // Let's encrypt input_file_buffer
    uint8_t result = 0;
    result = my_encrypt(input_file_buffer);

    // Convery result to string.
    printf("main: Encrypted File contents:\n-------\n%d\n", result);

    return 0;
}

Upvotes: 0

Views: 303

Answers (0)

Related Questions