douxuan0698 2016-05-24 17:57
浏览 44
已采纳

DSA算法:登录PHP

I need to sign a data on my server in php, and verify the signed data on my Java desktop application. My issue is that I don't succeed to generate a key that both languages accept...

My first try was to generate the key with Java KeyPairGenerator, but as shown in that question, I couldn't make openssl to read the key.

Now I generated the keys with openssl like that :

$openssl dsaparam 512 < /dev/random > dsaparam.pem
$openssl gendsa dsaparam.pem -out dsa_priv.pem
$openssl dsa -in dsa_priv.pem -pubout -out dsa_pub.pem

I now try to sign something with this code :

<?php
$data = "test";

$pkeyid = openssl_pkey_get_private("dsa_priv.pem");
echo openssl_sign($data, $signature, $pkeyid);
echo "<br/>";
echo $signature;
?>

Result ?

openssl_sign(): supplied key param cannot be coerced into a private key in ...

What am I doing wrong ? Can anyone help me generate a working DSA key and sign something with it ?

  • 写回答

1条回答 默认 最新

  • duanlu2935 2016-05-25 10:26
    关注

    You are missing one important thing here. The openssl_pkey_get_private method takes either key as a string or a file with file:// protocol. The following example works as expected:

    $data = "test";
    
    $pkeyid = openssl_pkey_get_private("file://dsa_priv.pem");
    openssl_sign($data, $signature, $pkeyid);
    echo "
    Signature: " . base64_encode($signature) . "
    ";
    

    And produces:

    Signature: MCwCFBpkSESngh+6ooMQZj7i+76t87QmAhQJTfyvvoZ4YFPd722R2qRnXD/Giw==

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号