OpenSSL Encryption (c++) EVP_DecryptFinal_ex detects different padding to applied EVP_EncryptFinal_ex padding

My C++ program which implements OpenSSL Encryption and Decryption has an inconsistency relating to the padding when I attempt to decrypt an encrypted file.

The encryption function is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const int Cipher::cIterations = 1000;
const int Cipher::cPassSizeBytes = 48;
const int Cipher::cOk = 1;
int Cipher::Encrypt(const string &plainText,
const string &passPhrase,
string &cipherText)
{
const int cKeySizeBytes = 32;
const int cSaltSizeBytes = 32;
const int cIvSizeBytes = 32;
try
{
string key(cKeySizeBytes, 0);
string salt(cSaltSizeBytes, 0);
string iv(cIvSizeBytes, 0);
string password(cPassSizeBytes, 0);
string encryptedStr(cipherText.size(), 0);
int actual_size = 0;
int final_size;
createRandString(salt);
createRandString(iv);
rfc2898DeriveBytes(passPhrase, salt, cIterations, key);
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(ctx);
if(cOk != EVP_EncryptInit_ex(ctx,
EVP_aes_256_cbc(),
NULL,
reinterpret_cast<unsigned char *>(&key[0]),
reinterpret_cast<unsigned char *>(&iv[0])))
{
handleErrors();
}
if(cOk != EVP_EncryptUpdate(ctx,
reinterpret_cast<unsigned char *>(&encryptedStr[0]),
&actual_size,
reinterpret_cast<const unsigned char *>(&plainText[0]),
plainText.size()))
{
handleErrors();
}
if(cOk != EVP_EncryptFinal_ex(ctx,
reinterpret_cast<unsigned char *>(&encryptedStr[actual_size]),
&final_size))
{
handleErrors();
}
actual_size += final_size;
encryptedStr.resize(actual_size);
cipherText = salt + iv + encryptedStr;
EVP_CIPHER_CTX_cleanup(ctx);
}
catch (std::runtime_error &ex)
{
throw ex;
}
catch (std::exception &ex)
{
std::cerr << ex.what() << std::endl;
}
return cipherText.length();
}
</code>
<code>const int Cipher::cIterations = 1000; const int Cipher::cPassSizeBytes = 48; const int Cipher::cOk = 1; int Cipher::Encrypt(const string &plainText, const string &passPhrase, string &cipherText) { const int cKeySizeBytes = 32; const int cSaltSizeBytes = 32; const int cIvSizeBytes = 32; try { string key(cKeySizeBytes, 0); string salt(cSaltSizeBytes, 0); string iv(cIvSizeBytes, 0); string password(cPassSizeBytes, 0); string encryptedStr(cipherText.size(), 0); int actual_size = 0; int final_size; createRandString(salt); createRandString(iv); rfc2898DeriveBytes(passPhrase, salt, cIterations, key); EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_CIPHER_CTX_init(ctx); if(cOk != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, reinterpret_cast<unsigned char *>(&key[0]), reinterpret_cast<unsigned char *>(&iv[0]))) { handleErrors(); } if(cOk != EVP_EncryptUpdate(ctx, reinterpret_cast<unsigned char *>(&encryptedStr[0]), &actual_size, reinterpret_cast<const unsigned char *>(&plainText[0]), plainText.size())) { handleErrors(); } if(cOk != EVP_EncryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(&encryptedStr[actual_size]), &final_size)) { handleErrors(); } actual_size += final_size; encryptedStr.resize(actual_size); cipherText = salt + iv + encryptedStr; EVP_CIPHER_CTX_cleanup(ctx); } catch (std::runtime_error &ex) { throw ex; } catch (std::exception &ex) { std::cerr << ex.what() << std::endl; } return cipherText.length(); } </code>
const int Cipher::cIterations = 1000;
const int Cipher::cPassSizeBytes = 48;
const int Cipher::cOk = 1;

