douxian7117 2012-02-04 11:31
浏览 60

在php中验证来自java的rsa符号

Since a few days I've got a problem that I can't solve on my own:

On a JavaCard I generate a RSA KeyPair (length: 1024) and a signature (Mode:ALG_RSA_MD5_PKCS1). Now I have to verify the signature in php.

From my JavaCard I get the exponent, modulus and the signature in hexadecimal:

    $mod = '951ADDA04637190B6202BB52787D3C19160A383C80C2E7242D0A7850FDD80C1CD1CCCF1395F8CA0B20270E3BC6C86F78232D65D148258BEFD0884563C60AB2C327506FB4FA0095CF0B1C527D942155731451F790EC0A227D38613C9EBFB2E04A657B3BA5456B35F71E92E14B7E1CB38DB6572559BFCA3B0AD8AA061D48F68931';
    $exp = '010001';
    $sign ='75867D42BDE6DF1066D4AF69418FCDD4B0F19173141128DFEBC64AF6C014CB92D38F4824E52BB064A610E07C7783AE57AE993A792F15208FB199CB1F45B64623AACB7FBA07AD89513C8DBA893C9FA6939857AA2CA53AAD99D9A9C1C32DF4E2769FCACB72E2C2C495727D368D953A911D32E79E230751202714DD15C0B6A34782';
    $plaintext = '01020304';

A Verification in Java is no problem. But know I have to verify the signature in PHP (I take phpseclib).

In PHP I generate my public_key with CRYPT_RSA_PUBLIC_FORMAT_RAW:

    $rsa = new Crypt_RSA();
    $pk = array(
        'e' => new Math_BigInteger($exp, 16),
        'n' => new Math_BigInteger($mod, 16)
    );
    $rsa->loadKey($pk, CRYPT_RSA_PUBLIC_FORMAT_RAW);

    $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
    echo $rsa->verify($plaintext, $sign) ? 'verified' : 'unverified';

The problem know is to set the correct values in the function verify. If I just set my signature in hexadecimal I get the notice:

Invalid signature: length = 256, k = 128 in C:\xampp\php\PEAR\Crypt\RSA.php on line 2175

So I have to customize the length of my signature:

    $sign_bigInteger = new Math_BigInteger($sign, 16);
    $sign_bytes = $sign_bigInteger->toBytes();

    echo $rsa->verify($plaintext, $sign_bytes) ? 'verified' : 'unverified';

But the verification is false.

I get the output of the verification function in RSA.php (_rsassa_pkcs1_v1_5_verify) where plaintext is compared with the signature :

    //sign
    "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ0 0*†H†÷ ÖÀZ!Q*y¡ßë*&/" 
    //plaintext
    "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ0!0 +•q£îê“O•äQ».åüÓSœÝ["

I don't really understand whats happening in the Class RSA.php. Can anyone help me and say what I do wrong?

EDIT:

Now I tried to convert my hexString.

    $plaintext_bin = pack("H*", $plaintext);
    $sign_bin = pack("H*", $sign);

I think that my public key is correct generated, so I just change the input of my verify:

    $rsa->verify($plaintext_bin, $sign_bin) ? 'verified' : 'unverified';

Output:

    em: string(128) "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ0 0*†H†÷ ÖÀZ!Q*y¡ßë*&/"
    em2: string(128) "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ0!0 +ÚÚÿMG‡­ã31G ,;D>7o" 

It's still not the same.

EDIT:

I fixed my problem. I forgot to set the Hash:

    $rsa1->setHash('md5');

Now it works!

Thank you GregS.

  • 写回答

1条回答 默认 最新

  • douwen5924 2012-02-04 12:38
    关注

    All your values are hex strings. Just convert them using hex2bin() or pack("H*", $hex_string);

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?