在sqlite中插入char *作为vector <byte>

时间:2018-03-29 11:07:30

标签: c++ sqlite

我试图将char*(从文件中读取)作为字节数组(vector<byte>)插入到SQLite数据库中,以避免被明确查看。

下面是我的代码,用于读取文件并将其作为字节数组放在缓冲区中,

void DataBaseUtils::readFileContents(string filePath)
{
    ifstream file(filePath, std::ios::binary);
    if (!file) {
        cerr << "An error occurred opening the file\n";
        return;
    }
    file.seekg(0, ifstream::end);
    size = file.tellg();
    file.seekg(0);

    char *buffer = new char[size];
    file.read(buffer, size);

    vector<byte> vByteArray(reinterpret_cast<byte>(buffer), size);

    mBlobBuffer = (char*)&vByteArray;

    mCharBuffer = buffer;

}

以下是我将数据插入源代码的地方

    std::stringstream strmInsert("");

    strmInsert << "INSERT or IGNORE INTO SAMPLETABLE(NAME, CONTENTS) VALUES(" << "'" << getName() << "'," << '?' << ")";

    string sInsert = strmInsert.str();
    char *strInsert = &sInsert[0];
    char *query1 = strInsert;

    sqlite3_stmt *stmt = NULL;

    int rc = sqlite3_prepare_v2(dbfile, query1, -1, &stmt, NULL);

    if (rc != SQLITE_OK) {
        cerr << "prepare failed: " << sqlite3_errmsg(dbfile) << endl;
    }
    else {
        // SQLITE_STATIC because the statement is finalized
        // before the buffer is freed:
        rc = sqlite3_bind_blob(stmt, 1, mCharBuffer /*mBlobBuffer*/, size, SQLITE_STATIC);
        if (rc != SQLITE_OK) {
            cerr << "bind failed: " << sqlite3_errmsg(dbfile) << endl;
        }
        else {
            rc = sqlite3_step(stmt);
            if (rc != SQLITE_DONE)
                cerr << "execution failed: " << sqlite3_errmsg(dbfile) << endl;
        }
    }
    sqlite3_finalize(stmt);

在我的sqlite3_bind_blob语句中,如果我使用mCharBuffer,则查询会正常运行,但如果我使用mBlobBuffer,则应用程序有时会崩溃并且有时会运行,没有任何模式。

如何解决它并将文件内容作为BLOB数据插入CONTENTS列。

Sample Image to show BLOB data

Sample Image to show Plain text

编辑:为清晰起见添加了图像。我希望将数据作为第一张图像的BLOB而不是第二张的纯文本(ABCD ..)

1 个答案:

答案 0 :(得分:1)

通过将vector<byte>存储在数据库中,无法隐藏文本,因为vector<byte>对象的包含您的文本。不是直接的。

vector<byte>基本上是几个指针(可能是3个)指向你的文字而没有别的。存储向量只是存储那些指针。

隐藏文字的唯一方式是加密

此代码的一个问题是您在设置std::vector<byte>时将char*投射到mBlobBuffer。即使向量是char*(它不是),它也会在函数结束时被破坏。所以mBlobBuffer没有任何用处。

我会考虑将mCharBuffer作为 vector 并使用它。你可以避免这种动态分配。像这样:

class DataBaseUtils
{
public:
    void readFileContents(string filePath);

private:
    // make this  std::vector
    std::vector<char> mCharBuffer;
};

void DataBaseUtils::readFileContents(string filePath)
{
    std::ifstream file(filePath, std::ios::binary);
    if (!file) {
        std::cerr << "An error occurred opening the file\n";
        return;
    }

    file.seekg(0, std::ios::end);
    mCharBuffer.resize(file.tellg()); // no need to store the size elsewhere
    file.seekg(0, std::ios::beg);

    if(!file.read(mCharBuffer.data(), mCharBuffer.size())) {
        std::cerr << "An error occurred reading the file\n";
        return;
    }

    // need to encrypt the contents of the vector here
}

然后,当你把它放入函数时:

rc = sqlite3_bind_blob(stmt, 1, mCharBuffer.data(), mCharBuffer.size(), SQLITE_STATIC);

大小记录在载体中。

此外,您不会记录错误发生的时间,我会抛出异常,而不仅仅是打印错误。