int Cipher::Encrypt(const string &plainText,
                    const string &passPhrase,
                    string &cipherText)
{
    const int cKeySizeBytes = 32;
    const int cSaltSizeBytes = 32;
    const int cIvSizeBytes = 32;
    try
    {
        string key(cKeySizeBytes, 0);
        string salt(cSaltSizeBytes, 0);
        string iv(cIvSizeBytes, 0);
        string password(cPassSizeBytes, 0);
        string encryptedStr(cipherText.size(), 0);
        int actual_size = 0;
        int final_size;

        createRandString(salt);
        createRandString(iv);

        rfc2898DeriveBytes(passPhrase, salt, cIterations, key);

    
        EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
        EVP_CIPHER_CTX_init(ctx);

        if(cOk != EVP_EncryptInit_ex(ctx,
                                     EVP_aes_256_cbc(),
                                     NULL,
                                     reinterpret_cast<unsigned char *>(&key[0]),
                                     reinterpret_cast<unsigned char *>(&iv[0])))
        {
            handleErrors();
        }

        if(cOk != EVP_EncryptUpdate(ctx,
                                    reinterpret_cast<unsigned char *>(&encryptedStr[0]),
                                    &actual_size,
                                    reinterpret_cast<const unsigned char *>(&plainText[0]),
                                    plainText.size()))
        {
            handleErrors();
        }

        if(cOk != EVP_EncryptFinal_ex(ctx,
                                      reinterpret_cast<unsigned char *>(&encryptedStr[actual_size]),
                                      &final_size))
        {
            handleErrors();
        }

        actual_size += final_size;

        encryptedStr.resize(actual_size);

        cipherText = salt + iv + encryptedStr;

        EVP_CIPHER_CTX_cleanup(ctx);
    }
    catch (std::runtime_error &ex)
    {
        throw ex;
    }
    catch (std::exception &ex)
    {
        std::cerr << ex.what() << std::endl;
    }
    return cipherText.length();
}

