-2

I am very new in c++ and want to cast a char* from a std::string to a byte*.

Here is my code:

inline string XOR(const string &value, const string &key) {
  string retval(value);
  CryptoPP::xorbuf(&retval[0], &key[0], retval.length());
  return retval;
}

In g++, the output is:

AESXCBC128.cpp: In function ‘std::string CryptoPP::XOR(const string&, const string&)’:
AESXCBC128.cpp:79:48: error: invalid conversion from ‘char*’ to ‘byte* {aka unsigned char*}’ [-fpermissive]
     xorbuf(&retval[0], &key[0], retval.length());
                                                ^
AESXCBC128.cpp:45:6: error:   initializing argument 1 of ‘void CryptoPP::xorbuf(byte*, const byte*, size_t)’ [-fpermissive]
 void xorbuf(byte *buf, const byte *mask, size_t count)
      ^
AESXCBC128.cpp:79:48: error: invalid conversion from ‘const char*’ to ‘const byte* {aka const unsigned char*}’ [-fpermissive]
     xorbuf(&retval[0], &key[0], retval.length());
                                                ^
AESXCBC128.cpp:45:6: error:   initializing argument 2 of ‘void CryptoPP::xorbuf(byte*, const byte*, size_t)’ [-fpermissive]
 void xorbuf(byte *buf, const byte *mask, size_t count)
jww
  • 97,681
  • 90
  • 411
  • 885
T. Rifle
  • 55
  • 1
  • 8
  • a char is not a string in c++ – user Feb 25 '17 at 12:14
  • It seems like you are using nonstandard C++, or you copied a Java code. C++ doesn't have type `byte`, `word32`, or `word64`. – Dannyu NDos Feb 25 '17 at 12:15
  • 1
    @DannyuNDos - I bet they are just typedefs, which is part of the standard. The real problem here is that `byte` is `unsigned char` and the strings contain `char`, a different type. The compiler also says that adding an `-fpermissive` option will make it ignore the error. Could be a temporary solution. – Bo Persson Feb 25 '17 at 12:17
  • 1
    Now available on the Crypto++ wiki: [`SecBlock`](https://www.cryptopp.com/wiki/SecBlock). The wiki page includes conversion examples. – jww Mar 23 '18 at 19:22

1 Answers1

0
inline string XOR(const string &value, const string &key) {
  string retval(value);
  CryptoPP::xorbuf(&retval[0], &key[0], retval.length());
  return retval;
}

Crypto++ typedefs a byte in confg.h:

typedef unsigned char byte;

You can use something like:

CryptoPP::xorbuf(
    reinterpret_cast<byte*>(&retval[0]),
    reinterpret_cast<const byte*>(&key[0]),
    retval.length());

Or, you can do it with C-style casts:

CryptoPP::xorbuf((byte*)&retval[0], (const byte*)&key[0], retval.length());

Here are some similar questions for SecByteBlock, which is a byte array rather than a char array:

Here are some references on C++ casting:

Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885
  • @T.Rifle - Maybe you should checkout [some search results](https://www.google.com/search?q=c%2B%2B+for+beginners) to help with future questions. Stack Overflow is not the easiest forum for those new to a language. Its kind of a well known problem with Stack Overflow. Also see [Could we please be a bit nicer to new users?](http://meta.stackexchange.com/questions/9953/could-we-please-be-a-bit-nicer-to-new-users). – jww Feb 26 '17 at 20:34