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.

4
  • 1
    possible duplicate of C++ - pointer array to Vector? Commented May 17, 2015 at 13:43
  • Which constructor SecureVector<byte> has ? I expected one similar to salt2(salt, salt +16) Commented May 17, 2015 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. Commented May 17, 2015 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. Commented May 17, 2015 at 16:01

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer, but it didn't work it now returns an error in compile C2664: 'Botan::SecureVector<Botan::byte>::SecureVector(const Botan::SecureVector<Botan::byte>
I solved the question by adding the line, SecureVector<byte> salt2((const byte*)salt, 16 ) ;
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);

1 Comment

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
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.

4 Comments

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 I was considering adding that, I think you are right that it's a better solution if it works.
thanks Namfuak, this also didnt work either. this returned an compile time error C2664: 'Botan::SecureVector<Botan::byte>::SecureVector(const Botan::SecureVector<Botan::byte>
@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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.