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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动