How to split an array in c? - c

I need a logic to acheive my target. I've buffer array size of 38400. This array data can be filled by a controller. Here i have to obtain an AES algorithm. In that i've to read 16 bytes of data from the buffer then encrypt, upto end of buffer. How to spilt an array into 16 bytes and encrypt? . I used following logic but i can't get it right now ?
unsigned char ptext[16] = "Attack at dawn!";
unsigned char ctext[16];
unsigned char decptext[16];
unsigned char buffer[120*160*2];
for (int count = 0; count < 120*160*2; count ++)
buffer[count] = count + 1;
for (i = 0; i < 120*160*2; i ++)
{
ptext[i]= buffer[i];
if(i%15 == 0)
{
aes_encrypt(ctx, ptext, ctext);
for(k = 0; k<=i; k++)
{
ptext[k]='\0';
}
}
}
void aes_encrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16])
{
int i;
// copy input to state
for(i = 0; i < 16; i++)
ctx->state[i & 0x03][i >> 2] = input[i];
aes_addroundkey(ctx, 0);
for(i = 1; i < ctx->rounds; i++) {
aes_subbytes(ctx);
aes_shiftrows(ctx);
aes_mixcolumns(ctx);
aes_addroundkey(ctx, i);
}
aes_subbytes(ctx);
aes_shiftrows(ctx);
aes_addroundkey(ctx, ctx->rounds);
// copy state to output
for(i = 0; i < 16; i++)
{
output[i] = ctx->state[i & 0x03][i >> 2];
printf("%c",output[i]);
}
}
Note: I've filled buffer[] with random numbers.
Me only have to know how to split an array.
Thanks in Advance.

You don't need to "split" the array (whatever "split" means for you.) Just operate on every 16-byte segment of it:
void process_segment(unsigned char segment[])
{
// Work on the first 16 bytes of 'segment'.
}
// ...
unsigned char buffer[120*160*2];
for (size_t i = 0; i < 120*160*2; i += 16) {
process_segment(buffer + i);
}
The above is just an example. If you want a nested for loop instead, you'd do something like this:
unsigned char buffer[120*160*2];
for (size_t i = 0; i < 120*160*2; i += 16) {
unsigned char* segment = buffer + i;
// Work on the first 16 bytes of 'segment'.
for (size_t j = 0; j < 16; ++j) {
// Work on segment[j].
}
}
You should probably change your aes_encrypt() function to take an unsigned char input[] instead of an unsigned char input[16] so that you can pass segment to it.
The code you posted would then become something like this:
unsigned char ptext[16] = "Attack at dawn!";
unsigned char ctext[16];
unsigned char decptext[16];
unsigned char buffer[120*160*2];
for (int count = 0; count < 120*160*2; count++)
buffer[count] = count + 1;
for (i = 0; i < 120*160*2; i += 16) {
unsigned char *segment = buffer + i;
aes_encrypt(ctx, segment, ctext);
// Clear the current 16-byte segment.
memset(segment, '\0', 16);
// ctext now contains the encrypted data of the current
// 16-byte segment. I assume you want to save it somewhere
// now since it will be overridden in the next iteration of
// the loop.
}
And the signature of your aes_encrypt() function would become:
void aes_encrypt(aes_ctx_t *ctx, unsigned char input[],
unsigned char output[16])

Related

Find the difference between elements in an array and store the results in a new array

