1

I'm doing this assignment and i came across this problem where if i input a sentence, eg: "Hello my name is Adam", it will only encrypt "Hello".. But when i assign the sentence to the plaintext, I get a full encrypted/decrypted sentence.

AutoSeededRandomPool rnd;

// Generate a random key
SecByteBlock key(0x00, AES::DEFAULT_KEYLENGTH);
rnd.GenerateBlock( key, key.size() );

// Generate a random IV
SecByteBlock iv(AES::BLOCKSIZE);
rnd.GenerateBlock(iv, iv.size());

// Read phrase
std::string plaintext;
cin >> plaintext;

std::string ciphertext;
std::string decryptedtext;

// encrypt
CTR_Mode<AES>::Encryption cfbEncryption(key, key.size(), iv);
CryptoPP::StreamTransformationFilter stfEncryptor(cfbEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();

cout << ciphertext << endl;

// decrypt
CTR_Mode<AES>::Decryption cfbDecryption(key, key.size(), iv);
CryptoPP::StreamTransformationFilter stfDecryptor(cfbDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();

cout << decryptedtext << endl;

Can someone explain..? I don't quite understand how this works... I need user input for this assignment. Is there a way where i can have user input and get the full encrypted sentence?

jww
  • 97,681
  • 90
  • 411
  • 885
Adam Chua
  • 29
  • 5
  • 1
    Possible duplicate of [What's the difference between getline and std::istream::operator>>()?](https://stackoverflow.com/q/30005015/608639) – jww Jun 04 '18 at 02:02
  • Related, the [Crypto++ wiki](https://www.cryptopp.com/wiki/) has lots of examples. It will show you how to [print ciphertext](https://www.cryptopp.com/wiki/HexEncoder) (instead of `cout << ciphertext << endl;`). – jww Jun 04 '18 at 02:07

1 Answers1

3

Instead of cin >> plaintext, try std::getline(cin, plaintext).

operator>> in this context will read until the next whitespace, which in this case is the space character after "Hello". On the other hand, std::getline will read until the specified delimiter; here we are using the default delimiter which is \n, so it will read the entire line.

jcai
  • 3,448
  • 3
  • 21
  • 36