字符串转换为SecByteBlock [英] string to SecByteBlock conversion
问题描述
我目前正在C ++中使用cryptofish加密/解密程序(使用crypto ++)。
我真的没有在google找到满意的答案。我试图发送一个SecByteBlock的密钥作为字符串,然后在另一部分作为字符串接收,然后需要重新获得SecByteBlock。
是可能的convert string< - > SecByteBlock
我可以更好地将密钥从一个函数发送到另一个函数吗?
我使用的代码如下:
int main(int argc,char * argv [])
{
AutoSeededRandomPool prng;
SecByteBlock键(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;
/ ********************************* \
\ ********************************* /
//漂亮的打印键
encoded.clear();
StringSource ss1(key,key.size(),true,
new HexEncoder(
new StringSink(encoded)
)// HexEncoder
); // StringSource
cout<< key:<<编码的< endl;
//漂亮的打印iv
encoded.clear();
StringSource ss2(iv,sizeof(iv),true,
new HexEncoder(
new StringSink(encoded)
)// HexEncoder
); // StringSource
cout<< iv:<编码的< endl;
/ ********************************* \
\ ********************************* /
try
{
cout<< plain text:<< plain<< endl;
CBC_Mode< Blowfish> :: Encryption e;
e.SetKeyWithIV(key,key.size(),iv);
// StreamTransformationFilter根据需要添加填充
//。 ECB和CBC模式必须填充
//到密码的块大小。
StringSource ss3(plain,true,
new StreamTransformationFilter(e,
new StringSink(cipher)
)// StreamTransformationFilter
); // StringSource
}
catch(const CryptoPP :: Exception& e)
{
cerr< e.what()< endl;
exit(1);
}
/ ********************************* \
\ ********************************* /
//漂亮print
encoded.clear();
StringSource ss4(cipher,true,
new HexEncoder(
new StringSink(encoded)
)// HexEncoder
); // StringSource
cout<< cipher text:<编码的< endl;
/ ********************************* \
\ ********************************* /
try
{
CBC_Mode< Blowfish> :: Decryption d;
d.SetKeyWithIV(key,key.size(),iv);
// StreamTransformationFilter根据需要删除
// padding。
StringSource ss5(cipher,true,
new StreamTransformationFilter(d,
new StringSink(restored)
)// StreamTransformationFilter
); // StringSource
cout<< recover text:<回收< endl;
}
catch(const CryptoPP :: Exception& e)
{
cerr< e.what()< endl;
exit(1);
}
/ ********************************* \
\ ********************************* /
系统(暂停);
return 0;
}
是否可以转换字符串< - > SecByteBlock
是的。每个都有一个构造函数,它接受指针和长度。
string s1(hello world);
SecByteBlock b1(s1.data(),s1.size());
和
byte a [] = {'h','e','l','l','o','','w','o','r','l' d'};
SecByteBlock b2(a,sizeof(a));
string s2(b2.data(),b2.size());
您可能需要在 char *
.cryptopp.com / docs / ref / class_string_source.htmlrel =nofollow> StringSource
还有一个构造函数,它接受指针和长度。所以你可以执行:
byte a [] = {'h','e','l','l' ,'o','','w','o','r','l','d'};
SecByteBlock b3(a,sizeof(a));
StringSource ss1(b3.data(),b3.size(),true ...);
或更直接:
byte a [] = {'h','e','l','l','o','','w','o','r' l','d'};
StringSource ss1(a,sizeof(a),true ...);
I'm currently writing blowfish encryption/decryprion program in C++ (using crypto++).
I really didn't find a satisfied answer at google. I'm trying to send a key of a SecByteBlock as a string and then received in the other part as string then need to be regained to SecByteBlock.
Is it possible to convert string <--> SecByteBlock
Can I do something better to send key from one function to another?
Thank you for help in advance.
The code I use is the following:
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;
}
Is it possible to convert string <--> SecByteBlock
Yes. Each has a constructor that takes a pointer and a length.
string s1("hello world");
SecByteBlock b1(s1.data(), s1.size());
And
byte a[] = {'h','e','l','l','o',' ','w','o','r','l','d'};
SecByteBlock b2(a, sizeof(a));
string s2(b2.data(), b2.size());
You might need to cast between char*
and byte*
, however.
And in your case, the StringSource
also has a constructor that takes a pointer and a length. So you could perform:
byte a[] = {'h','e','l','l','o',' ','w','o','r','l','d'};
SecByteBlock b3(a, sizeof(a));
StringSource ss1(b3.data(), b3.size(), true ...);
Or even more directly:
byte a[] = {'h','e','l','l','o',' ','w','o','r','l','d'};
StringSource ss1(a, sizeof(a), true ...);
这篇关于字符串转换为SecByteBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!