二进制文件的问题c ++ android

时间:2018-04-12 08:40:58

标签: android c++ android-ndk rsa

我正在尝试移植Windows c ++项目,但我遇到以下代码时出现问题:

HRSRC hResource = FindResource(hModule, MAKEINTRESOURCE(IDR_KEY_DATA), _T("KEY_DATA"));
pBuffer = (LPBYTE)LockResource(LoadResource(hModule, hResource));
cbBuffer = SizeofResource(hModule, hResource);
Keystore.Initialize(pBuffer, cbBuffer);

在c ++ NDK中,我选择将二进制文件的内容定义为BYTE数组,这就是我所拥有的:

BYTE key_bytes[] = { 0x06,0x02,0x00,0x00,0x00,0x4B,0x00,0x00,0x41,0x42,0x4A,0x4C,0x15,0x96,0x23,0x62,0x5F,...};

我如何在NDK中实现windows c ++代码:

    int arraySize = sizeof(key_bytes) / sizeof(key_bytes[0]);
    LPBYTE pBuffer = &key_bytes[0];
    DWORD cbBuffer = arraySize;
    Keystore.Initialize(pBuffer, cbBuffer);

问题是我使用的RSACypher方法在NDK实现上失败,并带有分段异常。

在Windows和NDK实现中调试期间检查pBuffer的值时,这是我得到的: Windows =" \ x6 \ x2" NDK =" \ 006 \ 002"

我认为异常的原因可能与此有关。我是新手c ++开发人员,不知道如何使NDK pBuffer看起来像Windows pBuffer变量。 pBuffer是LPBYTE类型。

有人可以帮帮我吗?

根据要求: Windows变量和值:

LPBYTE pBuffer = 0x0fd41518 "\x6\x2"
DWORD cbBuffer = 3120

Android NDK变量和值:

LPBYTE pBuffer = 0x8a8f5004 <key_bytes> "\006\002"
DWORD cbBuffer = 3120

windows dll项目正常运行,但android .so项目在RSA.cpp文件的以下行中给出了分段错误异常:

int cinfo = cryptInfo->ctxPKC.keySizeBits;

包含功能:

int rsaCypher(void *ptr, BYTE *buffer, int noBytes)

{

CRYPT_INFO *cryptInfo = (CRYPT_INFO *)ptr;
BN_CTX *bnCTX;
BIGNUM *n;
int cinfo = cryptInfo->ctxPKC.keySizeBits;
int length = bitsToBytes(cinfo);
int i, status = CRYPT_OK;

if (noBytes != length || length == 0)
    return -2;      // length error

                    /* Make sure we're not being fed suspiciously short data quantities */
for (i = 0; i < length; i++)
    if (buffer[i])
        break;
if (length - i < 56)
    return(CRYPT_ERROR_BADDATA);

if ((bnCTX = BN_CTX_new()) == NULL)
    return(CRYPT_ERROR_MEMORY);

/* Move the data from the buffer into a bignum, perform the modexp, and
move the result back into the buffer.  Since the bignum code performs
leading-zero truncation, we have to adjust where we copy the result to
in the buffer to take into account extra zero bytes which aren't
extracted from the bignum */
n = BN_new();
BN_bin2bn(buffer, length, n);
zeroise(buffer, length);    /* Clear buffer while data is in bignum */
BN_mod_exp_mont(n, n, cryptInfo->ctxPKC.rsaParam_e,
    cryptInfo->ctxPKC.rsaParam_n, bnCTX,
    cryptInfo->ctxPKC.rsaParam_mont_n);
BN_bn2bin(n, buffer + (length - BN_num_bytes(n)));
BN_clear_free(n);

BN_CTX_free(bnCTX);

return((status == -1) ? CRYPT_ERROR_FAILED : status);

}

经过调查,我看到* ptr变量值为0x0。我相信这会导致分段错误。在Windows库中,它包含一个有效的指针值。

我可以在两个项目之间找到的唯一区别是Windows项目中的pBuffer值与NDK项目中的pBuffer值之间存在差异。

WIndows项目vs2017 WIndows SDK 10 构建工具2017(141)

NDK项目vs2017 Xamarin NDK13b clang 3.8构建工具

更新: 我认为根本原因是字节数组编码。它应该是UTF-16BE,但VS Xamarin与clang3.8支持UTF-8。有人知道如何将原始字节复制到android NDK13c中的UTF-16BE字符串吗?

0 个答案:

没有答案