doutiaosu2310 2013-11-16 12:15
浏览 77

PHP中的C ++ DSS签名验证

I have a C++ application to sign and verify some data, now I want to verify the data in PHP, following is my C++ code for data signing:

extern "C" __declspec(dllexport) BYTE* Sign(BYTE* bytdata)
{
    // Private key blob
    BYTE prKeyBlob[] = {7 , 2 , 0 , 0 , 0 , 34 , 0 , 0 , ...};

    HCRYPTPROV hProv = NULL;
    HCRYPTKEY prKey;
    HCRYPTHASH hHash;
    DWORD SignLen;

    if(CryptAcquireContext(&hProv, NULL, NULL, PROV_DSS, CRYPT_VERIFYCONTEXT)) 
// Creating cryptography provider
    {
        // Importing public key
        if(!CryptImportKey(hProv, prKeyBlob, sizeof(prKeyBlob), 0, 0, &prKey))
            return NULL;

        // Creating hash object
        if(!CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash))
            return NULL;

        if(!CryptHashData(hHash, bytdata, DATALEN, 0))
            return NULL;

        // Signing hashed value
        if(!CryptSignHash(hHash, AT_SIGNATURE, NULL, 0, NULL, &SignLen))
            return NULL;

        BYTE* bytSign = (BYTE*)malloc(SignLen);
        if(CryptSignHash(hHash, AT_SIGNATURE, NULL, 0, bytSign, &SignLen))
            return bytSign;
        else
            return NULL;
    }
    else
        return NULL;
}

I've tried PHP openssl_verify() but I cannot feed the public key correctly; in C++ I have a byte array of public key blob but I don't know how to extract public key from this array, and use it with php openssl.

function verify($data, $sign)
{
    // fetch public key from certificate and ready it
    $cert = file_get_contents('./key.pem');
    $pubkeyid = openssl_get_publickey($cert) or die("KEY ERROR");

    // state whether signature is okay or not
    return openssl_verify($data, $sign, $pubkeyid, OPENSSL_ALGO_DSS1)?1:0;
}

But I get "error:0906D06C:PEM routines:PEM_read_bio:no start line" from openssl_get_publickey

my key.pem file contents:

-----BEGIN PUBLIC KEY-----
fkNkBaO1Y0ZruN8LD8BGm3IF00bbSNZN/ql8ak0duOjbzDP229rnkPFDIPihbO
9Uw6369b3suwqvPY3w+VzwRKKfLG99KiMxMgF3H3IvJl8hyzQf6qJGJ9X
sonzhrTqDeugT9fa2FnpY5pg+7g+6MqSRh1T0qTii9JFcwVf5r/o=
-----END PUBLIC KEY-----

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongxie8906 2013-11-27 00:17
    关注

    There is a clear indication in the PHP documentation what is wrong. PHP retrieves the public key from a certificate if you use openssl_get_publickey. Unfortunately, you haven't not a certificate, you only have a public key.

    So there are two options: create a (self-signed?) certificate around your public key or find a function in PHP that reads a PEM encoded public key. Unfortunately, that last function seems to be missing in action.

    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能