Encrypted output is different in openssl command and openssl C Program - c

I wrote a simple openssl program to perform 'AES-128-CBC' encryption.
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#define KEY_SIZE 16
#define IV_SIZE 16
void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
void hexdump(const void* data, size_t size) {
char ascii[17];
size_t i, j;
ascii[16] = '\0';
for (i = 0; i < size; ++i) {
printf("%02X ", ((unsigned char*)data)[i]);
if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') {
ascii[i % 16] = ((unsigned char*)data)[i];
} else {
ascii[i % 16] = '.';
}
if ((i+1) % 8 == 0 || i+1 == size) {
printf(" ");
if ((i+1) % 16 == 0) {
printf("| %s \n", ascii);
} else if (i+1 == size) {
ascii[(i+1) % 16] = '\0';
if ((i+1) % 16 <= 8) {
printf(" ");
}
for (j = (i+1) % 16; j < 16; ++j) {
printf(" ");
}
printf("| %s \n", ascii);
}
}
}
}
int encrypt_file(void)
{
EVP_CIPHER_CTX *ctx;
char plain_text[12] = "Hello World";
size_t plaintext_len = 12;
unsigned char *ciphertext = NULL;
int ciphertext_maxlen = plaintext_len + EVP_CIPHER_block_size(EVP_aes_128_cbc())*2 - 1;
int len = 0, ciphertext_len = 0;
ciphertext = (unsigned char *)malloc(ciphertext_maxlen * sizeof(unsigned char));
unsigned char key[KEY_SIZE]= {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11};
unsigned char iv[IV_SIZE] = {0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22};
hexdump(key, 16);
hexdump(iv, 16);
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
handleErrors();
if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plain_text, plaintext_len))
handleErrors();
ciphertext_len = len;
if( EVP_EncryptFinal_ex(ctx, ciphertext + len, &len) != 1 )
handleErrors();
ciphertext_len += len;
printf("ciphertext len:%d\n", ciphertext_len);
EVP_CIPHER_CTX_free(ctx);
hexdump(ciphertext, ciphertext_len);
free(ciphertext);
return 0;
}
int main(int argc, char *argv[])
{
encrypt_file();
return 0;
}
When i am running this program it's generating cipher text.
11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 | ................
22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 | """"""""""""""""
ciphertext len:16
4F 55 95 1F D9 A6 34 F1 2F 90 06 2B 82 F8 C2 19 | OU....4./..+....
I am trying to verify whether my C code is working fine or not by passing the same parameters to openssl commands
jamal#jamal-VirtualBox:~$ echo "Hello World" > plaintext.txt
jamal#jamal-VirtualBox:~$ cat plaintext.txt
Hello World
jamal#jamal-VirtualBox:~$ openssl enc -aes-128-cbc -in plaintext.txt -K '11111111111111111111111111111111' -iv '22222222222222222222222222222222' -out openssl_enc.txt -p
salt=F3753875307F0000
key=11111111111111111111111111111111
iv =22222222222222222222222222222222
jamal#jamal-VirtualBox:~$ hexdump openssl_enc.txt
0000000 ea60 4e8e a289 ea50 ee19 0edc 81ea 557d
0000010
Why is the cipher bytes generated by C code not same as the one generated by openssl command. Am i missing anything in Code?

Related

Convert String to array of char in C [duplicate]

Is there any standard C function that converts from hexadecimal string to byte array?
I do not want to write my own function.
As far as I know, there's no standard function to do so, but it's simple to achieve in the following manner:
#include <stdio.h>
int main(int argc, char **argv) {
const char hexstring[] = "DEadbeef10203040b00b1e50", *pos = hexstring;
unsigned char val[12];
/* WARNING: no sanitization or error-checking whatsoever */
for (size_t count = 0; count < sizeof val/sizeof *val; count++) {
sscanf(pos, "%2hhx", &val[count]);
pos += 2;
}
printf("0x");
for(size_t count = 0; count < sizeof val/sizeof *val; count++)
printf("%02x", val[count]);
printf("\n");
return 0;
}
Edit
As Al pointed out, in case of an odd number of hex digits in the string, you have to make sure you prefix it with a starting 0. For example, the string "f00f5" will be evaluated as {0xf0, 0x0f, 0x05} erroneously by the above example, instead of the proper {0x0f, 0x00, 0xf5}.
Amended the example a little bit to address the comment from #MassimoCallegari
I found this question by Googling for the same thing. I don't like the idea of calling sscanf() or strtol() since it feels like overkill. I wrote a quick function which does not validate that the text is indeed the hexadecimal presentation of a byte stream, but will handle odd number of hex digits:
uint8_t tallymarker_hextobin(const char * str, uint8_t * bytes, size_t blen)
{
uint8_t pos;
uint8_t idx0;
uint8_t idx1;
// mapping of ASCII characters to hex values
const uint8_t hashmap[] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // !"#$%&'
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ()*+,-./
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 01234567
0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 89:;<=>?
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // #ABCDEFG
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // HIJKLMNO
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // PQRSTUVW
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // XYZ[\]^_
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // `abcdefg
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // hijklmno
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pqrstuvw
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // xyz{|}~.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ........
};
bzero(bytes, blen);
for (pos = 0; ((pos < (blen*2)) && (pos < strlen(str))); pos += 2)
{
idx0 = (uint8_t)str[pos+0];
idx1 = (uint8_t)str[pos+1];
bytes[pos/2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1];
};
return(0);
}
Apart from the excellent answers above I though I would write a C function that does not use any libraries and has some guards against bad strings.
uint8_t* datahex(char* string) {
if(string == NULL)
return NULL;
size_t slength = strlen(string);
if((slength % 2) != 0) // must be even
return NULL;
size_t dlength = slength / 2;
uint8_t* data = malloc(dlength);
memset(data, 0, dlength);
size_t index = 0;
while (index < slength) {
char c = string[index];
int value = 0;
if(c >= '0' && c <= '9')
value = (c - '0');
else if (c >= 'A' && c <= 'F')
value = (10 + (c - 'A'));
else if (c >= 'a' && c <= 'f')
value = (10 + (c - 'a'));
else {
free(data);
return NULL;
}
data[(index/2)] += value << (((index + 1) % 2) * 4);
index++;
}
return data;
}
Explanation:
a. index / 2 | Division between integers will round down the value, so 0/2 = 0, 1/2 = 0, 2/2 = 1, 3/2 = 1, 4/2 = 2, 5/2 = 2, etc. So, for every 2 string characters we add the value to 1 data byte.
b. (index + 1) % 2 | We want odd numbers to result to 1 and even to 0 since the first digit of a hex string is the most significant and needs to be multiplied by 16. so for index 0 => 0 + 1 % 2 = 1, index 1 => 1 + 1 % 2 = 0 etc.
c. << 4 | Shift by 4 is multiplying by 16. example: b00000001 << 4 = b00010000
For short strings, strtol, strtoll, and strtoimax will work just fine (note that the third argument is the base to use in processing the string...set it to 16). If your input is longer than number-of-bits-in-the-longest-integer-type/4 then you'll need one of the more flexible methods suggested by other answers.
By some modification form user411313's code, following works for me:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int main ()
{
char *hexstring = "deadbeef10203040b00b1e50";
int i;
unsigned int bytearray[12];
uint8_t str_len = strlen(hexstring);
for (i = 0; i < (str_len / 2); i++) {
sscanf(hexstring + 2*i, "%02x", &bytearray[i]);
printf("bytearray %d: %02x\n", i, bytearray[i]);
}
return 0;
}
A fleshed out version of Michael Foukarakis post (since I don't have the "reputation" to add a comment to that post yet):
#include <stdio.h>
#include <string.h>
void print(unsigned char *byte_array, int byte_array_size)
{
int i = 0;
printf("0x");
for(; i < byte_array_size; i++)
{
printf("%02x", byte_array[i]);
}
printf("\n");
}
int convert(const char *hex_str, unsigned char *byte_array, int byte_array_max)
{
int hex_str_len = strlen(hex_str);
int i = 0, j = 0;
// The output array size is half the hex_str length (rounded up)
int byte_array_size = (hex_str_len+1)/2;
if (byte_array_size > byte_array_max)
{
// Too big for the output array
return -1;
}
if (hex_str_len % 2 == 1)
{
// hex_str is an odd length, so assume an implicit "0" prefix
if (sscanf(&(hex_str[0]), "%1hhx", &(byte_array[0])) != 1)
{
return -1;
}
i = j = 1;
}
for (; i < hex_str_len; i+=2, j++)
{
if (sscanf(&(hex_str[i]), "%2hhx", &(byte_array[j])) != 1)
{
return -1;
}
}
return byte_array_size;
}
void main()
{
char *examples[] = { "", "5", "D", "5D", "5Df", "deadbeef10203040b00b1e50", "02invalid55" };
unsigned char byte_array[128];
int i = 0;
for (; i < sizeof(examples)/sizeof(char *); i++)
{
int size = convert(examples[i], byte_array, 128);
if (size < 0)
{
printf("Failed to convert '%s'\n", examples[i]);
}
else if (size == 0)
{
printf("Nothing to convert for '%s'\n", examples[i]);
}
else
{
print(byte_array, size);
}
}
}
Here is HexToBin and BinToHex relatively clean and readable.
(Note originally there were returned enum error codes through an error logging system not a simple -1 or -2.)
typedef unsigned char ByteData;
ByteData HexChar (char c)
{
if ('0' <= c && c <= '9') return (ByteData)(c - '0');
if ('A' <= c && c <= 'F') return (ByteData)(c - 'A' + 10);
if ('a' <= c && c <= 'f') return (ByteData)(c - 'a' + 10);
return (ByteData)(-1);
}
ssize_t HexToBin (const char* s, ByteData * buff, ssize_t length)
{
ssize_t result = 0;
if (!s || !buff || length <= 0) return -2;
while (*s)
{
ByteData nib1 = HexChar(*s++);
if ((signed)nib1 < 0) return -3;
ByteData nib2 = HexChar(*s++);
if ((signed)nib2 < 0) return -4;
ByteData bin = (nib1 << 4) + nib2;
if (length-- <= 0) return -5;
*buff++ = bin;
++result;
}
return result;
}
void BinToHex (const ByteData * buff, ssize_t length, char * output, ssize_t outLength)
{
char binHex[] = "0123456789ABCDEF";
if (!output || outLength < 4) return (void)(-6);
*output = '\0';
if (!buff || length <= 0 || outLength <= 2 * length)
{
memcpy(output, "ERR", 4);
return (void)(-7);
}
for (; length > 0; --length, outLength -= 2)
{
ByteData byte = *buff++;
*output++ = binHex[(byte >> 4) & 0x0F];
*output++ = binHex[byte & 0x0F];
}
if (outLength-- <= 0) return (void)(-8);
*output++ = '\0';
}
Following is the solution I wrote up for performance reasons:
void hex2bin(const char* in, size_t len, unsigned char* out) {
static const unsigned char TBL[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 58, 59, 60, 61,
62, 63, 64, 10, 11, 12, 13, 14, 15, 71, 72, 73, 74, 75,
76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 10, 11, 12, 13, 14, 15
};
static const unsigned char *LOOKUP = TBL - 48;
const char* end = in + len;
while(in < end) *(out++) = LOOKUP[*(in++)] << 4 | LOOKUP[*(in++)];
}
Example:
unsigned char seckey[32];
hex2bin("351aaaec0070d13d350afae2bc43b68c7e590268889869dde489f2f7988f3fee", 64, seckey);
/*
seckey = {
53, 26, 170, 236, 0, 112, 209, 61, 53, 10, 250, 226, 188, 67, 182, 140,
126, 89, 2, 104, 136, 152, 105, 221, 228, 137, 242, 247, 152, 143, 63, 238
};
*/
If you don't need to support lowercase:
static const unsigned char TBL[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 58, 59,
60, 61, 62, 63, 64, 10, 11, 12, 13, 14, 15
};
you can use this function which
written with performance in mind (for embedded processors), no scanf, strtol or dynamic memory allocation
has guard against output buffer overflow and odd input str len
/// in: valid chars are 0-9 + A-F + a-f
/// out_len_max==0: convert until the end of input string, out_len_max>0 only convert this many numbers
/// returns actual out size
int hexStr2Arr(unsigned char* out, const char* in, size_t out_len_max = 0)
{
if (!out_len_max)
out_len_max = 2147483647; // INT_MAX
const int in_len = strnlen(in, out_len_max * 2);
if (in_len % 2 != 0)
return -1; // error, in str len should be even
// calc actual out len
const int out_len = out_len_max < (in_len / 2) ? out_len_max : (in_len / 2);
for (int i = 0; i < out_len; i++) {
char ch0 = in[2 * i];
char ch1 = in[2 * i + 1];
uint8_t nib0 = (ch0 & 0xF) + (ch0 >> 6) | ((ch0 >> 3) & 0x8);
uint8_t nib1 = (ch1 & 0xF) + (ch1 >> 6) | ((ch1 >> 3) & 0x8);
out[i] = (nib0 << 4) | nib1;
}
return out_len;
}
usage:
unsigned char result[128];
memset(result, 0, 128); // optional
printf("result len=%d\n", hexStr2Arr(result, "0a0B10")); // result = [0A 0B 10 00 00 ...]
memset(result, 0, 128); // optional
// only convert single number
printf("result len=%d\n", hexStr2Arr(result, "0a0B10", 1)); // result = [0A 00 00 00 00 ...]
hextools.h
#ifndef HEX_TOOLS_H
#define HEX_TOOLS_H
char *bin2hex(unsigned char*, int);
unsigned char *hex2bin(const char*);
#endif // HEX_TOOLS_H
hextools.c
#include <stdlib.h>
char *bin2hex(unsigned char *p, int len)
{
char *hex = malloc(((2*len) + 1));
char *r = hex;
while(len && p)
{
(*r) = ((*p) & 0xF0) >> 4;
(*r) = ((*r) <= 9 ? '0' + (*r) : 'A' - 10 + (*r));
r++;
(*r) = ((*p) & 0x0F);
(*r) = ((*r) <= 9 ? '0' + (*r) : 'A' - 10 + (*r));
r++;
p++;
len--;
}
*r = '\0';
return hex;
}
unsigned char *hex2bin(const char *str)
{
int len, h;
unsigned char *result, *err, *p, c;
err = malloc(1);
*err = 0;
if (!str)
return err;
if (!*str)
return err;
len = 0;
p = (unsigned char*) str;
while (*p++)
len++;
result = malloc((len/2)+1);
h = !(len%2) * 4;
p = result;
*p = 0;
c = *str;
while(c)
{
if(('0' <= c) && (c <= '9'))
*p += (c - '0') << h;
else if(('A' <= c) && (c <= 'F'))
*p += (c - 'A' + 10) << h;
else if(('a' <= c) && (c <= 'f'))
*p += (c - 'a' + 10) << h;
else
return err;
str++;
c = *str;
if (h)
h = 0;
else
{
h = 4;
p++;
*p = 0;
}
}
return result;
}
main.c
#include <stdio.h>
#include "hextools.h"
int main(void)
{
unsigned char s[] = { 0xa0, 0xf9, 0xc3, 0xde, 0x44 };
char *hex = bin2hex(s, sizeof s);
puts(hex);
unsigned char *bin;
bin = hex2bin(hex);
puts(bin2hex(bin, 5));
size_t k;
for(k=0; k<5; k++)
printf("%02X", bin[k]);
putchar('\n');
return 0;
}
char *hexstring = "deadbeef10203040b00b1e50", *pos = hexstring;
unsigned char val[12];
while( *pos )
{
if( !((pos-hexstring)&1) )
sscanf(pos,"%02x",&val[(pos-hexstring)>>1]);
++pos;
}
sizeof(val)/sizeof(val[0]) is redundant!
In main()
{
printf("enter string :\n");
fgets(buf, 200, stdin);
unsigned char str_len = strlen(buf);
k=0;
unsigned char bytearray[100];
for(j=0;j<str_len-1;j=j+2)
{ bytearray[k++]=converttohex(&buffer[j]);
printf(" %02X",bytearray[k-1]);
}
}
Use this
int converttohex(char * val)
{
unsigned char temp = toupper(*val);
unsigned char fin=0;
if(temp>64)
temp=10+(temp-65);
else
temp=temp-48;
fin=(temp<<4)&0xf0;
temp = toupper(*(val+1));
if(temp>64)
temp=10+(temp-65);
else
temp=temp-48;
fin=fin|(temp & 0x0f);
return fin;
}
This is a modified function from a similar question, modified as per the suggestion of https://stackoverflow.com/a/18267932/700597.
This function will convert a hexadecimal string - NOT prepended with "0x" - with an even number of characters to the number of bytes specified. It will return -1 if it encounters an invalid character, or if the hex string has an odd length, and 0 on success.
//convert hexstring to len bytes of data
//returns 0 on success, -1 on error
//data is a buffer of at least len bytes
//hexstring is upper or lower case hexadecimal, NOT prepended with "0x"
int hex2data(unsigned char *data, const unsigned char *hexstring, unsigned int len)
{
unsigned const char *pos = hexstring;
char *endptr;
size_t count = 0;
if ((hexstring[0] == '\0') || (strlen(hexstring) % 2)) {
//hexstring contains no data
//or hexstring has an odd length
return -1;
}
for(count = 0; count < len; count++) {
char buf[5] = {'0', 'x', pos[0], pos[1], 0};
data[count] = strtol(buf, &endptr, 0);
pos += 2 * sizeof(char);
if (endptr[0] != '\0') {
//non-hexadecimal character encountered
return -1;
}
}
return 0;
}
Two short routines to parse a byte or a word, using strchr().
// HexConverter.h
#ifndef HEXCONVERTER_H
#define HEXCONVERTER_H
unsigned int hexToByte (const char *hexString);
unsigned int hexToWord (const char *hexString);
#endif
// HexConverter.c
#include <string.h> // for strchr()
#include <ctype.h> // for toupper()
unsigned int hexToByte (const char *hexString)
{
unsigned int value;
const char *hexDigits = "0123456789ABCDEF";
value = 0;
if (hexString != NULL)
{
char *ptr;
ptr = strchr (hexDigits, toupper(hexString[0]));
if (ptr != NULL)
{
value = (ptr - hexDigits) << 4;
ptr = strchr (hexDigits, toupper(hexString[1]));
if (ptr != NULL)
{
value = value | (ptr - hexDigits);
}
}
}
return value;
}
unsigned int hexToWord (const char *hexString)
{
unsigned int value;
value = 0;
if (hexString != NULL)
{
value = (hexToByte (&hexString[0]) << 8) |
(hexToByte (&hexString[2]));
}
return value;
}
// HexConverterTest.c
#include <stdio.h>
#include "HexConverter.h"
int main (int argc, char **argv)
{
(void)argc; // not used
(void)argv; // not used
unsigned int value;
char *hexString;
hexString = "2a";
value = hexToByte (hexString);
printf ("%s == %x (%u)\n", hexString, value, value);
hexString = "1234";
value = hexToWord (hexString);
printf ("%s == %x (%u)\n", hexString, value, value);
hexString = "0102030405060708090a10ff";
printf ("Hex String: %s\n", hexString);
for (unsigned int idx = 0; idx < strlen(hexString); idx += 2)
{
value = hexToByte (&hexString[idx]);
printf ("%c%c == %x (%u)\n", hexString[idx], hexString[idx+1],
value, value);
}
return EXIT_SUCCESS;
}
Try the following code:
static unsigned char ascii2byte(char *val)
{
unsigned char temp = *val;
if(temp > 0x60) temp -= 39; // convert chars a-f
temp -= 48; // convert chars 0-9
temp *= 16;
temp += *(val+1);
if(*(val+1) > 0x60) temp -= 39; // convert chars a-f
temp -= 48; // convert chars 0-9
return temp;
}
Here's my version:
/* Convert a hex char digit to its integer value. */
int hexDigitToInt(char digit) {
digit = tolower(digit);
if ('0' <= digit && digit <= '9') //if it's decimal
return (int)(digit - '0');
else if ('a' <= digit && digit <= 'f') //if it's abcdef
return (int)(digit - ('a' - 10));
else
return -1; //value not in [0-9][a-f] range
}
/* Decode a hex string. */
char *decodeHexString(const char *hexStr) {
char* decoded = malloc(strlen(hexStr)/2+1);
char* hexStrPtr = (char *)hexStr;
char* decodedPtr = decoded;
while (*hexStrPtr != '\0') { /* Step through hexStr, two chars at a time. */
*decodedPtr = 16 * hexDigitToInt(*hexStrPtr) + hexDigitToInt(*(hexStrPtr+1));
hexStrPtr += 2;
decodedPtr++;
}
*decodedPtr = '\0'; /* final null char */
return decoded;
}
Could it be simpler?!
uint8_t hex(char ch) {
uint8_t r = (ch > 57) ? (ch - 55) : (ch - 48);
return r & 0x0F;
}
int to_byte_array(const char *in, size_t in_size, uint8_t *out) {
int count = 0;
if (in_size % 2) {
while (*in && out) {
*out = hex(*in++);
if (!*in)
return count;
*out = (*out << 4) | hex(*in++);
*out++;
count++;
}
return count;
} else {
while (*in && out) {
*out++ = (hex(*in++) << 4) | hex(*in++);
count++;
}
return count;
}
}
int main() {
char hex_in[] = "deadbeef10203040b00b1e50";
uint8_t out[32];
int res = to_byte_array(hex_in, sizeof(hex_in) - 1, out);
for (size_t i = 0; i < res; i++)
printf("%02x ", out[i]);
printf("\n");
system("pause");
return 0;
}
For newbies and to improve #Michael's answer here's a full version:
// out needs to be at least len/2+1 bytes long
// length will be returned
int hexStrToBin(char* out, const char *str)
{
int i, len = strlen(str);
for (i = 0; i < len; i++)
{
// Reads str & stores in op
sscanf(str, "%2hhx", &op[i]);
str += 2;
}
return len/2;
}
No. But it's relatively trivial to achieve using sscanf in a loop.
The best way I know:
int hex2bin_by_zibri(char *source_str, char *dest_buffer)
{
char *line = source_str;
char *data = line;
int offset;
int read_byte;
int data_len = 0;
while (sscanf(data, " %02x%n", &read_byte, &offset) == 1) {
dest_buffer[data_len++] = read_byte;
data += offset;
}
return data_len;
}
The function returns the number of converted bytes saved in dest_buffer.
The input string can contain spaces and mixed case letters.
"01 02 03 04 ab Cd eF garbage AB"
translates to dest_buffer containing
01 02 03 04 ab cd ef
and also
"01020304abCdeFgarbageAB"
translates as before.
Parsing stops at the first "error".
Here is a solution to deal with files, which may be used more frequently...
int convert(char *infile, char *outfile) {
char *source = NULL;
FILE *fp = fopen(infile, "r");
long bufsize;
if (fp != NULL) {
/* Go to the end of the file. */
if (fseek(fp, 0L, SEEK_END) == 0) {
/* Get the size of the file. */
bufsize = ftell(fp);
if (bufsize == -1) { /* Error */ }
/* Allocate our buffer to that size. */
source = malloc(sizeof(char) * (bufsize + 1));
/* Go back to the start of the file. */
if (fseek(fp, 0L, SEEK_SET) != 0) { /* Error */ }
/* Read the entire file into memory. */
size_t newLen = fread(source, sizeof(char), bufsize, fp);
if ( ferror( fp ) != 0 ) {
fputs("Error reading file", stderr);
} else {
source[newLen++] = '\0'; /* Just to be safe. */
}
}
fclose(fp);
}
int sourceLen = bufsize - 1;
int destLen = sourceLen/2;
unsigned char* dest = malloc(destLen);
short i;
unsigned char highByte, lowByte;
for (i = 0; i < sourceLen; i += 2)
{
highByte = toupper(source[i]);
lowByte = toupper(source[i + 1]);
if (highByte > 0x39)
highByte -= 0x37;
else
highByte -= 0x30;
if (lowByte > 0x39)
lowByte -= 0x37;
else
lowByte -= 0x30;
dest[i / 2] = (highByte << 4) | lowByte;
}
FILE *fop = fopen(outfile, "w");
if (fop == NULL) return 1;
fwrite(dest, 1, destLen, fop);
fclose(fop);
free(source);
free(dest);
return 0;
}

I can't understand how to convert hex string to unsigned char in C?

I have input
unsigned char hex[64] = 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a0"
and I want output like this:
unsigned char tmpInHash[32] = { 0x9f, 0x86, 0xd0, 0x81, 0x88, 0x4c, 0x7d, 0x65, 0x9a, 0x2f, 0xea, 0xa0, 0xc5, 0x5a, 0xd0, 0x15, 0xa3, 0xbf, 0x4f, 0x1b, 0x2b, 0x0b, 0x82, 0x2c, 0xd1, 0x5d, 0x6c, 0x15, 0xb0, 0xf0, 0x0a, 0x08 }
I searched for the answer everywhere, but the answers , which I found, do not fit.
EDIT
I want that when I write:
for(i=0; i< strlen(tmpInHash); i++ {
printf("%c ", tmpInHash);
}
I get this:
0x9f 0x86 0xd0 0x81 0x88 0x4c 0x7d 0x65 0x9a ...
Is it possible?
One way to convert this is the following way:
Iterate through every item of the string, where every 2 characters represent a number.
Break out each such character pair and store them in a temporary string: char tmp {str[i], str[i+1], '\0'};.
Call strtol(tmp, NULL, 16) on this string to get the integer number.
Without "high level" functions
#include <stdio.h>
#include <string.h>
int main(void)
{
unsigned char hex[] = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
size_t stringLength = (sizeof(hex)/sizeof(hex[0]))-1;
unsigned char tmpInHash[stringLength/2];
int j=0;
// reset the char to 0. This grants that next or operation works on reset buffer
tmpInHash[0] = 0;
// loop that parse the whole string
for (size_t i = 0; i < stringLength; i++)
{
// check if the char is a to f
if ((hex[i] >= 'a') && (hex[i] <= 'f'))
{
tmpInHash[j] |= hex[i] -'a' + 10;
}
// che if is a digit
else if ((hex[i] >= '0') && (hex[i] <= '9'))
{
tmpInHash[j] |= hex[i] -'0';
}
// character not allowed
else
{
fprintf(stderr, "Character not allowed: %c position: %zu\n", hex[i], i);
return 1;
}
// even index chars are hig 4 bits of unsigned char
if ((i%2) == 0)
{
tmpInHash[j]<<=4;
}
else
{
// nex unsigned char
j++;
// reset the char to 0. This grants that next or operation works on reset buffer
if (j < stringLength/2)
tmpInHash[j] = 0;
}
}
for (size_t i = 0; i < stringLength/2; i++)
{
printf("0x%02X ", tmpInHash[i]);
}
printf("\n");
return 0;
}
OUTPUT
0x9F 0x86 0xD0 0x81 0x88 0x4C 0x7D 0x65 0x9A 0x2F 0xEA 0xA0 0xC5 0x5A 0xD0 0x15 0xA3 0xBF 0x4F 0x1B 0x2B 0x0B 0x82 0x2C 0xD1 0x5D 0x6C 0x15 0xB0 0xF0 0x0A 0x08
EDIT
Another example can be
#include <stdio.h>
#include <string.h>
unsigned char hex[] = "9f86d081884g7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
#define HEX_LEN (sizeof(hex)-1)
unsigned char tmpInHash[HEX_LEN/2]={0};
int main(void)
{
size_t i = 0;
size_t j = 0;
// parse all charcaters of hex
while(hex[i] != '\0')
{
// check if the char is a to f
if ((hex[i] >= 'a') && (hex[i] <= 'f'))
{
tmpInHash[j] |= hex[i] -'a' + 10;
}
// che if is a digit
else if ((hex[i] >= '0') && (hex[i] <= '9'))
{
tmpInHash[j] |= hex[i] -'0';
}
// character not allowed
else
{
fprintf(stderr, "Character not allowed: %c position: %zu\n", hex[i], i);
return 1;
}
// even index chars are hig 4 bits of unsigned char
if ((i%2) == 0)
{
tmpInHash[j]<<=4;
}
else
{
// nex unsigned char
j++;
}
// next hex char
i++;
}
// print loop
for (i = 0; i < (HEX_LEN/2); i++)
{
printf("0x%02X ", tmpInHash[i]);
}
printf("\n");
return 0;
}
unsigned char hex[64] = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a0";
unsigned char *ptrHex =hex;
unsigned char tmpInHash[32];
int i ,tmp ;
for(i=0 ; i < 32 ; i++)
{
if((*ptrHex)<=9 && (*ptrHex) >=0)
tmp=(*ptrHex)-'0';
else tmp=(*ptrHex)-'a'+10;
tmpInHash[i]=(tmp<<4);
ptrHex++;
if((*ptrHex)<=9 && (*ptrHex) >=0)
tmp=(*ptrHex)-'0';
else tmp=(*ptrHex)-'a'+10;
tmpInHash[i]|=tmp ;
ptrHex++;
}

Reading char array containing Hex values in C

This is part of a program that I use to decrypt AES in C. The program is supposed to take in an IV from an external file, read it then set as the IV. Unfortunately the IV is declared as a char array in the code (which I cannot change) so what I tried to do is to read the external file as byte[], then convert it to char[].
One of the issues I am currently facing is regarding the AES_IV variable as seen below:
#define AES_BLOCK_SIZE 0x10
char AES_IV[AES_BLOCK_SIZE] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
When I try to print out the AES_IV using the code
printf("\nAES_IV: ");
for (i3 = 0; i3 < AES_BLOCK_SIZE; i3++) {
printf("%02x", AES_IV[i3]);
}
printf("\n");
The output is:
AES_IV: 0c0a513398f1eda13ed2fc7213b50b10
Which is different from the AES_IV I declared.
I changed the for loop to
for (i3 = 0; i3 < 50; i3++) {
and the resultant output is much longer
AES_IV: 0c0a513398f1eda13ed2fc7213b50b105369a...(100 chars in total)
I checked the length of AES_IV and it is still 16, but why does my for loop returns such a long result? Also why did the value of AES_IV change? I have checked and there are no reference to AES_IV anywhere before this part.
Your help is much appreciated.
Source code for reference:
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <dlfcn.h>
#include "SmartSDLib.h"
#define SDCARD_PATH "/boot"
#define P1_INDEX 2
#define P2_INDEX 3
#define P3_INDEX 4
#define AES_BLOCK_SIZE 0x10
#define APDU_HEADER_SIZE 5
#define FILELEN 32 // For reading IV from file
void To_Hex(char s8Data[],char hexBuffer[], int offset, int nlen)
{
int j;
int nSize = (nlen - offset);
char finalhash[8192];
char hexval [] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
for(j=0 ; j < nSize; j++)
{
finalhash[j*2] = hexval[(s8Data[j] >> 4) & 0xF];
finalhash[(j*2) + 1] = hexval[s8Data[j] & 0x0F];
}
memset(hexBuffer,0x00,8192);
memcpy(hexBuffer, finalhash, nSize*2);
}
char * AccessAPDU(char command[], int size)
{
unsigned char u8Ret;
int s32Len[1];
char s8Buffer[512], hexBuffer[8192];
To_Hex(command, hexBuffer, 0, size);
//printf ("%s\n%s%s\n", "AccessAPDUCommand:","CMD:" , hexBuffer);
u8Ret = OT_SmartSDCard_AccessAPDUCommand(size, command, s32Len, s8Buffer, 0);
if(u8Ret == 0x00)
{
printResult(s32Len, s8Buffer);
}
else
{
printf ("%s%x\n\n", "Error 0x", u8Ret);
}
return s8Buffer;
}
void printResult (int s32Len[], char s8Buffer[])
{
char hexBuffer[8192];
if(s32Len[0] >= 2)
{
To_Hex(s8Buffer, hexBuffer, 0, s32Len[0]);
// printf ("%s%s\n", "Ret:", hexBuffer);
// printf ("%s%d\n\n", "Len: ", s32Len[0]);
}
else
{
printf ("%s%d\n", "Len: ", s32Len[0]);
}
}
void CopyBuffer(char Dest[], char Source[], unsigned int length){
while(length){
Dest[length - 1] = Source[length - 1];
length--;
}
}
int ascii_to_hex(char c) // For reading IV from file
{
int num = (int) c;
if(num < 58 && num > 47) // Handle numbers (0-9)
{
return num - 48;
}
if(num < 103 && num > 96) // Handle lowercase alphabets (a-f)
{
return num - 87;
}
if(num < 71 && num > 64) // Handle uppercase alphabets (A-F)
{
return num - 55;
}
return num;
}
int main()
{
unsigned char u8Ret;
char SDCard_Path[64];
char line[64];
int s32Len[1];
char s8Buffer[512], hexBuffer[8192];
int exit = 1;
int iChoice;
char * response;
int test;
char testchar[32];
bool flag = true;
char fileName[100];
char encrypted[100];
char decrypted[100];
int i;
int j;
int blockCounter = 0;
int commandCounter = 0;
char imageBuffer[1];
/* APDU commands */
char AES_encrypt_cmd[APDU_HEADER_SIZE + AES_BLOCK_SIZE] = {0x80, 0x2A, 0x01,0x00, AES_BLOCK_SIZE}; //encryption APDU
char AES_decrypt_cmd[APDU_HEADER_SIZE + AES_BLOCK_SIZE] = {0x80, 0x2A, 0x02,0x00, AES_BLOCK_SIZE}; //decryption APDU
char GetResponse_cmd[] = {0x00, 0xC0, 0x00,0x00, 0x00}; //Le to be changed before use
char SelectAESapplet_cmd[] = {0x00, 0xA4, 0x04,0x00, 0x10, 0xA0, 0x00, 0x00, 0x00, 0x77, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0xF1, 0x00, 0x00, 0x00, 0x04};
//char PutKeyAES_cmd[] = {0x80, 0xD8, 0x00,0xF8, 0x20, 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00};
char PutKeyAES_cmd[] = {0x80, 0xD8, 0x00,0xF8, 0x20, 0xCF,0x10,0xF9,0xBD,0xF6,0xAA,0x15,0xC5,0x0E,0xCD,0x79,0xE6,0x28,0x3D,0xA8,0x0A,0x79,0x7A,0x2D,0x27,0x03,0xAE,0x07,0xD5,0x2F,0x5D,0x8F,0x60,0x1F,0x1E,0xF2,0x6F};
/* AES variables */
int AES_keyIndex = 0;
char AES_IV[AES_BLOCK_SIZE] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
//char AES_IV[AES_BLOCK_SIZE] = {0x12,0xFB,0x87,0x11,0x62,0x7E,0xBB,0x55,0x71,0x7B,0x2F,0x70,0xA4,0x97,0x55,0x7B};
//char AES_IV[AES_BLOCK_SIZE];
char AES_CBC_vector[AES_BLOCK_SIZE];
char AES_cmdBuffer[APDU_HEADER_SIZE + AES_BLOCK_SIZE];
FILE * rFile;
FILE * wFile;
FILE *fileptr;char *buffer;long filelen;int i2,i3,i4;char IV_temp[AES_BLOCK_SIZE]; // This line for the file --> byte --> Hex
printf ("%s\n", "This program will run 1, 2 followed by 6");
// case 1 :
printf ("%s\n", "Initializing: ");
u8Ret = OT_SmartSDCard_Initialization(SDCARD_PATH);
if(u8Ret == 0x00)
printf ("%s\n\n", "Initialization OK");
else
printf ("%s%x\n\n", "Initialization: Error 0x",u8Ret);
// printf ("%s\n\n", "-------------------------------------");
/* ATR */
u8Ret = OT_SmartSDCard_Reset(s32Len, s8Buffer);
if(u8Ret == 0x00)
{
To_Hex(s8Buffer,hexBuffer, 0, s32Len[0]);
// printf ("%s%s\n", "ATR : ", hexBuffer);
// printf ("%s%d\n\n", "Len : ", s32Len[0]);
}
else
printf ("%s%x\n\n", "Reset: Error 0x",u8Ret);
// printf ("%s\n\n", "-------------------------------------");
/* Select AES applet */
printf("%s\n", "Selecting AES applet...");
response = AccessAPDU(SelectAESapplet_cmd, sizeof(SelectAESapplet_cmd));
if(response[0] == 0x61){
GetResponse_cmd[P3_INDEX] = response[1];
AccessAPDU(GetResponse_cmd, sizeof(GetResponse_cmd));
}
// printf ("%s\n\n", "-------------------------------------");
// case 2 :
printf("Putting key...");
AES_keyIndex = 0;
memset(line, 0x00, sizeof(line));
PutKeyAES_cmd[2] = AES_keyIndex;
printf("putting key on index %d\n", AES_keyIndex);
response = AccessAPDU(PutKeyAES_cmd, sizeof(PutKeyAES_cmd));
AES_encrypt_cmd[P2_INDEX] = AES_keyIndex;
AES_decrypt_cmd[P2_INDEX] = AES_keyIndex;
// printf ("%s\n\n", "-------------------------------------");
// case 6 :
printf ("Decrypting in CBC...");
// printf ("File Name: /home/pi/test_encrypted\n");
// printf ("Encrypted File Name: /home/pi/test_decrypted\n");
rFile = fopen("/home/pi/test_encrypted","rb");
wFile = fopen("/home/pi/test_decrypted","wb");
////////////////////////////////////////////////////////////////
// This part to read rFile and store as a byte [WORKING!]
fseek(rFile, 0, SEEK_END); // Jump to the end of the file
filelen = ftell(rFile); // Get the current byte offset in the file
rewind(rFile); // Jump back to the beginning of the file
buffer = (char *)malloc((filelen+1)*sizeof(char)); // Enough memory for file + \0
fread(buffer, filelen, 1, rFile); // Read in the entire file
// This part to convert buffer to hex value [WORKING!]
for (i2 = 0; i2 < 100; i2++)
{
if (i2 > 0) printf(" ");
printf("%02X", buffer[i2]);
IV_temp[i2] = buffer[i2];
}
// This part to print stuff [AES_IV not printing out correctly]
printf("\nAES_IV: ");
//fwrite(AES_IV, 1, sizeof(AES_IV), stdout);
for (i3 = 0; i3 < 50; i3++) {
printf("%02x", AES_IV[i3]);
}
printf("\n");
/*
printf("IV_temp: ");
for (i4 = 0; i4 < AES_BLOCK_SIZE; i4++) {
printf("%02x", IV_temp[i4]);
}
printf("\n");
*/
for (i3 = 0; i3 < AES_BLOCK_SIZE; i3++) {
AES_IV[i3] = IV_temp[i3];
}
printf("\nLength of AES_IV = %d\n", sizeof(AES_IV));
printf("\nAES_IV: ");
for (i3 = 0; i3 < 50; i3++) {
printf("%02x", AES_IV[i3]);
}
printf("\n");
// Return the file pointer to start of file
rewind(rFile);
////////////////////////////////////////////////////////////////
if (rFile == NULL || wFile == NULL)
perror("Error opening File");
else{
int IsEOF = false;
// Initialize the CBC block with IV
CopyBuffer(AES_CBC_vector, AES_IV, AES_BLOCK_SIZE);
while (IsEOF == false) {
i = 0;
while(i < AES_BLOCK_SIZE){ // i run from 0 to (AES_BLOCK_SIZE - 1)
AES_cmdBuffer[APDU_HEADER_SIZE + i] = fgetc(rFile);
if(feof(rFile)) {
IsEOF = true;
if(i == 0) { // break out of while loop if a new block is not yet in progress
break;
} else { // continue to finish the block and pad with zeros
AES_cmdBuffer[APDU_HEADER_SIZE + i] = 0x00; //padding
}
}
i++;
}
if(i == 0) break; //break out of while loop, nothing to send to card
// Fill in the APDU header
CopyBuffer(AES_cmdBuffer, AES_decrypt_cmd, APDU_HEADER_SIZE);
response = AccessAPDU(AES_cmdBuffer, sizeof(AES_cmdBuffer));
if(response[0] == 0x61){
if(response[1] != AES_BLOCK_SIZE) {
perror("Card returning wrong block size");
}
GetResponse_cmd[P3_INDEX] = AES_BLOCK_SIZE;
response = AccessAPDU(GetResponse_cmd, sizeof(GetResponse_cmd));
/* CBC mode */
for(i = 0; i < AES_BLOCK_SIZE; i++){
response[i] ^= AES_CBC_vector[i];
}
fwrite(response, sizeof(char), AES_BLOCK_SIZE, wFile);
// Take the ciphertext to perform XOR next time
CopyBuffer(AES_CBC_vector, &AES_cmdBuffer[APDU_HEADER_SIZE], AES_BLOCK_SIZE);
} else {
perror("Error decrypting file using the smart card");
}
}
}
fclose (rFile);
fclose (wFile);
// printf ("%s\n\n", "-------------------------------------");
return 0;
}
As one comment already stated you should use unsigned char otherwise each element is treated as a signed char by default. Better yet you might want to use fixed-width uint8_t from #include <stdint.h> instead.
Here is a minimal example that works as expected:
#include <stdint.h>
#include <stdio.h>
#define AES_BLOCK_SIZE 0x10
uint8_t AES_IV[AES_BLOCK_SIZE] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
int
main( void )
{
int i;
for ( i = 0; i < sizeof(AES_IV); i++ ) {
printf( "%02x", AES_IV[i] );
}
return 0;
}

char array not displaying when passed to a function

Why is the passing of char array not showing?
The location of the pointer is passed to a function.
char plaintext[] = {
0xCD, 0x76, 0x43, 0xF0,
0x72, 0xA4, 0xA0, 0x82,
}
Given
void displayArray(char** plaintext, int size) {
// int newSize = sizeof(**plaintext);
int i;
for(i=0; i < size; ++i) {
printf("%02X ", (0xff & *plaintext[i]));
if (((i+1)% 8) == 0) // as index starts from 0, (i+1)
printf("\n");
}
}
in main()
char* plaintextCopy;
plaintextCopy = (char*) malloc(numberOfItems*sizeof(char));
memcpy(plaintextCopy, plaintext, numberOfItems);
displayArray(&plaintextCopy, numberOfItems);
Based on your code :
void displayArray(char** plaintext, int size)
{
int i;
for(i=0; i < size; i++)
{
printf("%02X ", (0xff & (*plaintext)[i]));
if(((i+1)% 8) == 0) // as index starts from 0, (i+1)
printf("\n");
}
}
int main(void)
{
char plaintext[] = {
0xCD, 0x76, 0x43, 0xF0,
0x72, 0xA4, 0xA0, 0x82,
};
int numberOfItems = sizeof(plaintext);
char* plaintextCopy;
plaintextCopy = (char*) malloc(numberOfItems*sizeof(char));
memcpy(plaintextCopy, plaintext, numberOfItems);
displayArray(&plaintextCopy, numberOfItems);
return 0;
}
It outputs :
CD 76 43 F0 72 A4 A0 82
Also, if you're sending an array that you want to display or change the values of, you don't need to send a double pointer to a function. A regular pointer would do. You should only use double pointers if the original array changes it's location in memory, that is, it's getting a new pointer after the function returns.

Hexadecimal string to byte array in C

Is there any standard C function that converts from hexadecimal string to byte array?
I do not want to write my own function.
As far as I know, there's no standard function to do so, but it's simple to achieve in the following manner:
#include <stdio.h>
int main(int argc, char **argv) {
const char hexstring[] = "DEadbeef10203040b00b1e50", *pos = hexstring;
unsigned char val[12];
/* WARNING: no sanitization or error-checking whatsoever */
for (size_t count = 0; count < sizeof val/sizeof *val; count++) {
sscanf(pos, "%2hhx", &val[count]);
pos += 2;
}
printf("0x");
for(size_t count = 0; count < sizeof val/sizeof *val; count++)
printf("%02x", val[count]);
printf("\n");
return 0;
}
Edit
As Al pointed out, in case of an odd number of hex digits in the string, you have to make sure you prefix it with a starting 0. For example, the string "f00f5" will be evaluated as {0xf0, 0x0f, 0x05} erroneously by the above example, instead of the proper {0x0f, 0x00, 0xf5}.
Amended the example a little bit to address the comment from #MassimoCallegari
I found this question by Googling for the same thing. I don't like the idea of calling sscanf() or strtol() since it feels like overkill. I wrote a quick function which does not validate that the text is indeed the hexadecimal presentation of a byte stream, but will handle odd number of hex digits:
uint8_t tallymarker_hextobin(const char * str, uint8_t * bytes, size_t blen)
{
uint8_t pos;
uint8_t idx0;
uint8_t idx1;
// mapping of ASCII characters to hex values
const uint8_t hashmap[] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // !"#$%&'
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ()*+,-./
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 01234567
0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 89:;<=>?
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // #ABCDEFG
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // HIJKLMNO
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // PQRSTUVW
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // XYZ[\]^_
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // `abcdefg
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // hijklmno
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pqrstuvw
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // xyz{|}~.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ........
};
bzero(bytes, blen);
for (pos = 0; ((pos < (blen*2)) && (pos < strlen(str))); pos += 2)
{
idx0 = (uint8_t)str[pos+0];
idx1 = (uint8_t)str[pos+1];
bytes[pos/2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1];
};
return(0);
}
Apart from the excellent answers above I though I would write a C function that does not use any libraries and has some guards against bad strings.
uint8_t* datahex(char* string) {
if(string == NULL)
return NULL;
size_t slength = strlen(string);
if((slength % 2) != 0) // must be even
return NULL;
size_t dlength = slength / 2;
uint8_t* data = malloc(dlength);
memset(data, 0, dlength);
size_t index = 0;
while (index < slength) {
char c = string[index];
int value = 0;
if(c >= '0' && c <= '9')
value = (c - '0');
else if (c >= 'A' && c <= 'F')
value = (10 + (c - 'A'));
else if (c >= 'a' && c <= 'f')
value = (10 + (c - 'a'));
else {
free(data);
return NULL;
}
data[(index/2)] += value << (((index + 1) % 2) * 4);
index++;
}
return data;
}
Explanation:
a. index / 2 | Division between integers will round down the value, so 0/2 = 0, 1/2 = 0, 2/2 = 1, 3/2 = 1, 4/2 = 2, 5/2 = 2, etc. So, for every 2 string characters we add the value to 1 data byte.
b. (index + 1) % 2 | We want odd numbers to result to 1 and even to 0 since the first digit of a hex string is the most significant and needs to be multiplied by 16. so for index 0 => 0 + 1 % 2 = 1, index 1 => 1 + 1 % 2 = 0 etc.
c. << 4 | Shift by 4 is multiplying by 16. example: b00000001 << 4 = b00010000
For short strings, strtol, strtoll, and strtoimax will work just fine (note that the third argument is the base to use in processing the string...set it to 16). If your input is longer than number-of-bits-in-the-longest-integer-type/4 then you'll need one of the more flexible methods suggested by other answers.
By some modification form user411313's code, following works for me:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int main ()
{
char *hexstring = "deadbeef10203040b00b1e50";
int i;
unsigned int bytearray[12];
uint8_t str_len = strlen(hexstring);
for (i = 0; i < (str_len / 2); i++) {
sscanf(hexstring + 2*i, "%02x", &bytearray[i]);
printf("bytearray %d: %02x\n", i, bytearray[i]);
}
return 0;
}
A fleshed out version of Michael Foukarakis post (since I don't have the "reputation" to add a comment to that post yet):
#include <stdio.h>
#include <string.h>
void print(unsigned char *byte_array, int byte_array_size)
{
int i = 0;
printf("0x");
for(; i < byte_array_size; i++)
{
printf("%02x", byte_array[i]);
}
printf("\n");
}
int convert(const char *hex_str, unsigned char *byte_array, int byte_array_max)
{
int hex_str_len = strlen(hex_str);
int i = 0, j = 0;
// The output array size is half the hex_str length (rounded up)
int byte_array_size = (hex_str_len+1)/2;
if (byte_array_size > byte_array_max)
{
// Too big for the output array
return -1;
}
if (hex_str_len % 2 == 1)
{
// hex_str is an odd length, so assume an implicit "0" prefix
if (sscanf(&(hex_str[0]), "%1hhx", &(byte_array[0])) != 1)
{
return -1;
}
i = j = 1;
}
for (; i < hex_str_len; i+=2, j++)
{
if (sscanf(&(hex_str[i]), "%2hhx", &(byte_array[j])) != 1)
{
return -1;
}
}
return byte_array_size;
}
void main()
{
char *examples[] = { "", "5", "D", "5D", "5Df", "deadbeef10203040b00b1e50", "02invalid55" };
unsigned char byte_array[128];
int i = 0;
for (; i < sizeof(examples)/sizeof(char *); i++)
{
int size = convert(examples[i], byte_array, 128);
if (size < 0)
{
printf("Failed to convert '%s'\n", examples[i]);
}
else if (size == 0)
{
printf("Nothing to convert for '%s'\n", examples[i]);
}
else
{
print(byte_array, size);
}
}
}
Here is HexToBin and BinToHex relatively clean and readable.
(Note originally there were returned enum error codes through an error logging system not a simple -1 or -2.)
typedef unsigned char ByteData;
ByteData HexChar (char c)
{
if ('0' <= c && c <= '9') return (ByteData)(c - '0');
if ('A' <= c && c <= 'F') return (ByteData)(c - 'A' + 10);
if ('a' <= c && c <= 'f') return (ByteData)(c - 'a' + 10);
return (ByteData)(-1);
}
ssize_t HexToBin (const char* s, ByteData * buff, ssize_t length)
{
ssize_t result = 0;
if (!s || !buff || length <= 0) return -2;
while (*s)
{
ByteData nib1 = HexChar(*s++);
if ((signed)nib1 < 0) return -3;
ByteData nib2 = HexChar(*s++);
if ((signed)nib2 < 0) return -4;
ByteData bin = (nib1 << 4) + nib2;
if (length-- <= 0) return -5;
*buff++ = bin;
++result;
}
return result;
}
void BinToHex (const ByteData * buff, ssize_t length, char * output, ssize_t outLength)
{
char binHex[] = "0123456789ABCDEF";
if (!output || outLength < 4) return (void)(-6);
*output = '\0';
if (!buff || length <= 0 || outLength <= 2 * length)
{
memcpy(output, "ERR", 4);
return (void)(-7);
}
for (; length > 0; --length, outLength -= 2)
{
ByteData byte = *buff++;
*output++ = binHex[(byte >> 4) & 0x0F];
*output++ = binHex[byte & 0x0F];
}
if (outLength-- <= 0) return (void)(-8);
*output++ = '\0';
}
Following is the solution I wrote up for performance reasons:
void hex2bin(const char* in, size_t len, unsigned char* out) {
static const unsigned char TBL[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 58, 59, 60, 61,
62, 63, 64, 10, 11, 12, 13, 14, 15, 71, 72, 73, 74, 75,
76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 10, 11, 12, 13, 14, 15
};
static const unsigned char *LOOKUP = TBL - 48;
const char* end = in + len;
while(in < end) *(out++) = LOOKUP[*(in++)] << 4 | LOOKUP[*(in++)];
}
Example:
unsigned char seckey[32];
hex2bin("351aaaec0070d13d350afae2bc43b68c7e590268889869dde489f2f7988f3fee", 64, seckey);
/*
seckey = {
53, 26, 170, 236, 0, 112, 209, 61, 53, 10, 250, 226, 188, 67, 182, 140,
126, 89, 2, 104, 136, 152, 105, 221, 228, 137, 242, 247, 152, 143, 63, 238
};
*/
If you don't need to support lowercase:
static const unsigned char TBL[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 58, 59,
60, 61, 62, 63, 64, 10, 11, 12, 13, 14, 15
};
you can use this function which
written with performance in mind (for embedded processors), no scanf, strtol or dynamic memory allocation
has guard against output buffer overflow and odd input str len
/// in: valid chars are 0-9 + A-F + a-f
/// out_len_max==0: convert until the end of input string, out_len_max>0 only convert this many numbers
/// returns actual out size
int hexStr2Arr(unsigned char* out, const char* in, size_t out_len_max = 0)
{
if (!out_len_max)
out_len_max = 2147483647; // INT_MAX
const int in_len = strnlen(in, out_len_max * 2);
if (in_len % 2 != 0)
return -1; // error, in str len should be even
// calc actual out len
const int out_len = out_len_max < (in_len / 2) ? out_len_max : (in_len / 2);
for (int i = 0; i < out_len; i++) {
char ch0 = in[2 * i];
char ch1 = in[2 * i + 1];
uint8_t nib0 = (ch0 & 0xF) + (ch0 >> 6) | ((ch0 >> 3) & 0x8);
uint8_t nib1 = (ch1 & 0xF) + (ch1 >> 6) | ((ch1 >> 3) & 0x8);
out[i] = (nib0 << 4) | nib1;
}
return out_len;
}
usage:
unsigned char result[128];
memset(result, 0, 128); // optional
printf("result len=%d\n", hexStr2Arr(result, "0a0B10")); // result = [0A 0B 10 00 00 ...]
memset(result, 0, 128); // optional
// only convert single number
printf("result len=%d\n", hexStr2Arr(result, "0a0B10", 1)); // result = [0A 00 00 00 00 ...]
hextools.h
#ifndef HEX_TOOLS_H
#define HEX_TOOLS_H
char *bin2hex(unsigned char*, int);
unsigned char *hex2bin(const char*);
#endif // HEX_TOOLS_H
hextools.c
#include <stdlib.h>
char *bin2hex(unsigned char *p, int len)
{
char *hex = malloc(((2*len) + 1));
char *r = hex;
while(len && p)
{
(*r) = ((*p) & 0xF0) >> 4;
(*r) = ((*r) <= 9 ? '0' + (*r) : 'A' - 10 + (*r));
r++;
(*r) = ((*p) & 0x0F);
(*r) = ((*r) <= 9 ? '0' + (*r) : 'A' - 10 + (*r));
r++;
p++;
len--;
}
*r = '\0';
return hex;
}
unsigned char *hex2bin(const char *str)
{
int len, h;
unsigned char *result, *err, *p, c;
err = malloc(1);
*err = 0;
if (!str)
return err;
if (!*str)
return err;
len = 0;
p = (unsigned char*) str;
while (*p++)
len++;
result = malloc((len/2)+1);
h = !(len%2) * 4;
p = result;
*p = 0;
c = *str;
while(c)
{
if(('0' <= c) && (c <= '9'))
*p += (c - '0') << h;
else if(('A' <= c) && (c <= 'F'))
*p += (c - 'A' + 10) << h;
else if(('a' <= c) && (c <= 'f'))
*p += (c - 'a' + 10) << h;
else
return err;
str++;
c = *str;
if (h)
h = 0;
else
{
h = 4;
p++;
*p = 0;
}
}
return result;
}
main.c
#include <stdio.h>
#include "hextools.h"
int main(void)
{
unsigned char s[] = { 0xa0, 0xf9, 0xc3, 0xde, 0x44 };
char *hex = bin2hex(s, sizeof s);
puts(hex);
unsigned char *bin;
bin = hex2bin(hex);
puts(bin2hex(bin, 5));
size_t k;
for(k=0; k<5; k++)
printf("%02X", bin[k]);
putchar('\n');
return 0;
}
char *hexstring = "deadbeef10203040b00b1e50", *pos = hexstring;
unsigned char val[12];
while( *pos )
{
if( !((pos-hexstring)&1) )
sscanf(pos,"%02x",&val[(pos-hexstring)>>1]);
++pos;
}
sizeof(val)/sizeof(val[0]) is redundant!
In main()
{
printf("enter string :\n");
fgets(buf, 200, stdin);
unsigned char str_len = strlen(buf);
k=0;
unsigned char bytearray[100];
for(j=0;j<str_len-1;j=j+2)
{ bytearray[k++]=converttohex(&buffer[j]);
printf(" %02X",bytearray[k-1]);
}
}
Use this
int converttohex(char * val)
{
unsigned char temp = toupper(*val);
unsigned char fin=0;
if(temp>64)
temp=10+(temp-65);
else
temp=temp-48;
fin=(temp<<4)&0xf0;
temp = toupper(*(val+1));
if(temp>64)
temp=10+(temp-65);
else
temp=temp-48;
fin=fin|(temp & 0x0f);
return fin;
}
This is a modified function from a similar question, modified as per the suggestion of https://stackoverflow.com/a/18267932/700597.
This function will convert a hexadecimal string - NOT prepended with "0x" - with an even number of characters to the number of bytes specified. It will return -1 if it encounters an invalid character, or if the hex string has an odd length, and 0 on success.
//convert hexstring to len bytes of data
//returns 0 on success, -1 on error
//data is a buffer of at least len bytes
//hexstring is upper or lower case hexadecimal, NOT prepended with "0x"
int hex2data(unsigned char *data, const unsigned char *hexstring, unsigned int len)
{
unsigned const char *pos = hexstring;
char *endptr;
size_t count = 0;
if ((hexstring[0] == '\0') || (strlen(hexstring) % 2)) {
//hexstring contains no data
//or hexstring has an odd length
return -1;
}
for(count = 0; count < len; count++) {
char buf[5] = {'0', 'x', pos[0], pos[1], 0};
data[count] = strtol(buf, &endptr, 0);
pos += 2 * sizeof(char);
if (endptr[0] != '\0') {
//non-hexadecimal character encountered
return -1;
}
}
return 0;
}
Two short routines to parse a byte or a word, using strchr().
// HexConverter.h
#ifndef HEXCONVERTER_H
#define HEXCONVERTER_H
unsigned int hexToByte (const char *hexString);
unsigned int hexToWord (const char *hexString);
#endif
// HexConverter.c
#include <string.h> // for strchr()
#include <ctype.h> // for toupper()
unsigned int hexToByte (const char *hexString)
{
unsigned int value;
const char *hexDigits = "0123456789ABCDEF";
value = 0;
if (hexString != NULL)
{
char *ptr;
ptr = strchr (hexDigits, toupper(hexString[0]));
if (ptr != NULL)
{
value = (ptr - hexDigits) << 4;
ptr = strchr (hexDigits, toupper(hexString[1]));
if (ptr != NULL)
{
value = value | (ptr - hexDigits);
}
}
}
return value;
}
unsigned int hexToWord (const char *hexString)
{
unsigned int value;
value = 0;
if (hexString != NULL)
{
value = (hexToByte (&hexString[0]) << 8) |
(hexToByte (&hexString[2]));
}
return value;
}
// HexConverterTest.c
#include <stdio.h>
#include "HexConverter.h"
int main (int argc, char **argv)
{
(void)argc; // not used
(void)argv; // not used
unsigned int value;
char *hexString;
hexString = "2a";
value = hexToByte (hexString);
printf ("%s == %x (%u)\n", hexString, value, value);
hexString = "1234";
value = hexToWord (hexString);
printf ("%s == %x (%u)\n", hexString, value, value);
hexString = "0102030405060708090a10ff";
printf ("Hex String: %s\n", hexString);
for (unsigned int idx = 0; idx < strlen(hexString); idx += 2)
{
value = hexToByte (&hexString[idx]);
printf ("%c%c == %x (%u)\n", hexString[idx], hexString[idx+1],
value, value);
}
return EXIT_SUCCESS;
}
Try the following code:
static unsigned char ascii2byte(char *val)
{
unsigned char temp = *val;
if(temp > 0x60) temp -= 39; // convert chars a-f
temp -= 48; // convert chars 0-9
temp *= 16;
temp += *(val+1);
if(*(val+1) > 0x60) temp -= 39; // convert chars a-f
temp -= 48; // convert chars 0-9
return temp;
}
Here's my version:
/* Convert a hex char digit to its integer value. */
int hexDigitToInt(char digit) {
digit = tolower(digit);
if ('0' <= digit && digit <= '9') //if it's decimal
return (int)(digit - '0');
else if ('a' <= digit && digit <= 'f') //if it's abcdef
return (int)(digit - ('a' - 10));
else
return -1; //value not in [0-9][a-f] range
}
/* Decode a hex string. */
char *decodeHexString(const char *hexStr) {
char* decoded = malloc(strlen(hexStr)/2+1);
char* hexStrPtr = (char *)hexStr;
char* decodedPtr = decoded;
while (*hexStrPtr != '\0') { /* Step through hexStr, two chars at a time. */
*decodedPtr = 16 * hexDigitToInt(*hexStrPtr) + hexDigitToInt(*(hexStrPtr+1));
hexStrPtr += 2;
decodedPtr++;
}
*decodedPtr = '\0'; /* final null char */
return decoded;
}
Could it be simpler?!
uint8_t hex(char ch) {
uint8_t r = (ch > 57) ? (ch - 55) : (ch - 48);
return r & 0x0F;
}
int to_byte_array(const char *in, size_t in_size, uint8_t *out) {
int count = 0;
if (in_size % 2) {
while (*in && out) {
*out = hex(*in++);
if (!*in)
return count;
*out = (*out << 4) | hex(*in++);
*out++;
count++;
}
return count;
} else {
while (*in && out) {
*out++ = (hex(*in++) << 4) | hex(*in++);
count++;
}
return count;
}
}
int main() {
char hex_in[] = "deadbeef10203040b00b1e50";
uint8_t out[32];
int res = to_byte_array(hex_in, sizeof(hex_in) - 1, out);
for (size_t i = 0; i < res; i++)
printf("%02x ", out[i]);
printf("\n");
system("pause");
return 0;
}
For newbies and to improve #Michael's answer here's a full version:
// out needs to be at least len/2+1 bytes long
// length will be returned
int hexStrToBin(char* out, const char *str)
{
int i, len = strlen(str);
for (i = 0; i < len; i++)
{
// Reads str & stores in op
sscanf(str, "%2hhx", &op[i]);
str += 2;
}
return len/2;
}
No. But it's relatively trivial to achieve using sscanf in a loop.
The best way I know:
int hex2bin_by_zibri(char *source_str, char *dest_buffer)
{
char *line = source_str;
char *data = line;
int offset;
int read_byte;
int data_len = 0;
while (sscanf(data, " %02x%n", &read_byte, &offset) == 1) {
dest_buffer[data_len++] = read_byte;
data += offset;
}
return data_len;
}
The function returns the number of converted bytes saved in dest_buffer.
The input string can contain spaces and mixed case letters.
"01 02 03 04 ab Cd eF garbage AB"
translates to dest_buffer containing
01 02 03 04 ab cd ef
and also
"01020304abCdeFgarbageAB"
translates as before.
Parsing stops at the first "error".
Here is a solution to deal with files, which may be used more frequently...
int convert(char *infile, char *outfile) {
char *source = NULL;
FILE *fp = fopen(infile, "r");
long bufsize;
if (fp != NULL) {
/* Go to the end of the file. */
if (fseek(fp, 0L, SEEK_END) == 0) {
/* Get the size of the file. */
bufsize = ftell(fp);
if (bufsize == -1) { /* Error */ }
/* Allocate our buffer to that size. */
source = malloc(sizeof(char) * (bufsize + 1));
/* Go back to the start of the file. */
if (fseek(fp, 0L, SEEK_SET) != 0) { /* Error */ }
/* Read the entire file into memory. */
size_t newLen = fread(source, sizeof(char), bufsize, fp);
if ( ferror( fp ) != 0 ) {
fputs("Error reading file", stderr);
} else {
source[newLen++] = '\0'; /* Just to be safe. */
}
}
fclose(fp);
}
int sourceLen = bufsize - 1;
int destLen = sourceLen/2;
unsigned char* dest = malloc(destLen);
short i;
unsigned char highByte, lowByte;
for (i = 0; i < sourceLen; i += 2)
{
highByte = toupper(source[i]);
lowByte = toupper(source[i + 1]);
if (highByte > 0x39)
highByte -= 0x37;
else
highByte -= 0x30;
if (lowByte > 0x39)
lowByte -= 0x37;
else
lowByte -= 0x30;
dest[i / 2] = (highByte << 4) | lowByte;
}
FILE *fop = fopen(outfile, "w");
if (fop == NULL) return 1;
fwrite(dest, 1, destLen, fop);
fclose(fop);
free(source);
free(dest);
return 0;
}

Resources