const unsigned char*到字符串或const char*的转换

const unsigned char* conversion to/from string or const char*

本文关键字:const char 转换 unsigned 字符串      更新时间:2023-10-16

我迷失在指针的黑社会中!这是我的问题,

它非常古怪,我只能控制其中一个功能,所以请不要说我需要重新设计。这是在Linux Ubuntu 11.04中使用android-ndkr7编译的。这是一个纯原生的应用程序(或服务),将在安卓手机上运行。我正在使用谷歌测试来验证我的类/函数。第一个函数(我的测试类)必须声明无符号的char*,它将其传递给第二个函数用作输出(crypt::encryptBuffer),encrypt获取声明的变量,为其分配内存,并将其传递到第三个函数,该函数将值作为输出放入其中。

地下室

class Crypt
{
public:
   Crypt();
   ~Crypt();
   bool encryptBuffer(const unsigned char* inDecryptBuffer, const int inputSize, unsigned char** outEncryptBuffer, int*  pOutSize);
};
#endif

Crypt.cpp

#include "Crypt.h"
#include "pan/crypt.h"
static unsigned char HydraEncryptionKey[] = {0x17, 0x43, 0x9B, 0x55, 0x07, 0xAE, 0x73, 0xB1, 0x32, 0x10, 0xE0, 0x22, 0xD9, 0xC7, 0xF2, 0x3B};
bool AccCrypt::encryptBuffer(const unsigned char* inDecryptBuffer, const int inputSize, unsigned char** outEncryptBuffer, int*  pOutSize)
{
    int encryptedSize;
    pan::aes128_cbc enc(HydraEncryptionKey);
    // see how long the encrypted data will be and allocate space for the data
    encryptedSize = enc.output_len( inputSize );
    *outEncryptBuffer = (unsigned char*)malloc(encryptedSize + 4);
    enc.encrypt(inDecryptBuffer, *outEncryptBuffer, inputSize );
    return true;
}

CryptTest.cpp

#incude "Crypt.h"
#include <gtest/gtest.h>
#define CHECK_COND(X, a, b, c) { 
if(X) 
{ 
    printf("FAIL: %sn", c); 
    printf("Press any key to continue");
    getc(stdin);
}
else 
{ 
    printf("PASS: %sn", c); 
}
}
#define EXPECT_EQ(a,b,c)  CHECK_COND((a != b), a, b, c)
const char* decBuff = "something";
const int inputSize = 10;
unsigned char* encBuffTest = NULL;
int pOutsize = 0;
class cryptTester : public testing::Test
{
    protected:
    virtual void SetUp()
    {
        cryptTest = new Crypt();
        cryptTest->encryptBuffer((const unsigned char*)decBuff, inputSize, &encBuffTest, &pOutsize);
    }
    virtual void TearDown()
    {
    }
    Crypt* cryptTest;
};
TEST_F(AccCryptTest, decryptBuffer)
{
    int poutSize = 0;
    EXPECT_EQ(true, accCryptTest->decryptBuffer((const unsigned char*)encBuffTest, pOutsize, &outDecryptBuffTest, &poutSize), "decryptBuffer(valid, valid)");
}

这会编译得很好,但是当我在手机上运行它时,我会遇到分段错误。我无法弄清楚这是在哪里发生的,因为我无法从adb shell正确设置调试。

任何帮助都将不胜感激!

您的代码看起来不错,可能错误在encrypt方法中:

enc.encrypt(inDecryptBuffer, *outEncryptBuffer, inputSize );