This is my current code:
#include <stdio.h>
int index_x[] = {0,0,1,0,1,0,0,0,1,0,0,1,0}; // any number of elements
int len = sizeof index_x / sizeof*index_x;
int main(void) {
int arr[len];
int j = 0;
for (int i = 0; i < len; i++)
if (index_x[i])
arr[j++] = i; // save and advance j only if value is "1"
for (int i = 0; i < j; i++) // only print below j !
printf("%d\n", arr[i]);
}
Output:
2
4
8
11
From this output, I would like to generate another array that is the difference between these elements. In this case the new array would be {2,4,3}. (2-4=2, 8-4=4, 11-8=3).
I am currently struggling with two things:
Saving the array generated from the current code arr[i] as a another array so I can manipulate it for future uses.
Generating the "differences array". The tricky part is that the number of elements will not be constant so I cannot specify an array size.
It may be helpful to break things out into functions.
int indexes_of_non_zero(int *source, size_t len, int **dest) {
*dest = malloc(sizeof(int) * len);
int count = 0;
for (int i = 0; i < len; i++) {
if (source[i] != 0) {
(*dest)[count++] = i;
}
}
*dest = realloc(*dest, sizeof(int) * count);
return count;
}
So we have a function indexes_of_non_zero that takes a source array of ints with size specified by argument len, and then a pointer to an array of ints that will be the destination for our output.
We can naively allocate the same amount of memory to the destination, then loop over the source array and store the indexes of non-zero elements. When we're done, we use count to know the size of destination array. We use realloc to shrink the array to just the needed size. And the key is we return the count so our main function knows how big the destination array is.
We can now create a diffs function to do basically the same thing for the differences.
int diffs(int *source, size_t len, int **dest) {
*dest = malloc(sizeof(int) * (len - 1));
for (int i = 0; i < (len - 1); i++) {
(*dest)[i] = abs(source[i] - source[i + 1]);
}
return len - 1;
}
Putting it all together (not copying and pasting the function implementations for terseness) with a main function, and remembering to free the memory we've used, we get:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int indexes_of_non_zero(int *source, size_t len, int **dest);
int diffs(int *source, size_t len, int **dest);
int main(void) {
int data[] = {0,0,1,0,1,0,0,0,1,0,0,1,0};
size_t len = sizeof(data) / sizeof(*data);
int *nz = NULL;
int *d = NULL;
int nzc = indexes_of_non_zero(data, len, &nz);
int dlen = diffs(nz, nzc, &d);
for (int i = 0; i < nzc; i++) {
printf("%d\n", nz[i]);
}
for (int i = 0; i < dlen; i++) {
printf("%d\n", d[i]);
}
free(nz);
free(d);
}
And compiling and running it, the output is:
2
4
8
11
2
4
3
I changed it to get the differences as direct as possible; the inner loop is very simple.
The size question, first part: I (still) tried to avoid size_t for the input but added some sanity check for the size. Just to show there is a limit.
part two: a first loop to get the sum of true values in advance. Also the booldata array consists of chars and is not called index_x. Index is i.
part three: the diffs VLA gets its exact size, which can be 0 even for huge inputs. Some test could be added here after counting, to rule out a (big) input full of "1".
I added the copying, with message and printout.
#include <stdio.h>
#include <string.h>
char booldata[] = { 0,0,1,0,1,0,0,0,1,0,0,1,0 };
char sz_overf = sizeof booldata > 0xffffL * 0xffff; // too-big-flag for exiting...
int len = sizeof booldata / sizeof * booldata; // ...to keep len below size_t
int sum_of_trues (char *booldata, int len) {
int sum = 0;
for (int i = 0; i < len; i++)
if (booldata[i])
sum++;
return sum;
}
void print_intarr(int *inta, int len) {
for (int i = 0; i < len; i++)
printf("%d\n", inta[i]);
}
int main(void)
{
if (sz_overf) return 100; // len might be overflowed
int unsigned // signed provokes VLA warning: 9 gazillions seems ok, but not 18
dsum = sum_of_trues(booldata, len) - 1; // Invest a counting loop
int diffs[dsum], // VLA
di = 0; // diffs' index
int sti = -1; // Stored last index containing true
for (int i = 0; i < len; i++)
if (booldata[i]) { // true?
if (sti >= 0) { // and is there a left neighbour?
int diff = i - sti; // how far away?
printf("%d\n", diff); // 1. on-the-fly result
diffs[di++] = diff; // 2. for keeps
}
sti = i; // remember last "true"
}
printf("Copying %zu bytes from Diff.-Array\n", sizeof diffs);
int diffscopy[sizeof diffs];
memcpy(diffscopy, diffs, sizeof diffs);
print_intarr(diffscopy, dsum); // dsum or sizeof diffs
return 0;
}
output:
2
4
3
Copying 12 bytes from Diff.-Array
2
4
3
The inner loop could look like this w/o multi-option:
if (booldata[i]) {
if (sti >= 0) // and is there a left neighbour?
diffs[di++] = i - st; // 2. for keeps
sti = i;
}
Two arrays and three variables. The rest is needed to make this work.
BUGS: all-false input segfaults. It takes one "true" to get zero diffs...

Convert char array elements into Hexadecimal equivalent in C code

I'm currently trying to create a custom function in C code that would take an unsigned char array like: array[] = "11000000111111111000000010000000" as the input and convert this into 0xC0FF8080 and either store this back into array or simply printf to the console. Any help in clearing up my confusion on this would be greatly appreciated. Thanks!
Iterate over the string, and with each iteration shift the result to the left and set the right-most bit to the appropriate value:
#include <stdio.h>
#include <string.h>
int main(void)
{
char const array[] = "11000000111111111000000010000000";
size_t const length = strlen(array);
unsigned long long n = 0ULL;
for (size_t idx = 0U; idx < length; ++idx)
{
n <<= 1U;
n |= array[idx] == '0' ? 0U : 1U;
// n |= array[idx] - '0'; // An alternative to the previous line
}
printf("%#llx\n", n);
}
This uses a (signed) char array, but the method is the same.
Storing the result back into the char array:
#include <stdio.h>
#include <string.h>
int main(void)
{
char array[] = "11000000111111111000000010000000";
size_t length = strlen(array);
for (size_t idx = 0U; idx < length; ++idx)
array[idx] = array[idx] - '0';
for (size_t idx = 0U; idx < length; ++idx)
printf("%d", array[idx]);
putchar('\n');
}
Note that here the char types will hold the decimal values 0 and 1.

Get an Integer value from 4 consecutive bytes in a string

Being given a char array, I have to save in a variable of type int a number that is represented on 4 consecutive bytes in that char array.
I know the position where the first byte is in that array.
What should I do?
Edit:
Solution #1:
int number , offset;
char *data;
memcpy(&number, &data[offset], 4);
Solution #2:
int number , offset;
char *data;
number = *(int*)(data + offset);
// offset: where the first of the four bytes is situated
Not tested
int convert_to_int(chat *buff, int ascii, int str, int endianess)
{
union
{
int i;
unsigned char uc[sizeof(int)];
}ui = {.i = 0};
int mult = 1;
if(str)
{
for(int digit = 0; digit < sizeof(ui.i); digit++)
{
mult *= 10;
}
}
for(size_t index = 0; index < sizeof(int); index++)
{
size_t pos = endianess ? index : sizeof(ui.i) - 1 - index;
if(str)
{
ui.i += mul * (*buff++ - '0');
mul /= 10;
}
else
{
ui[pos] = ascii ? *buff++ - '0' : *buff++;
}
}
return ui.i;
}
This is what I tried to do. It was correct, but I had a wrong offset and I thought the method was wrong. Using memcpy, I got the same result, so I knew which was the problem.
int number , offset;
char *data;
number = *(int*)(data + offset);
// offset: where the first of the four bytes is situated

Converting unsigned char (array) to unsigned int (array)

I want to make a function that converts unsigned char to unsigned int and store it into an array. However, this ends up with an error that says
passing argument 1 of 'sprintf' from incompatible pointer type.
int main(void) {
unsigned char key[16] = "1234567812345678";
phex(key, 16); //store into an array here
}
uint64_t* phex(unsigned char* string, long len)
{
uint64_t hex[len];
int count = 0;
for(int i = 0; i < len; ++i) {
count = i * 2;
sprintf(hex + count, "%.2x", string[i]);
}
for(int i = 0; i < 32; i++)
printf(hex[i]);
return hex;
}
As comments have already said, you have problems in your code...
First of all sprintf function does totally opposite thing of what you want/expect it to do. Next, you create a local variable in your function, and return pointer to it.. As soon as function exits, pointer is invalid. Third problem I see is that you never assign return value to anything...
Proposition on how to fix your code:
unsigned* phex(unsigned char* string, long len);
int main(void) {
int i;
unsigned char key[16] = "1234567812345678";
unsigned* ints = phex(key,16); //store into an array here
for(i = 0; i < 16; i++)
printf("%d ", ints[i]);
//never forget to deallocate memory
free(ints);
return 0;
}
unsigned* phex(unsigned char* string, long len)
{
int i;
//allocate memory for your array
unsigned* hex = (unsigned*)malloc(sizeof(unsigned) * len);
for(i = 0; i < len; ++i) {
//do char to int conversion on every element of char array
hex[i] = string[i] - '0';
}
//return integer array
return hex;
}

C RC4 super weird behavior

so I found the implementation of RC4 in pure C, which I was using on my website. It was working super good except when I input a 6 characters string. Then I get the Internal Error Page. Figured out that only this length causes a problem.
1.Crypt.c
unsigned char S[256];
unsigned int i, j;
void swap(unsigned char *s, unsigned int i, unsigned int j) {
unsigned char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
/* KSA */
void rc4_init(unsigned char *key, unsigned int key_length) {
for (i = 0; i < 256; i++)
S[i] = i;
for (i = j = 0; i < 256; i++) {
j = (j + key[i % key_length] + S[i]) & 255;
swap(S, i, j);
}
i = j = 0;
}
/* PRGA */
unsigned char rc4_output() {
i = (i + 1) & 255;
j = (j + S[i]) & 255;
swap(S, i, j);
return S[(S[i] + S[j]) & 255];
}
char *rc4_e(char *text, size_t text_length)
{
char *dup=(char *)malloc(text_length * sizeof(char));
strcpy(dup,text);
unsigned char *vector[2] = {"key", dup};
int y;
rc4_init(vector[0], strlen((char*)vector[0]));
char *out=(char *)malloc(text_length * sizeof(char) );
char *ptr=out;
for (y = 0; y < strlen((char*)vector[1]); y++)
ptr += sprintf(ptr,"%02X",vector[1][y] ^ rc4_output());
*(ptr + 1) = '\0';
return out;
}
2.Main
#define SIZE 1000
char* pass=(char*)malloc(SIZE * sizeof(char));
char *RC4_pass=(char*)malloc(getSize(pass) * sizeof(char));
strcpy(RC4_pass,rc4_e(pass,sizeof(pass)));
Any advice or thoughts are extremely welcome. Just want to know whether it is the function itself that is bad or the rest of my C code.
Thank!
There is a problem with this line:
char *dup=(char *)malloc(text_length * sizeof(char));
You forgot to add an extra byte for the terminating '\0' at the end of the string. So at the very next line:
strcpy(dup,text);
you're committing an out-of-bounds access in the array dup, which is causing undefined behaviour.

Resources