1

I am using a library called botan for encryption. but the case here is not related with the library, it seems to be a issue in c++ or casting. using the library a 16 byte long vector is created as below.

  SecureVector<byte> salt = rng.random_vec(16);

then it is converted to a string as,

  std::string salt_string ((const char*)salt.begin() , salt.size());

using Qt i can just read the srting as,

  ui->textEdit->append("Salt is : "+ QString::fromStdString(salt_string));

now I need to write this to a file and regenerate the vector at a later time. It is written to a file as,

 ofstream outfile ("salt.txt" , std::ios::binary);
 outfile.write((const char*)salt.begin(), salt.size());

up to this point the code looks working great and the problem occurs when reading and regenerating the vector.

here is how i read the data to a char* array,

  ifstream infile ("salt.txt" , std::ios::binary );
  char* salt = new char[16];
  infile.read(salt , 16 );

now I need to recreate the SecureVector<byte> as salt2 , I tried to do it using reinterpret_cast as below,

 SecureVector<byte> salt2 = reinterpret_cast<byte> (salt);

which compiles without errors but returns a empty string when try to display as i displayed salt above. what am i doing wrong or how to do the conversion correctly. any help or advice will be highly appreciated.

danial weaber
  • 846
  • 2
  • 17
  • 37
  • 1
    possible duplicate of [C++ - pointer array to Vector?](http://stackoverflow.com/questions/7278347/c-pointer-array-to-vector) – Jack May 17 '15 at 13:43
  • Which constructor `SecureVector` has ? I expected one similar to `salt2(salt, salt +16)` – Jarod42 May 17 '15 at 13:49
  • Do you have any documentation for `SecureVector` I have no idea what that is. So it is impossible to tell what it is expecting for an assignment or constructor. – Martin York May 17 '15 at 14:35
  • @LokiAstari documentation is found here. `http://fossies.org/dox/Botan-1.10.9/classBotan_1_1SecureVector.html` but I have no idea about doing the conversation. thanks in any help. – danial weaber May 17 '15 at 16:01

3 Answers3

8

reinterpret_cast doesn't magically convert one type to another, even if it appears to do so. Frankly, unless and until you understand what it does do, you should never use it.

To make a vector contain the bytes from an array, create the vector and then add the bytes to it. You can't do this using a cast.

SecureVector<byte> salt2(salt, salt + 16);
davmac
  • 20,150
  • 1
  • 40
  • 68
1

It's ugly, but due to the type conversion you may have to just do a for loop here:

for(int i = 0; i < 16; ++i)
    salt2.push_back(reinterpret_cast<byte>(salt[i]));

I don't think casting like that can work because a vector isn't laid out the same way an array is in memory, it has to contain other information like its size.

IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17
  • If `byte` is a typedef of `char` or `unsigned char` (or, I think, if `char` is convertible to `byte`) - which seems a reasonable assumption - then the loop isn't necessary. You can just use the appropriate vector constructor. – davmac May 17 '15 at 14:01
  • @davmac I was considering adding that, I think you are right that it's a better solution if it works. – IllusiveBrian May 17 '15 at 14:04
  • thanks Namfuak, this also didnt work either. this returned an compile time error `C2664: 'Botan::SecureVector::SecureVector(const Botan::SecureVector` – danial weaber May 17 '15 at 15:57
  • 1
    @danialweaber `Botan::byte` appears to be a typedef for `uint_8` according to their documentation, so you may need to convert to unsigned char first (or use a C-style cast). – IllusiveBrian May 17 '15 at 16:06
1

The problem here is your assignment:

SecureVector<byte> salt2 = reinterpret_cast<byte>(salt);

You are converting the char* into a byte (So a pointer is being converted to a byte (I assume you meant to convert it to a byte* (note the extra *) but that did not compile so you took off the * to see what would hapen)). What this does is undefined (if not a very bad idea). But You have a byte.

But it compiles because SecureVector<byte> has a constructor that takes a size_t as a parameter. A size_t is an integer as is a byte and thus the compiler generated an auto conversion and constructed you vector using the byte as a size.

What you actually want to do is use the constructor that takes a pointer to byte and a size. see: SecureVector.

SecureVector<byte> salt2(reinterpret_cast<byte*>(salt), 16);
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • You said exactly what i did. Now i got the mistake what i did and got a good idea about what was going on. Why it complied but didnt work... Ect. Thanks again, this answer helped me a lot – danial weaber May 17 '15 at 17:32