error: invalid conversion from ‘const char*’ to ‘const byte*’

1,009 views
Skip to first unread message

Alvaro Leiva

unread,
Dec 7, 2012, 5:18:31 PM12/7/12
to cryptop...@googlegroups.com
I have been trying to use the HexDecoder as it shows: http://www.cryptopp.com/wiki/HexDecoder to decode the key and iv in a usefull manner to be compatible with: .SetKeyWithIV(), so far I have tried a bounch of things with no luck, here is my failed implementation:

#include "osrng.h"
using CryptoPP::AutoSeededRandomPool;

#include <iostream>
using std::cout;
using std::cerr;
using std::endl;

#include <string>
using std::string;

#include <cstdlib>
using std::exit;

#include "cryptlib.h"
using CryptoPP::Exception;

#include "hex.h"
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;

#include "filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::StreamTransformationFilter;

#include "aes.h"
using CryptoPP::AES;

#include "ccm.h"
using CryptoPP::CTR_Mode;

#include "assert.h"


void CharToByte(char* chars, byte* bytes, unsigned int count){
    for(unsigned int i = 0; i < count; i++)
    bytes[i] = (byte)chars[i];
}

void ByteToChar(byte* bytes, char* chars, unsigned int count){
    for(unsigned int i = 0; i < count; i++)
    chars[i] = (char)bytes[i];
}


int main(int argc, char* argv[])
{
AutoSeededRandomPool prng;

byte key[AES::DEFAULT_KEYLENGTH];
prng.GenerateBlock(key, sizeof(key));

byte iv[AES::BLOCKSIZE];
prng.GenerateBlock(iv, sizeof(iv));

string plain = "CTR Mode Test";
string cipher, encoded, recovered, skey, siv, fkey, fiv;
/*********************************\
\*********************************/

// Pretty print key
encoded.clear();
StringSource(key, sizeof(key), true,
new HexEncoder(
new StringSink(skey)
) // HexEncoder
); // StringSource
cout << "key: " << skey << endl;

// Pretty print iv
encoded.clear();
StringSource(iv, sizeof(iv), true,
new HexEncoder(
new StringSink(siv)
) // HexEncoder
); // StringSource
cout << "iv: " << siv << endl;

/*********************************\
\*********************************/



    StringSource ss1(skey, true /*pumpAll*/,
        new HexDecoder(
            new StringSink(fkey)
        ) // HexDecoder
    ); // StringSource

    StringSource ss2(siv, true /*pumpAll*/,
        new HexDecoder(
            new StringSink(fiv)
        ) // HexDecoder
    ); // StringSource



try
{





cout << "plain text: " << plain << endl;

CTR_Mode< AES >::Encryption e;
e.SetKeyWithIV(fkey.data(), fkey.size(), fiv.data());

// The StreamTransformationFilter adds padding
//  as required. ECB and CBC Mode must be padded
//  to the block size of the cipher.
StringSource(plain, true, 
new StreamTransformationFilter(e,
new StringSink(cipher)
) // StreamTransformationFilter      
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
exit(1);
}

/*********************************\
\*********************************/

// Pretty print
encoded.clear();
StringSource(cipher, true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
cout << "cipher text: " << encoded << endl;

/*********************************\
\*********************************/

try
{
CTR_Mode< AES >::Decryption d;
d.SetKeyWithIV(key, sizeof(key), iv);

// The StreamTransformationFilter removes
//  padding as required.
StringSource s(cipher, true, 
new StreamTransformationFilter(d,
new StringSink(recovered)
) // StreamTransformationFilter
); // StringSource

cout << "recovered text: " << recovered << endl;
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
exit(1);
}

/*********************************\
\*********************************/

return 0;
}


the g++ compiler returns:

Driver.cpp: In function ‘int main(int, char**)’:
Driver.cpp:112: error: invalid conversion from ‘const char*’ to ‘const byte*’
Driver.cpp:112: error:   initializing argument 1 of ‘void CryptoPP::SimpleKeyingInterface::SetKeyWithIV(const byte*, size_t, const byte*)’
Driver.cpp:112: error: invalid conversion from ‘const char*’ to ‘const byte*’
Driver.cpp:112: error:   initializing argument 3 of ‘void CryptoPP::SimpleKeyingInterface::SetKeyWithIV(const byte*, size_t, const byte*)’

thank u 4 your time !!!

Alvaro Leiva

unread,
Dec 7, 2012, 6:42:29 PM12/7/12
to cryptop...@googlegroups.com
its byte data in char data type !?
how i transform it to byte data type ?

Fraser Hutchison

unread,
Dec 8, 2012, 8:36:08 AM12/8/12
to Alvaro Leiva, cryptop...@googlegroups.com
You need to reinterpret_cast const char* to const byte* :-

e.SetKeyWithIV(reinterpret_cast<const byte*>(fkey.data()),
�������������� fkey.size(),
�������������� reinterpret_cast<const byte*>(fiv.data()));

Or you can use the byte arrays from which the string versions were created:-

e.SetKeyWithIV(key, sizeof(key), iv);

Cheers,
Fraser.



On 07/12/2012 23:42, Alvaro Leiva wrote:
its byte data in char data type !?
how i transform it to byte data type ?

El viernes, 7 de diciembre de 2012 19:18:31 UTC-3, Alvaro Leiva escribi�:
I have been trying to use the HexDecoder as it shows:�http://www.cryptopp.com/wiki/HexDecoder�to decode the key and iv in a usefull manner to be compatible with:�.SetKeyWithIV(), so far I have tried a bounch of things with no luck, here is my failed implementation:
� � for(unsigned int i = 0; i < count; i++)
� � bytes[i] = (byte)chars[i];
}

void ByteToChar(byte* bytes, char* chars, unsigned int count){
� � for(unsigned int i = 0; i < count; i++)
� � chars[i] = (char)bytes[i];
� � StringSource ss1(skey, true /*pumpAll*/,
� � � � new HexDecoder(
� � � � � � new StringSink(fkey)
� � � � ) // HexDecoder
� � ); // StringSource

� � StringSource ss2(siv, true /*pumpAll*/,
� � � � new HexDecoder(
� � � � � � new StringSink(fiv)
� � � � ) // HexDecoder
� � ); // StringSource



try
{





cout << "plain text: " << plain << endl;

CTR_Mode< AES >::Encryption e;
e.SetKeyWithIV(fkey.data(), fkey.size(), fiv.data());

// The StreamTransformationFilter adds padding
// �as required. ECB and CBC Mode must be padded
// �to the block size of the cipher.
StringSource(plain, true,�
new StreamTransformationFilter(e,
new StringSink(cipher)
) // StreamTransformationFilter � � �
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
exit(1);
}

/*********************************\
\*********************************/

// Pretty print
encoded.clear();
StringSource(cipher, true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
cout << "cipher text: " << encoded << endl;

/*********************************\
\*********************************/

try
{
CTR_Mode< AES >::Decryption d;
d.SetKeyWithIV(key, sizeof(key), iv);

// The StreamTransformationFilter removes
// �padding as required.
StringSource s(cipher, true,�
new StreamTransformationFilter(d,
new StringSink(recovered)
) // StreamTransformationFilter
); // StringSource

cout << "recovered text: " << recovered << endl;
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
exit(1);
}

/*********************************\
\*********************************/

return 0;
}


the g++ compiler returns:

Driver.cpp: In function �int main(int, char**)�:
Driver.cpp:112: error: invalid conversion from �const char*� to �const byte*�
Driver.cpp:112: error: � initializing argument 1 of �void CryptoPP::SimpleKeyingInterface::SetKeyWithIV(const byte*, size_t, const byte*)�
Driver.cpp:112: error: invalid conversion from �const char*� to �const byte*�
Driver.cpp:112: error: � initializing argument 3 of �void CryptoPP::SimpleKeyingInterface::SetKeyWithIV(const byte*, size_t, const byte*)�

thank u 4 your time !!!
--
You received this message because you are subscribed to the "Crypto++ Users" Google Group.
To unsubscribe, send an email to cryptopp-user...@googlegroups.com.
More information about Crypto++ and this group is available at http://www.cryptopp.com.

Jeffrey Walton

unread,
Dec 21, 2012, 6:54:05 PM12/21/12
to Crypto++ Users


On Dec 8, 8:36 am, Fraser Hutchison <fraser.hutchi...@gmail.com>
wrote:
> You need to reinterpret_cast const char* to const byte* :-e.SetKeyWithIV(reinterpret_cast<const byte*>(fkey.data()),fkey.size(),reinterpret_cast<const byte*>(fiv.data()));
> Or you can use the byte arrays from which the string versions were created:-e.SetKeyWithIV(key, sizeof(key), iv);
> Cheers,
I generally remove the casts in the sample code to help with code
readability.

The sample programs include the casts.

Jeff

> Fraser.On 07/12/2012 23:42, Alvaro Leiva wrote:
>
> its byte data in char data type !?
> how i transform it to byte data type ?
> El viernes, 7 de diciembre de 2012 19:18:31 UTC-3, Alvaro Leiva escribi :I have been trying to use the HexDecoder as it shows:http://www.cryptopp.com/wiki/HexDecoderto decode the key and iv in a usefull manner to be compatible with:.SetKeyWithIV(), so far I have tried a bounch of things with no luck, here is my failed implementation:#include "osrng.h"
>
> [SNIP]
Reply all
Reply to author
Forward
0 new messages