В настоящее время я пишу программу шифрования/дешифрования blowfish в C++ (используя crypto ++).строка для преобразования SecByteBlock
Я действительно не нашел удовлетворительного ответа в google. Я пытаюсь отправить ключ SecByteBlock в виде строки, а затем получить в другой части в виде строки, после чего необходимо восстановить его в SecByteBlock.
Можно ли преобразовать строку < -> SecByteBlock
Могу ли я сделать что-то лучше, чтобы отправить ключ от одной функции к другой?
Благодарим вас за помощь.
код я использую следующее:
int main(int argc, char* argv[])
{
AutoSeededRandomPool prng;
SecByteBlock key(Blowfish::DEFAULT_KEYLENGTH);
prng.GenerateBlock(key, key.size());
byte iv[Blowfish::BLOCKSIZE];
prng.GenerateBlock(iv, sizeof(iv));
BLOCK test = ReadStaticBlocks("testBin.dat");
string plain=test.bl1 ;
string cipher, encoded, recovered;
/*********************************\
\*********************************/
// Pretty print key
encoded.clear();
StringSource ss1(key, key.size(), true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
cout << "key: " << encoded << endl;
// Pretty print iv
encoded.clear();
StringSource ss2(iv, sizeof(iv), true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
cout << "iv: " << encoded << endl;
/*********************************\
\*********************************/
try
{
cout << "plain text: " << plain << endl;
CBC_Mode<Blowfish>::Encryption e;
e.SetKeyWithIV(key, key.size(), iv);
// The StreamTransformationFilter adds padding
// as required. ECB and CBC Mode must be padded
// to the block size of the cipher.
StringSource ss3(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 ss4(cipher, true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
cout << "cipher text: " << encoded << endl;
/*********************************\
\*********************************/
try
{
CBC_Mode<Blowfish>::Decryption d;
d.SetKeyWithIV(key, key.size(), iv);
// The StreamTransformationFilter removes
// padding as required.
StringSource ss5(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);
}
/*********************************\
\*********************************/
system("pause");
return 0;
}
Спасибо за ваш ответ. Однако либо вы не понимаете мой вопрос, либо, может быть, это я, который не понимает ваш ответ. На самом деле я хочу отправить send ключ SecByteBlock (Blowfish :: DEFAULT_KEYLENGTH); от одной функции к другой. – xtensa1408
@SADOK - Возможно, я неправильно понял вопрос в заголовке: * "string to SecByteBlock conversion" *; и вопрос в теле: * «Возможно ли преобразовать строку <--> SecByteBlock» *. – jww
'SecByteBlock b1 (s1.data(), s1.size());' не работает для меня ... _invalid преобразование из 'const char *' в 'const unsigned char * _. Что вы подразумеваете под 'cast from char * и byte *'? – EisenHeim