0

I'm implementing a system that uses libcrafter and crypto++ to transmit specific frames on the network. But the problem I'm stuck with isn't at this level at all.

It's about conversion between types used in these libraries.


1) At the emission (solved)

I'm trying to convert the Crafter::byte array to a std::string, in order to put this message into a network frame (as an initialization vector for an AES encryption/decryption).

Moreover, the iv must be zeroed, and I can't initialize it properly (despite the answers here or there).

EDIT : to initialize it to 00, I had to do it in hexadecimal : 0x30. And to convert it to a std::string I had to provide the length ie ivLen2 (thanks for the answers).

Here's what I do :

const int ivLen2 = 2;
std::string encrypted_message("whatever");

Crafter::byte iv[ivLen2]={0x30, 0x30}; // crypto salt of 2 bytes at 0.

std::string ivStr( reinterpret_cast< char const* >(iv), ivLen2 ) ;

string mess2send = ivStr + encrypted_message;

And if I display them, with this :

cout<<"iv[0] : "<<iv[0]<<endl;            // display 0
cout<<"mess2send : "<<mess2send<<endl;    // display 00whatever

Why don't I simply create a zeroed string and send it ? In order to have generic functions, and a re-usable code.


2) At the reception (pending)

Without surprises I have to do the opposite. I get a iv and the message concatenated within a vector<byte>* payload, and I have to extract the iv as a byte array, and the message within a string.

The message isn't actually the problem, given that std::string is close to vector.

Here's what I tempt to retrieve the iv :

Crafter::byte iv[ ivLen2 ];
for (int i = 0; i < ivLen2; i++)
{
    iv[i] = (byte)payload->at(i);
}
std::string iv_rcv( reinterpret_cast< char const* >(iv) ) ;

And to display them, I do (in the same loop) :

cout<<iv[i];

But it gives me a non-ASCII character.

I've also tried this (following this and this answers) :

Crafter::byte* iv;

std::string iv_rcv( payload->begin(), payload->begin()+ivLen2 ) ;
iv = (byte*)iv_rcv.c_str();

But it doesn't give me the supposed initialized values...

Does anybody have a clue ? Is my code wrong ?

Community
  • 1
  • 1
3isenHeim
  • 243
  • 5
  • 22

1 Answers1

1

I don't think this will work:

const int ivLen2 = 2;
std::string encrypted_message("whatever");

Crafter::byte iv[ivLen2]={0x00, 0x00}; // crypto salt of 2 bytes

std::string ivStr( reinterpret_cast< char const* >(iv) ) ;

How does the std::string know how much data to copy from the iv pointer?

You have to use a constructor that takes the length of the data like:

std::string ivStr( reinterpret_cast< char const* >(iv), ivLen2 ) ;

The pointer only constructor is for specifically encoded strings that are terminated by a null character. Unless you are using one of those you must pass the length.

Try this:

const int ivLen2 = 2;
std::string encrypted_message("whatever");

Crafter::byte iv[ivLen2]={0x00, 0x00}; // crypto salt of 2 bytes

std::string ivStr( reinterpret_cast< char const* >(iv), ivLen2 ) ;

std::string mess2send = ivStr + encrypted_message;

std::cout << (int)mess2send[0] << (int)mess2send[1] << mess2send.substr(2) << '\n';
Galik
  • 47,303
  • 4
  • 80
  • 117
  • OK, thanks! :) But I still have the problem of the initialization. It doesn't gives me `00`, even if a convert it in hex... – 3isenHeim Sep 18 '15 at 14:23
  • @EisenHeim `std::cout` will not show you `00` because what you put in there was two character codes with value `0`, not two character codes with values that are represented by the letter `'0'`. – Galik Sep 18 '15 at 14:26
  • @EisenHeim Try this: `std::cout << (int)mess2send[0] << (int)mess2send[1] << mess2send.substr(2) << '\n';` – Galik Sep 18 '15 at 14:29
  • @Ga Lik - `error: ‘std::string’ has no member named ‘substring’` – 3isenHeim Sep 18 '15 at 14:31
  • @EisenHeim my error I have edited it to fix bug and use your string name – Galik Sep 18 '15 at 14:33
  • It displays me `10whatever` ! :) Could `10` be the values of the initialized `iv` ? – 3isenHeim Sep 18 '15 at 14:41
  • @EisenHeim I don't know why that's happening for you. The code I posted at the end of my answer produces this: `00whatever `. – Galik Sep 18 '15 at 14:52
  • Sorry, I didn't see the new edit. Yeah, now it's working ! I think I'll look at the reception on monday. Thanks and have a nice WE ! – 3isenHeim Sep 18 '15 at 14:55