src/crypter.cpp
// General secure AES 256 CBC encryption routine
General secure AES 256 CBC encryption routine
General secure AES 256 CBC encryption routine
bool EncryptAES256(const SecureString& sKey, const SecureString& sPlaintext, const std::string& sIV, std::string& sCiphertext)
{
    int nLen = sPlaintext.size();
    int nCLen = nLen + AES_BLOCK_SIZE;
    int nFLen = 0;
    if(sKey.size() != 32 || sIV.size() != AES_BLOCK_SIZE) {
        LogPrintf("crypter EncryptAES256 - Invalid key or block size: Key: %d sIV:%d\n", sKey.size(), sIV.size());
        return false;
    }
    sCiphertext.resize(nCLen);
    DEF_EVP_CIPHER_CTX ctx;
    bool fOk = true;
    DEF_EVP_CIPHER_CTX_init(ctx);
    if (fOk) fOk = EVP_EncryptInit_ex(SSL_ADDR(ctx), EVP_aes_256_cbc(), NULL, (const unsigned char*) &sKey[0], (const unsigned char*) &sIV[0]);
    if (fOk) fOk = EVP_EncryptUpdate(SSL_ADDR(ctx), (unsigned char*) &sCiphertext[0], &nCLen, (const unsigned char*) &sPlaintext[0], nLen);
    if (fOk) fOk = EVP_EncryptFinal_ex(SSL_ADDR(ctx), (unsigned char*) (&sCiphertext[0])+nCLen, &nFLen);
    EVP_CIPHER_CTX_cleanup(SSL_ADDR(ctx));
    if (!fOk)
        return false;
    sCiphertext.resize(nCLen + nFLen);
    return true;
}
bool EncryptAES256 ( const SecureString & sKey , const SecureString & sPlaintext , const std :: string & sIV , std :: string & sCiphertext ) { int nLen = sPlaintext . size ( ) ; int nCLen = nLen + AES_BLOCK_SIZE ; int nFLen = 0 ; if ( sKey . size ( ) != 32 || sIV . size ( ) != AES_BLOCK_SIZE ) { LogPrintf ( " \n " , sKey . size ( ) , sIV . size ( ) ) ; return false ; } sCiphertext . resize ( nCLen ) ; DEF_EVP_CIPHER_CTX ctx ; bool fOk = true ; DEF_EVP_CIPHER_CTX_init ( ctx ) ; if ( fOk ) fOk = EVP_EncryptInit_ex ( SSL_ADDR ( ctx ) , EVP_aes_256_cbc ( ) , NULL , ( const unsigned char * ) & sKey [ 0 ] , ( const unsigned char * ) & sIV [ 0 ] ) ; if ( fOk ) fOk = EVP_EncryptUpdate ( SSL_ADDR ( ctx ) , ( unsigned char * ) & sCiphertext [ 0 ] , & nCLen , ( const unsigned char * ) & sPlaintext [ 0 ] , nLen ) ; if ( fOk ) fOk = EVP_EncryptFinal_ex ( SSL_ADDR ( ctx ) , ( unsigned char * ) ( & sCiphertext [ 0 ] ) + nCLen , & nFLen ) ; EVP_CIPHER_CTX_cleanup ( SSL_ADDR ( ctx ) ) ; if ( ! fOk ) return false ; sCiphertext . resize ( nCLen + nFLen ) ; return true ; }
General secure AES 256 CBC encryption routine
General secure AES 256 CBC encryption routine
// max ciphertext len for a n bytes of plaintext is // n + AES_BLOCK_SIZE - 1 bytes // Verify key sizes // Prepare output buffer // Perform the encryption