StreamTransformationFilter: ciphertext length is not a multiple of block size

2,895 views
Skip to first unread message

starburst

unread,
Jun 9, 2011, 12:21:31 PM6/9/11
to Crypto++ Users
Hi everyone,

I am currently working on a program where i have one client and two
servers (one encryptor server and one decryptor server). The client
will ask the user for some data to encrypt (as a string and turn into
char with memcpy) and send the char buffer using sockets to the
encryptor server. Which encrypts the info and sends it back to the
client. The client then sends the encrypted data to the second server
which will decrypt it and send it back to the client.

So far, I can get the encrypted data to the 2nd server fine and
SOMETIMES, the decryptor server will give me this message: (other
times it will work perfectly fine to give the client back the
decrypted data - prob because the cliphertext length was good)

StreamTransformationFilter: ciphertext length is not a multiple of
block size

Please help me. Thanks in advance.









My decryption code is as follows:

int decrypt(char *pBuffer, int size, int &i)
{
//CONVERTING THE KEY FROM STRING TO BYTE

string stringKey = "2b7e151628aed2a6abf7158809cf4f3c";
int buf = stringKey.size()+1;
char pKey[TEMP_BUFFER_SIZE];
memset(pKey, 0, buf);
memcpy (pKey, stringKey.c_str(), stringKey.size()+1);

byte key[AES::DEFAULT_KEYLENGTH];
memset(key, 0, AES::DEFAULT_KEYLENGTH);
CharToByte(pKey, key, stringKey.size());

// "ECB Mode";
string encoded, recovered;

//pBUFFER HAS THE ENCRYPTED INFORMATION

string cipher = pBuffer;


try
{
ECB_Mode< AES >::Decryption d;
d.SetKey(key, sizeof(key));


// The StreamTransformationFilter removes
// padding as required.
StringSource s(cipher, true, new StreamTransformationFilter(d,new
StringSink(recovered), StreamTransformationFilter::DEFAULT_PADDING,
true
) // StreamTransformationFilter
); // StringSource

cout << "recovered text: " << recovered << endl;
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
exit(1);
}

int bufSz = recovered.size()+1;
memset(pBuffer, 0, bufSz);
memcpy (pBuffer, recovered.c_str(), recovered.size()+1);

//strcpy_s(pBuffer,bufSz,recovered.c_str());
cout << "pBuffer text: " << pBuffer << endl;

return recovered.size();

}

starburst

unread,
Jun 9, 2011, 2:17:42 PM6/9/11
to Crypto++ Users
I did some debugging and I think what is happening is that the
ciphertext changes sizes after being copied to a char and back to a
string multiple times (because if the cipher text has zeros in the
middle the rest of the data become invalid).

Can anyone point me to a better way of converting a char to a string
and vice versa. I've tried strcpy with no luck.

Jeffrey Walton

unread,
Jun 16, 2011, 1:59:55 AM6/16/11
to Crypto++ Users
Use an alternate std::string constructor (http://www.cplusplus.com/
reference/string/string/string/):
string cipher(pBuffer, nBufferSize);;
Reply all
Reply to author
Forward
0 new messages