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==

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

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能