1

I'm trying to encrypt a std::vector with XTEA. Because using std::vector brings various benefits dealing with big amounts of data, i want to use it.

The XTEA-Alogrithm uses two unsigned longs (v0 and v1) which take 64 bits of data, to encrypt them.

xtea_enc(unsigned char buf[], int length, unsigned char key[], unsigned char** outbuf)
/* Source http://pastebin.com/uEvZqmUj */

unsigned long v0 = *((unsigned long*)(buf+n));  
unsigned long v1 = *((unsigned long*)(buf+n+4));

My problem is, that I'm looking for the best way to convert my char vector into a unsigned long pointer.

Or is there another way to split vector in 64-bit parts for the encryption function?

  • I suggest you add to your question an explicit statement of what type the vector is. Do you really have `std::vector`? It looks to me like the `xtea_enc` function is provided by a library, and you are looking at the implementation details. When all you need to do is pass in a pointer to the beginning of your data, plus the length. – NicholasM Aug 17 '14 at 14:51

2 Answers2

2

The insight comes in realizing that each char is a byte; thus a 64 bit number consists of 8 bytes or two 32 bit numbers.

Thus one 32 bit number can store 4 bytes, so you would for each 8 byte block in your char vector, store a pair of 4 byte numbers in a pair of 32 bit numbers. You would then pass this pair in to your xtea function, something like:

uint32_t datablock[2];
datablock[0] = (buf[0] << 24) | (buf[1] << 16)  | (buf[2] << 8) | (buf[3]);
datablock[1] = (buf[4] << 24) | (buf[5] << 16)  | (buf[6] << 8) | (buf[7]);

where in this example, buf is the type char[8] (or more appropriately uint8_t[8]).

The bit-shift '<<' operator shifts the placement of where a given byte's bits should be stored in the uint32_t (thus for example, the first byte in the above example is stored in the first 8 bits of datablock[0]). The '|' operator provides a concatenaton of all bits so that you end up with the full 32 bit number. Hope that makes sense.

Ben J
  • 1,367
  • 2
  • 15
  • 33
0

My problem is, that I'm looking for the best way to convert my char vector into a unsigned long pointer.

((unsigned long*)vec.data()) since C++11 or ((unsigned long*)&vec[0])) pre-c++11?

PS: i guess someone will come along and argue that it should be a reinterpret_cast<unsigned long*>() or something sooner or later, and they'll probably be right.

also, i used a std::string, but here's how i did the enciper loop:

        string message = readMessage();
        for (size_t i = 0; i < message.length(); i += 8)
        {
            encipher(32, (uint32_t *)&message[i], keys);
        }
        // now message is encrypted

and

        for (size_t i = 0; i < message.length(); i += 8)
        {
            decipher(32, (uint32_t *)&message[i], keys);
        }
        // now message is decrypted (still may have padding bytes tho)
  • and i just used the sample C enciper/deciper functions from XTEA's wikipedia page.
hanshenrik
  • 19,904
  • 4
  • 43
  • 89