d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help With Splitting A Key Into Segments
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 10 2013 01:42am
Hey guys need some help understanding how to do this real quick

I am doing a program in C and one of the requirements is to accept a 64-bit private KEY on the command line. I have to take this key and split it into 8 byte-length segments and store them into an array.

So for example if I am given:

Code
3'kTO  //ASCII value


edit: not sure if that is the exact input my professor will give us, I used a random key generating site to produce that :o


how would I go about finding the byte representation of that, and then breaking it up?


we are working with unsigned chars for the inherent mod 256 properties

This post was edited by Eep on Jul 10 2013 02:10am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 10 2013 06:03am
in C, just read it in as a string since a string is already a byte array. i think it's 1 byte per character. there will be a lot of non-printable chars though so i'm not sure what happens with that.
Member
Posts: 4
Joined: Jul 10 2013
Gold: 0.00
Jul 10 2013 12:49pm
in C each character is a single byte, unless its a unicode character which then it is 2 bytes in length.

essentially just make an 64 byte array and read in 8 bytes at a time.

a simplesolutionn could be something like so.

Code
char *key = 'abcdefghijklmnopqrstuvwyxzabcdefghijklmnopqrstuvwyxz';
char eight_bytes[9] = {0};
char *pointer  = key;

while(*pointer)
{
  memcpy(eight_bytes, pointer, 8);
  printf("%s", eight_bytes);
  *pointer += 8;
}


essentially it will loop until *pointer reaches a null byte and then each iteration it will copy 8 bytes into a new char array and increment the pointer by 8 to fetch the next 8 bytes the next iteration.

edit:: a more direct approach would be to just to look through each byte and using a counter to know when to stop at 8 byte increments.

This post was edited by pop_eax on Jul 10 2013 12:51pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 11 2013 08:21pm
yeah turns out it will just literally be 8 hex values

he said an example would be like:

0x0001020304050607

would just have to split it to 00 01 02 03 etc

This post was edited by Eep on Jul 11 2013 08:21pm
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Aug 8 2013 06:46pm
the value is already is available as an 8 byte array oO.

arguments in C are char* pointers so it's already there and all done for you.

Assuming you pass only one argument in the command line:

Code

int main(int argv, char** argc)
{
  char* my_8bytes_array = argc[1]; //this is your array

  /*your code here
  */

  return 0;
}
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 9 2013 03:33am
Quote (flyinggoat @ Aug 8 2013 07:46pm)
the value is already is available as an 8 byte array oO.

arguments in C are char* pointers so it's already there and all done for you.

Assuming you pass only one argument in the command line:

Code
int main(int argv, char** argc)
{
  char* my_8bytes_array = argc[1]; //this is your array

  /*your code here
  */

  return 0;
}


yeah i solved this a long time ago my problem was not that but rather converting random ascii into values but I was wrong about what the form of the key was, it was actually a string of hex characters

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll