我在crypto ++库中下载了示例代码。我测试了该代码,结果返回了一组HEX代码。我想将用户ID作为加密文本发送到浏览器。当我收到查看特定用户详细信息的请求时,我需要解密请求文本并找到用户ID并处理该用户的数据并将响应发送到浏览器。但是当我使用下面的代码时,我将chipper文本作为一组HEX值。我希望这个削片机文本像SHA值一样大约20到25个字符串长度。如何更改削片器文本,如SHA值。
//Key and IV setup
//AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-
//bit). This key is secretly exchanged between two parties before communication
//begins. DEFAULT_KEYLENGTH= 16 bytes
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
//
// String and Sink setup
//
std::string plaintext = "Now is the time for all good men to come to the aide...";
std::string ciphertext;
std::string decryptedtext;
//
// Dump Plain Text
//
std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
std::cout << plaintext;
std::cout << std::endl << std::endl;
//
// Create Cipher Text
//
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();
//
// Dump Cipher Text
//
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
for( int i = 0; i < ciphertext.size(); i++ ) {
std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}
std::cout << std::endl << std::endl;
//
// Decrypt
//
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();
//
// Dump Decrypted Text
//
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;
输出:
Plain Text (55 bytes)
Now is the time for all good men to come to the aide...
Cipher Text (64 bytes)
0x7f 0xf8 0xb3 0xea 0x8a 0x2 0xb3 0x7a 0x3d 0x28 0x66 0x9c 0x97 0x13 0xa7 0xb3 0xf 0xa2 0x50 0x25 0x80 0xd5 0xd2 0x32 0xce 0xe8 0xa 0x57 0x33 0xef 0x70 0xff 0x48 0xe9 0xe8 0x4 0x98 0xa9 0x4 0xc2 0x5e 0xa7 0xb0 0x40 0x43 0xa1 0xfc 0x23 0xb1 0xa1 0xeb 0x1e 0xb2 0xf6 0x97 0x62 0x70 0xa1 0x81 0xca 0x6e 0x78 0x80 0x90
Decrypted Text:
Now is the time for all good men to come to the aide...
但我想要削片机文本,
kdHkekdKLI!kdheGHWewqef
答案 0 :(得分:1)
但我希望削片机文字为
kdHkekdKLI!kdheGHWewqef
我想你要Base64Encoder或Base64URLEncoder。我不清楚你想要哪个字母,因为它们非常相似。
您可以更改以下内容:
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
for( int i = 0; i < ciphertext.size(); i++ )
{
std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}
使用Pipeline进行以下操作。管道允许数据从源流到接收器。
std::string encoded;
StringSource ss(ciphertext, true, new Base64Encoder(new StringSink(encoded)));
您也可以使用Put
和Get
手动执行此操作,这更多是C-ish。在引擎盖下,这就是管道为你做的事情(放弃一些手):
std::string encoded;
Base64Encoder encoder;
encoder.Put((const byte*)ciphertext.data(), ciphertext.size());
encoder.MessageEnd();
size_t ready = encoder.MaxRetrievable();
if (ready)
{
encoded.resize(ready);
encoder.Get((byte*)&encoded[0], ready);
}
std::cout << encoded << std::endl;
最后,您可以使用以下内容在StreamTransformationFilter
管道中执行base64编码:
StreamTransformationFilter stfEncryptor(cbcEncryption, new Base64Encoder(new StringSink(ciphertext)));