And the decryption function is

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>int Cipher::Decrypt(const string &cipherText,
const string &passPhrase,
string &plainText)
{
const int cKeySizeBytes = 32;
const int cSaltSizeBytes = 32;
const int cIvSizeBytes = 32;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
int actual_size = 0;
int final_size = 0;
string key(cKeySizeBytes, 0);
string salt = cipherText.substr(0, cKeySizeBytes);
string iv = cipherText.substr(cKeySizeBytes, cIvSizeBytes);
try
{
rfc2898DeriveBytes(passPhrase, salt, cIterations, key);
EVP_CIPHER_CTX_init(ctx);
// Enc is 1 to encrypt, 0 to decrypt, or -1 (see documentation).
if(cOk != EVP_DecryptInit_ex(ctx,
EVP_aes_256_cbc(),
NULL,
reinterpret_cast<unsigned char *>(&key[0]),
reinterpret_cast<unsigned char *>(&iv[0])))
{
handleErrors();
}
if(cOk != EVP_DecryptUpdate(ctx,
reinterpret_cast<unsigned char *>(&plainText[0]),
&actual_size,
reinterpret_cast<const unsigned char *>(&cipherText[cKeySizeBytes + cIvSizeBytes]),
cipherText.size()- (cKeySizeBytes + cIvSizeBytes)))
{
handleErrors();
}
if(cOk != EVP_DecryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(&plainText[actual_size]), &final_size))
{
handleErrors();
}
actual_size += final_size;
plainText.resize(actual_size);
EVP_CIPHER_CTX_cleanup(ctx);
}
catch (std::runtime_error &ex)
{
throw ex;
}
catch (std::exception &ex)
{
std::cerr << ex.what() << std::endl;
}
return plainText.length();
}
void Cipher::rfc2898DeriveBytes(const string &passphrase,
const string &salt,
unsigned int iterations,
string &data)
{
try
{
PKCS5_PBKDF2_HMAC(passphrase.c_str(),
passphrase.size(),
reinterpret_cast<const unsigned char *>(&salt[0]),
salt.size(),
iterations,
EVP_sha1(),
data.size(),
reinterpret_cast<unsigned char *>(&data[0]));
}
catch (std::runtime_error &ex)
{
throw ex;
}
catch (std::exception &ex)
{
std::cerr << ex.what() << std::endl;
}
catch (...)
{
std::cerr << "Cipher::rfc2898DeriveBytes" << std::endl;
}
}
</code>
<code>int Cipher::Decrypt(const string &cipherText, const string &passPhrase, string &plainText) { const int cKeySizeBytes = 32; const int cSaltSizeBytes = 32; const int cIvSizeBytes = 32; EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); int actual_size = 0; int final_size = 0; string key(cKeySizeBytes, 0); string salt = cipherText.substr(0, cKeySizeBytes); string iv = cipherText.substr(cKeySizeBytes, cIvSizeBytes); try { rfc2898DeriveBytes(passPhrase, salt, cIterations, key); EVP_CIPHER_CTX_init(ctx); // Enc is 1 to encrypt, 0 to decrypt, or -1 (see documentation). if(cOk != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, reinterpret_cast<unsigned char *>(&key[0]), reinterpret_cast<unsigned char *>(&iv[0]))) { handleErrors(); } if(cOk != EVP_DecryptUpdate(ctx, reinterpret_cast<unsigned char *>(&plainText[0]), &actual_size, reinterpret_cast<const unsigned char *>(&cipherText[cKeySizeBytes + cIvSizeBytes]), cipherText.size()- (cKeySizeBytes + cIvSizeBytes))) { handleErrors(); } if(cOk != EVP_DecryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(&plainText[actual_size]), &final_size)) { handleErrors(); } actual_size += final_size; plainText.resize(actual_size); EVP_CIPHER_CTX_cleanup(ctx); } catch (std::runtime_error &ex) { throw ex; } catch (std::exception &ex) { std::cerr << ex.what() << std::endl; } return plainText.length(); } void Cipher::rfc2898DeriveBytes(const string &passphrase, const string &salt, unsigned int iterations, string &data) { try { PKCS5_PBKDF2_HMAC(passphrase.c_str(), passphrase.size(), reinterpret_cast<const unsigned char *>(&salt[0]), salt.size(), iterations, EVP_sha1(), data.size(), reinterpret_cast<unsigned char *>(&data[0])); } catch (std::runtime_error &ex) { throw ex; } catch (std::exception &ex) { std::cerr << ex.what() << std::endl; } catch (...) { std::cerr << "Cipher::rfc2898DeriveBytes" << std::endl; } } </code>
int Cipher::Decrypt(const string &cipherText,
                    const string &passPhrase,
                    string &plainText)
{
    const int cKeySizeBytes = 32;
    const int cSaltSizeBytes = 32;
    const int cIvSizeBytes = 32;

    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    int actual_size = 0;
    int final_size = 0;
    string key(cKeySizeBytes, 0);
    string salt = cipherText.substr(0, cKeySizeBytes);
    string iv = cipherText.substr(cKeySizeBytes, cIvSizeBytes);

    try
    {
        rfc2898DeriveBytes(passPhrase, salt, cIterations, key);
        EVP_CIPHER_CTX_init(ctx);
        // Enc is 1 to encrypt, 0 to decrypt, or -1 (see documentation).

        if(cOk != EVP_DecryptInit_ex(ctx,
                                     EVP_aes_256_cbc(),
                                     NULL,
                                     reinterpret_cast<unsigned char *>(&key[0]),
                                     reinterpret_cast<unsigned char *>(&iv[0])))
        {
            handleErrors();
        }

        if(cOk != EVP_DecryptUpdate(ctx,
                                    reinterpret_cast<unsigned char *>(&plainText[0]),
                                    &actual_size,
                                    reinterpret_cast<const unsigned char *>(&cipherText[cKeySizeBytes + cIvSizeBytes]),
                                    cipherText.size()- (cKeySizeBytes + cIvSizeBytes)))
        {
            handleErrors();
        }

        if(cOk != EVP_DecryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(&plainText[actual_size]), &final_size))
        {
            handleErrors();
        }

        actual_size += final_size;

        plainText.resize(actual_size);

        EVP_CIPHER_CTX_cleanup(ctx);

    }
    catch (std::runtime_error &ex)
    {
        throw ex;
    }
    catch (std::exception &ex)
    {
        std::cerr << ex.what() << std::endl;
    }

    return plainText.length();
}

void Cipher::rfc2898DeriveBytes(const string &passphrase,
                                const string &salt,
                                unsigned int iterations,
                                string &data)
{

    try
    {
        PKCS5_PBKDF2_HMAC(passphrase.c_str(),
                          passphrase.size(),
                          reinterpret_cast<const unsigned char *>(&salt[0]),
                          salt.size(),
                          iterations,
                          EVP_sha1(),
                          data.size(),
                          reinterpret_cast<unsigned char *>(&data[0]));
    }
    catch (std::runtime_error &ex)
    {
        throw ex;
    }
    catch (std::exception &ex)
    {
        std::cerr << ex.what() << std::endl;
    }
    catch (...)
    {
        std::cerr << "Cipher::rfc2898DeriveBytes" << std::endl;
    }
}

During encryption, when I call EVP_EncryptFinal_ex, the final_size value is 16, and when I call EVP_DecryptFinal_ex, the final_size value is 10. This doesn’t cause a problem in my C++ program but I have problems sometimes when trying to decrypt files not encrypted by my C++ program. I thought that this would be a good place to start investigating.

Can anyone help please?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật