函数可作为可执行文件而不是共享库C ++

时间:2019-12-14 15:14:45

标签: c++ security encryption

我想从数字保险箱获得密码,然后将其返回给我的主程序。 该密码是一个字符串,我必须将其返回为c(请参见下面的代码)。 我发现以下问题:

  • 当我将其编译为可执行文件并显示密码时,它可以工作;
  • 当我将其编译为共享库时,它不起作用;
  • 如果我创建一个具有与初始密码相同内容的新变量(str2)(并返回c2而不是c),则这两种情况都可以用作可执行文件和共享库。

当我比较c和c2时,它们都具有相同的特性(类型,值,长度,十六进制代码未在此处显示)。

#include <iostream>
#include <string>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <stdio.h>
#include <iomanip>
//hostname
#include <unistd.h>
#include <limits.h>
//lms
#include "CPasswordSDK.h"
#include "keyStore.h"
#include <stdlib.h>
#include <string.h>



using namespace std;


char* KeyStore::getPwd(const string& aUID)
{
cout << "getPwd called for " << aUID.c_str() << endl;
return KeyStore::getPwd(aUID.c_str());
}



char* KeyStore::getPwd(const char* const aUID){

//lms
    int iRc = 0;
    ObjectHandle hResponse = NULL;
    ObjectHandle hRequest = NULL;
    char** pVals = NULL;
    char QueryStr[200];

    string AppBillingString;
    string SafeString;
    string FolderString;
    string ObjectString;

    const char* userconst;
    userconst = aUID; 
    char* userchar = const_cast<char*>(userconst);
    string user(userchar);
    string str;


//host
    char hostname[HOST_NAME_MAX];
    gethostname(hostname, HOST_NAME_MAX);
    string server(hostname);

//Creation de la requete

    string passwordString = "PASSWORD";
    char* password = strdup(passwordString.c_str());

    hRequest = PSDK_CreateRequest(password);

    if (hRequest != NULL){
        string AppDescsIDString = "AppDescs.AppID";
        char* AppDescsID = strdup(AppDescsIDString.c_str());


    string ligne;
    string res;
    ifstream fichier;
        fichier.open("cfnconf.csv", ios::in);  
    int i = 0;

    while(fichier){
        string element;
        if(!getline(fichier, element)){
            break;
        }
        istringstream line(element);
        vector<string>row;

        while (line){
            if(!getline(line, element, ';')){
                i=0;
                break;
            }
            row.push_back(element);
            i++;
        }
        if((server.compare(row[0])==0)&&(user.compare(row[1])==0)){
            AppBillingString=row[2];
            SafeString=row[3];
            FolderString=row[4];
            ObjectString=row[5];
            break;
        }
        row.push_back(element);
    }
    if(!fichier.eof()){
    }

        char* AppBilling = strdup(AppBillingString.c_str());

        iRc = PSDK_SetAttribute(hRequest, AppDescsID, AppBilling);
        if (iRc == PSDK_RC_ERROR){
            cout << "APP_ID non trouve" << endl;
        }

        string QueryString = "Query";
        char* Query = strdup(QueryString.c_str());
        char* AppSafe = strdup(SafeString.c_str());
        char* AppFolder = strdup(FolderString.c_str());
        char* AppObject = strdup(ObjectString.c_str());

        sprintf(QueryStr, "Safe=%s;Folder=%s;Object=%s", AppSafe, AppFolder, AppObject);
        iRc = PSDK_SetAttribute(hRequest, Query, QueryStr);

        if (iRc == PSDK_RC_ERROR){
            cout << "Query pas bonne" << endl;
        }
    }


//Envoi de la requete
    iRc = PSDK_GetPassword(hRequest, &hResponse);
    if (iRc == PSDK_RC_ERROR){
        printf ("error code: [%d], error message: [%s]\n", PSDK_GetErrorCode(hRequest), PSDK_GetErrorMsg(hRequest));
        cout << "erreur 1" << endl;
    }
    else{
        pVals = PSDK_GetAttribute(hResponse, password);
        if (iRc == PSDK_RC_ERROR){
            printf("error code: [%d], error message: [%s]\n", PSDK_GetErrorCode(hResponse), PSDK_GetErrorMsg(hResponse));
            cout << "erreur 2" << endl;
        }
        else{
            str = pVals[0];
            printf("adresses pVals[0] %p, *pVals %p, pVals %p\n", pVals[0], *pVals, pVals );
            printf("password pVals[0]=[%s]\n", pVals[0]);
            printf("password   *pVals=[%s]\n", *pVals);
            PSDK_ReleaseAttributeData(&pVals);      

        }
    }
    if (hRequest != NULL){
        PSDK_ReleaseHandle(&hRequest);
    }
    if (hResponse != NULL){
        PSDK_ReleaseHandle(&hResponse);
    }


/*
    higher we have the password stored in a string str

*/

    char *c =&str[0];

    string str2="BV2OA_MAJ00";
    // here I create a new variable with the same content than the password str

    const char *c2=str2.c_str();
    printf("adresse c2 %p\n", c2);

    //below i will compare str and str2 and c and c2

    cout << "password length : " << strlen(c) << endl;
    cout << "password2 length : " << strlen(c2) << endl;

    int result = strcmp(c, c2);
    cout << "strcmp c and c2 : " << result << endl;
    cout << "c :"<<c<<":"<< endl;
    cout << "c2:"<<c2<<":"<<endl;

    cout << "pVals[0] type : " << typeid(pVals[0]).name() << endl;
    cout << "*pVals[0] type : " << typeid(*pVals[0]).name() << endl;
    cout << "str type : " << typeid(str).name() << endl;
    cout << "str value : " << str << endl;
    cout << "str2 type : " << typeid(str2).name() << endl;
    cout << "str2 value : " << str2 << endl;    
    cout << "c type : " << typeid(c).name() << endl;
    cout << "c value : " << c << endl;
    cout << "c2 type : " << typeid(c2).name() << endl;
    cout << "c2 value : " << c2 << endl;
    cout << "const_cast char *c type : " << typeid(const_cast<char*>(c)).name() << endl;
    cout << "const_cast char *c value : " << const_cast<char*>(c) << endl;
    cout << "const_cast char *c2 type : " << typeid(const_cast<char*>(c2)).name() << endl;
    cout << "const_cast char *c2 value : " << const_cast<char*>(c2) << endl;

    return c;
}

(可执行文件)日志如下。您会看到两个密码相同。

adresse c2 0x9edf08
password length : 11
password2 length : 11
strcmp c and c2 : 0
c :BV2OA_MAJ00:
c2:BV2OA_MAJ00:
pVals[0] type : Pc
*pVals[0] type : c
str type : Ss
str value : BV2OA_MAJ00
str2 type : Ss
str2 value : BV2OA_MAJ00
c type : Pc
c value : BV2OA_MAJ00
c2 type : PKc
c2 value : BV2OA_MAJ00
const_cast char *c type : Pc
const_cast char *c value : BV2OA_MAJ00
const_cast char *c2 type : Pc
const_cast char *c2 value : BV2OA_MAJ00

在我的主窗口中,我还显示返回的密码:

PASSWORD MAIN : [BV2OA_MAJ00]

为什么我返回c2而不是返回c1时共享库能工作?

感谢您的指导!

P.S。这是我的第一个问题,希望我不要忘记任何事情。

0 个答案:

没有答案