0

    //Key and IV setup
    CryptoPP::byte key[32], iv[CryptoPP::AES::BLOCKSIZE];
    memset(key, 0x00, 32);
    memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);

    std::string plaintext;
    getline(std::cin, plaintext); //Input text to encrypt
    std::string ciphertext;
    std::string decryptedtext;

    std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
    std::cout << plaintext;
    std::cout << std::endl << std::endl;

    CryptoPP::AES::Encryption aesEncryption(key, 32);
    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());
    stfEncryptor.MessageEnd();

    std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
    std::string messy_text;
    for (int i = 0; i < ciphertext.size(); i++) {
        std::cout << "0x" << std::hex << (0xFF & static_cast<CryptoPP::byte>(ciphertext[i])) << " ";
    }

    std::cout << std::endl << std::endl;

    CryptoPP::AES::Decryption aesDercyption(key, 32);
    CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDercyption, iv);

    CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext));
    stfDecryptor.Put(reinterpret_cast<const unsigned char*>(ciphertext.c_str()), ciphertext.size());
    stfDecryptor.MessageEnd();

    std::cout << "Decrypted Text: " << std::endl;
    std::cout << decryptedtext;
    std::cout << std::endl << std::endl;

    return 0;

I used this code from here.

The above code accepts sentence or line of strings but when emojis are used it output question mark instead of emoji.

Output


this is a laughing emoji 

Plain Text (27 bytes)
this is a laughing emoji ??

Cipher Text (32 bytes)
0x87 0x22 0x50 0xe 0xee 0x1a 0x9d 0x1 0xc5 0x7f 0x78 0xe4 0x23 0xcc 0x89 0xc 0xa3 0xfb 0xf5 0x37 0x5f 0xc6 0xd7 0x3 0xf6 0x7c 0x10 0xa 0xcb 0xe5 0xd9 0x53

Decrypted Text:
this is a laughing emoji ??
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Aakash Gautam
  • 171
  • 1
  • 6
  • 2
    I'd say your encrypting and decrypting is OK, but the problem comes from `std::getline` not being able to read a Unicode character. Notice 1) the second output line already prints the `??`, and 2) the decrypted text is the same as the plain text. – rturrado Feb 24 '22 at 20:17
  • I'm guessing you are using windows? Getting full unicode support on the windows console is tricky – Alan Birtles Feb 24 '22 at 20:28

0 Answers0