如何在 web 中使用 crypto++ 代码

How to use crypto++ code in web

我在 crypto++ 库中下载了示例代码。我测试了该代码,结果是 returns 一组十六进制代码。我想将用户 ID 作为加密文本发送到浏览器。当我收到查看特定用户详细信息的请求时,我需要解密请求文本并找到用户 ID 并处理该用户的数据并将响应发送到浏览器。但是当我使用下面的代码时,我得到了一组 HEX 值的 chipper 文本。我希望此 chipper 文本与 SHA 值一样,长度约为 20 到 25 个字符串。如何更改 SHA 值等 chipper 文本。

 //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 

But I want chipper text as kdHkekdKLI!kdheGHWewqef

我想你想要一个 Base64Encoder or 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)));

您也可以使用 PutGet 手动完成,后者更符合 C 风格。在引擎盖下,这就是管道为您所做的(有些人放弃了):

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)));