0

I am using the EVP Symmetric Encryption and Decryption algorithm to encrypt and decrypt a file of text.

The encryption works file, a new encrypted file is generated, but when I try to decrypt the file back it always crashes when EVP_DecryptFinal_ex is called the first time.

I am using two Visual Studio projects: one for encryption and one for decryption.
The libraries I am using I presumed they are build in DEBUG mode (because they have the .pdb files), so that is how my project is also build. (if I choose release mode, the compiler cannot find the openssl include header anymore).

This is the error I get:

digital envelope routines:EVP_DecryptFinal_ex:wrong final block length

I am using the C++11 version, here is my code:

void Cipher::Encrypt(const byte key[KEY_SIZE], const byte iv[BLOCK_SIZE], const secure_string& ptext, secure_string& ctext) {
    EVP_CIPHER_CTX_free_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);
    int rc = EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL, key, iv);
    if (rc != 1)
        throw std::runtime_error("EVP_EncryptInit_ex failed");

    // Recovered text expands upto BLOCK_SIZE
    ctext.resize(ptext.size() + BLOCK_SIZE);
    int out_len1 = (int)ctext.size();

    rc = EVP_EncryptUpdate(ctx.get(), (byte*)&ctext[0], &out_len1, (const byte*)&ptext[0], (int)ptext.size());
    if (rc != 1)
        throw std::runtime_error("EVP_EncryptUpdate failed");

    int out_len2 = (int)ctext.size() - out_len1;
    rc = EVP_EncryptFinal_ex(ctx.get(), (byte*)&ctext[0] + out_len1, &out_len2);
    if (rc != 1)
        throw std::runtime_error("EVP_EncryptFinal_ex failed");

    // Set cipher text size now that we know it
    ctext.resize(out_len1 + out_len2);
}

void Cipher::Decrypt(const byte key[KEY_SIZE], const byte iv[BLOCK_SIZE], const secure_string& ctext, secure_string& rtext) {
    EVP_CIPHER_CTX_free_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);
    int rc = EVP_DecryptInit_ex(ctx.get(), EVP_aes_256_cbc(), NULL, key, iv);
    if (rc != 1)
        throw std::runtime_error("EVP_DecryptInit_ex failed");

    // Recovered text contracts upto BLOCK_SIZEB
    rtext.resize(ctext.size());
    int out_len1 = (int)rtext.size();

    rc = EVP_DecryptUpdate(ctx.get(), (byte*)&rtext[0], &out_len1, (const byte*)&ctext[0], (int)ctext.size());
    if (rc != 1)
        throw std::runtime_error("EVP_DecryptUpdate failed");

    int out_len2 = (int)rtext.size() - out_len1;
    rc = EVP_DecryptFinal_ex(ctx.get(), (byte*)&rtext[0] + out_len1, &out_len2);
    if (rc != 1) {
        ERR_print_errors_fp(stderr);
        throw std::runtime_error("EVP_DecryptFinal_ex failed");
    }

    // Set recovered text size now that we know it
    rtext.resize(out_len1 + out_len2);
}
int main(int argc, char* argv[])
{
    // Load the necessary cipher
    EVP_add_cipher(EVP_aes_256_cbc());

    // create Cipher object
    Cipher cipher;
    ifstream f("d:/temp.YML");
    ofstream out("d:/tempDecrypt.YML");

    byte key[KEY_SIZE] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2};
    byte iv[BLOCK_SIZE] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
    //cipher.gen_params(key, iv);

    secure_string line;
    secure_string temp;
    while (getline(f, line)) {
        cipher.Decrypt(key, iv, line, temp);
        std::cout << temp << std::endl;
        out << temp;
    }

    OPENSSL_cleanse(key, KEY_SIZE);
    OPENSSL_cleanse(iv, BLOCK_SIZE);

    return 0;
}

I also read that it could be a padding issue, not sure if that is the case and what should I do. I am not that good with encryption.

Any pointers on how to proceed further would be welcomed. If you need more information let me know.

Adrian
  • 16,820
  • 32
  • 99
  • 188
  • `while (getline(f, line))` - The file being decrypted is *not* ASCII; it is binary. – jww Apr 05 '19 at 13:27
  • @jww: not sure how do I read a binary file so I can decrypted ? read a specific number of bytes ? – Adrian Apr 05 '19 at 13:32
  • [Reading and writing binary file](https://stackoverflow.com/q/5420317/608639). You may have a similar problem when writing the file. We can't say for certain because the code to write the file is not present. ([c++ read and write binary file site:stackoverflow.com](https://duckduckgo.com/?q=c%2B%2B+read+and+write+binary+file+site%3Astackoverflow.com) may be helpful). – jww Apr 05 '19 at 13:40
  • @jww: yeah I was reading that. Thanks, I will try. – Adrian Apr 05 '19 at 13:40

0 Answers0