1

how to convert 'const char *' to 'const unsigned char *'

 bool AES(const uint8_t *Data, size_t Size) {  
     std::string s(reinterpret_cast<const char *>(Data), Size);
     AES_cbc_encrypt(s.c_str(), enc_out, Size, &enc_key, iv, AES_ENCRYPT);
    }

the error got while compilation of the program

candidate function not viable: no known conversion from 'const char *' to 'const unsigned char *'
      for 1st argument
void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,

I tried this doesn't work

std::string s(reinterpret_cast<const unsigned char *>(Data), Size);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
anish
  • 6,884
  • 13
  • 74
  • 140
  • 6
    Why aren't you just passing `Data` to `AES_cbc_encrypt`? – Sneftel Feb 17 '17 at 16:46
  • `std::string` is `std::basic_string`, so you probably need `std::basic_string`, but anyway, why do you need temporary string object? – myaut Feb 17 '17 at 16:50
  • Awesome This work like a Cart @Sneftel – anish Feb 17 '17 at 16:50
  • @myaut Is `basic_string` guaranteed to exist though? As far as I remember the standard only requires `basic_string` specializations for `char`, `wchar_t`, `char16_t` and `char32_t`. I'll have to check. – DeiDei Feb 17 '17 at 16:55
  • @DeiDei: why not? `std::string` is not specialization but a typedef for `std::basic_string`. You can miss some overloads for `std::hash` or `operator <<`, though – myaut Feb 17 '17 at 17:01
  • CHAR_BIT is at least 8 and char has to be the narrowest unit for a implementation. This means when `uint8_t` is available, it must be an alias to `unsigned char`. (If char is wider than 8 bit, them `uint8_t` won't be provided) – user3528438 Feb 17 '17 at 17:02
  • 1
    `std::vector` has more appropriate semantics for this than `std::basic_string`. Of course, in this case you don't need a container class at all, see the answer by @gurka. – Christian Hackl Feb 17 '17 at 17:03

1 Answers1

1

uint8_t, if it's available, is just a typedef to unsigned char. Therefore, you can just do:

 bool AES(const uint8_t *Data, size_t Size) {  
     AES_cbc_encrypt(Data, enc_out, Size, &enc_key, iv, AES_ENCRYPT);
 }

Note that you should probably return something in your function as well.

Community
  • 1
  • 1
simon
  • 2,042
  • 2
  • 20
  • 31