⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 warcrypttext.cpp

📁 ftpserver very good sample
💻 CPP
字号:
#include "StdAfx.h"#include "WarCryptText.h"   // class implemented#if WAR_CRYPTO#ifndef WAR_INCLUDED_TCHAR_H#   define WAR_INCLUDED_TCHAR_H#   include <tchar.h>#endif#if WAR_CRYPTO && !defined(WAR_OPENSSL_RSA_H_INCLUDED)#   define WAR_OPENSSL_RSA_H_INCLUDED#   include <openssl/rsa.h>#endif#if WAR_CRYPTO && !defined(WAR_OPENSSL_PEM_H_INCLUDED)#   define WAR_OPENSSL_PEM_H_INCLUDED#   include <openssl/pem.h>#endif#ifndef WAR_LOG_H#   include "WarLog.h"#endifusing namespace std;/////////////////////////////// PUBLIC ///////////////////////////////////////#define BL_LEN EVP_CIPHER_block_size(ctx.cipher)#define BL_ALIGN(a) a += (BL_LEN - (a % BL_LEN))//============================= LIFECYCLE ====================================WarCryptText::WarCryptText(war_ccstr_t secretPasswd): mPasswd(passwd_t::SM_ERASE){    mPasswd = secretPasswd;}// WarCryptTextWarCryptText::WarCryptText(const WarCryptText& from): mPasswd(passwd_t::SM_ERASE){    operator = (from);}// WarCryptTextWarCryptText::~WarCryptText(){}// ~WarCryptText//============================= OPERATORS ====================================WarCryptText& WarCryptText::operator = (const WarCryptText& from){    mPasswd = from.mPasswd;    return *this;    }// =//============================= OPERATIONS ===================================void WarCryptText::EncryptBytes(buffer_t& toBuf,                                 const byteptr_t fromBuf,                                const size_t bufferLength)                                const throw(WarException){    EVP_CIPHER_CTX ctx;    if (mPasswd.mString.empty())        WarThrow(WarError(WAR_ERR_INTERNAL_DATA_NOT_INITIALIZED), NULL);        if (!EVP_EncryptInit(&ctx, EVP_bf_cbc(),         (unsigned char *)mPasswd.GetValue().c_str(),        (unsigned char *)"wfde"))    {        WarOpenSslError ssl_err;        WarLog warn_log(WARLOG_WARNINGS, "WarCryptText::EncryptBytes");                warn_log << "Failed to initialize encryption. "            "EVP_EncryptInit() failed. "            << ssl_err            << war_endl;                WarThrow(ssl_err, NULL);    }        size_t block_size = bufferLength;    if (block_size % BL_LEN)        BL_ALIGN(block_size);        vector<unsigned char> nencr_buffer(block_size + BL_LEN);    toBuf.resize(block_size + BL_LEN);    byteptr_t nonencr_value = (byteptr_t)&nencr_buffer[0];    byteptr_t encr_value = (byteptr_t)&toBuf[0];    int encr_bytes = 0;        memcpy(nonencr_value, fromBuf, bufferLength);        if (!EVP_EncryptUpdate(&ctx, encr_value, &encr_bytes,        nonencr_value, bufferLength))    {        WarOpenSslError err;        EVP_CIPHER_CTX_cleanup(&ctx);        WarThrow(err, "EVP_EncryptUpdate()");    }        int pad_len = 0;    if (!EVP_EncryptFinal(&ctx, encr_value + encr_bytes, &pad_len))    {        WarOpenSslError err;        EVP_CIPHER_CTX_cleanup(&ctx);        WarThrow(err, "EVP_EncryptFinal()");    }    encr_bytes += pad_len;        EVP_CIPHER_CTX_cleanup(&ctx);    memset(nonencr_value, 0, nencr_buffer.size());    toBuf.resize(encr_bytes);}void WarCryptText::DecryptBytes(buffer_t& toBuf,                                 const byteptr_t fromBuf,                                const size_t bufferLength)                                const throw(WarException){    EVP_CIPHER_CTX ctx;    if (mPasswd.mString.empty())        WarThrow(WarError(WAR_ERR_INTERNAL_DATA_NOT_INITIALIZED), NULL);        if (!EVP_DecryptInit(&ctx, EVP_bf_cbc(),         (unsigned char *)mPasswd.GetValue().c_str(),        (unsigned char *)"wfde"))    {        WarOpenSslError ssl_err;        WarLog warn_log(WARLOG_WARNINGS, "WarCryptText::DecryptBytes");                warn_log << "Failed to initialize decryption. "            "EVP_DecryptInit() failed. "            << ssl_err            << war_endl;                WarThrow(ssl_err, NULL);    }        size_t block_size = bufferLength;    if (block_size % BL_LEN)        BL_ALIGN(block_size);        vector<unsigned char> encr_buffer(block_size + BL_LEN);    toBuf.resize(block_size + BL_LEN);    byteptr_t encr_value = (byteptr_t)&encr_buffer[0];    byteptr_t plain_value = (byteptr_t)&toBuf[0];    int decr_bytes = 0;        memcpy(encr_value, fromBuf, bufferLength);        if (!EVP_DecryptUpdate(&ctx, plain_value, &decr_bytes,        encr_value, bufferLength))    {        WarOpenSslError err;        EVP_CIPHER_CTX_cleanup(&ctx);        WarThrow(err, "EVP_EncryptUpdate()");    }        int pad_len = 0;    if (!EVP_DecryptFinal(&ctx, plain_value + decr_bytes, &pad_len))    {        WarOpenSslError err;        EVP_CIPHER_CTX_cleanup(&ctx);        WarThrow(err, "EVP_EncryptFinal()");    }    decr_bytes += pad_len;        EVP_CIPHER_CTX_cleanup(&ctx);    memset(encr_value, 0, encr_buffer.size());    toBuf.resize(decr_bytes);}//============================= CALLBACK   ===================================//============================= ACCESS     ===================================//============================= INQUIRY    ===================================/////////////////////////////// PROTECTED  ////////////////////////////////////////////////////////////////// PRIVATE    ///////////////////////////////////#endif // WAR_CRYPTO

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -