Shravan40
Shravan40

Reputation: 9908

StreamTransformationFilter: ciphertext length is not a multiple of block size?

I am trying to encrypt and decrypt a plain text using aes algorithm present in crypto++

Here is my encryption method

/*
 * Encrypt the given text
 */

template<typename T>
T encryptText(T plainText) {
    /* 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 );
    T cipherText;
    cout << "\n\nPlain Text size is (" << plainText.size() << " bytes)" << "\n\n";
    cout << plainText << "\n\n";
    /*
     * 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();
    cout << "\n\nCipher Text size is (" << cipherText.size() << " bytes)" << std::endl;
    for( int i = 0; i < cipherText.size(); i++ ) {
        cout << "0x" << std::hex << (0xFF & static_cast<byte>(cipherText[i])) << " ";
    }
    return cipherText;
}

Here is my decryption method

/*
 * Decrypt the given text
 */

template<typename T>
T decryptText(T encryptedText) {
    /* 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 );
    CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
    T decryptedText;
    CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedText ) );
    stfDecryptor.Put( reinterpret_cast<const unsigned char*>( encryptedText.c_str() ), encryptedText.size() );
    stfDecryptor.MessageEnd();
    std::cout << "\n\nDecrypted Text is " << "\n\n";
    std::cout << "\n\n" << decryptedText << "\n\n";
    return decryptedText;
}

encryptText function execute normally and i save the output of it in mysql database. While calling the decryptText function an error occurs saying

An uncaught exception occurred: StreamTransformationFilter: ciphertext length is not a multiple of block size

And i understand that this error is because of encrypted text may contain some null(0) value too. How decrypt the actual text.

Edit :

As i asked to show my sql queries in comment section.

Here is my sql query to fetch the saved encrypted data in mysql database.

template<typename T1, typename T2>
void getUserSms(T1 userId,vector<T1>& smsIds,vector<T2>& text){
    try{
        mysql::connection db_cs(config_cs_db());
        auto sms = db_cs.run(select(all_of(us)).from(us).where(us.usersUserId == userId));
        while(!sms.empty()){
            const auto& row = sms.front();
            auto smsId = row.id.value();
            smsIds.push_back(smsId);
            auto encryptedText = row.text.value();
            auto plainText = decryptText(encryptedText);
            text.push_back(plainText);
            sms.pop_front();
        }
        return;
    }
    catch (const sqlpp::exception& e) {
        std::cerr << "Could not get user sms due to some reason ...!!\n";
        std:cerr << e.what() << "\n";
        return;
    }
}

I am using sqlpp11 for sql queries. And the table description is

CREATE TABLE `user_sms`(
`id` BIGINT(50) NOT NULL AUTO_INCREMENT,
`sender_number` VARCHAR(20),
`text` VARCHAR(2000),
`incoming_time` TIMESTAMP,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`users_user_id` BIGINT(30),
PRIMARY KEY(`id`),
FOREIGN KEY(`users_user_id`) REFERENCES `users`(`user_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

Upvotes: 0

Views: 2383

Answers (1)

Shravan40
Shravan40

Reputation: 9908

  • Change the data type of array key and 'ivfrombytetounsigned char`
  • Change the text type in mysql database table user_sms from VARCHAR to BLOB.

CREATE TABLE `user_sms`( // Changes below is the changes has been made. `text` BLOB, )ENGINE=InnoDB DEFAULT CHARSET=utf8;

Thanks jww for help.

Upvotes: 1

Related Questions