使用phpseclib生成RSA DES3签名

时间:2012-01-17 16:01:27

标签: php rsa phpseclib

私钥:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,A3CEE9AD263E2C7F

td8ySvosoIOBjoZMxA5rnr//1YoZsyReia9P9xJVXf1D+ime68AFTEf0A+is9whB
9dthPGhJ3SiOBrFZICNuFZrPmXtrw0T5VoJHs3I71yHIclgKA6AMKYVwUBfzdwLL
1RKwkGeZI7nkXTCmG1ZF0pa0v6CmWinZCv6capTF8WVEwTxggvOyxbCqltkVsDyV
cO5j2zZhhqIP+/peMxzTs7lqsWIhHgohsCPAVsBp8fFyuu1I6RUSgVANzjkN4K+k
sQ1+l7oJKJh4FnzWNaj0VMwNPGXastA97TSTmqciSUohb6yUOArFZUxIQ9o9mH8c
P4ajkmLrYOUcwA1+e9QPTTeQko2wF3Lla1a57Wq8zLIjexsF2E5klkBUCopyeBOL
p8MgmB18k9/43W45v4fQZjojRyn07Q+Wu25tTdqC00GFDCzH2CR7p9O+Uc67SjcB
4FqKjLez9QwJbPoZvdAhjbiarV251eoXwq2RtHv7teH8S629x/9vGryUNnNHw5mJ
2XPiIWWW/y/lIw3e1GIRsNm8cW2KSkB9pYHGbVEwEjCp1ozFsTLockjpMpqVvHET
l4XVYhQx6RXQQx8NkofwYBSlqI7KvbCSPDxPhLDe6vY0aPITFmmevEL5SY7qb6QU
5Yj2blmA9UxDMqsAfWQZrTonwa1NiYzh0FAc+kPb0cUOWkP3EMfmVvY8fLtJ7Y+w
C5cWO24j8k1E5KNr8kYuT2tKfMNB3V5hL35Dq1XZNYvXbT167+wDWjiaSwZuQQKi
eFJoqj0nHLkGzdgrPifxNyoP4IqZ28sdOcYHfN/EQZFNEKy0DlujOg==
-----END RSA PRIVATE KEY-----

字符串:

eyJhZ2VudCI6InBsYWFheS10ZXN0IiwidGltZXNhbHQiOjEzMjY4MTUxMDgsImFjdGlvbiI6InRlc3QiLCJ2ZXJzaW9uIjoiMS4wLjEiLCJpZCI6MX0=

我需要为该字符串生成签名。 代码:

$xpacket = eyJhZ2VudCI6InBsYWFheS10ZXN0IiwidGltZXNhbHQiOjEzMjY4MTUxMDgsImFjdGlvbiI6InRlc3QiLCJ2ZXJzaW9uIjoiMS4wLjEiLCJpZCI6MX0=;

include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents('private_rsa'), CRYPT_RSA_PUBLIC_FORMAT_PKCS1); //tryied any format there
$rsa->setSignatureMode(CRYPT_DES_MODE_3CBC);
$rsa->setPassword('my_password');
$signature = $rsa->sign($xpacket); //
echo $signature;

首先我收到这个错误:

Warning: mcrypt_generic_init() [function.mcrypt-generic-init]: Key size too large; supplied length: 26, max: 24 in /var/www/u2113183/data/www/plaaay.ru/api/Crypt/TripleDES.php on line 708

我尝试从

更改TripleDES.php第255行
define('CRYPT_DES_MODE', CRYPT_DES_MODE_MCRYPT); 

define('CRYPT_DES_MODE', CRYPT_DES_MODE_INTERNAL);

强制内部模式。但在内部模式中,我收到了错误:

Warning: unpack() [function.unpack]: Type N: not enough input, need 4, have 2 in /var/www/u2113183/data/www/plaaay.ru/api/Crypt/DES.php on line 1047

请告诉我,有什么问题? 谢谢。

1 个答案:

答案 0 :(得分:0)

$ rsa-> setPassword()应该在$ rsa-> loadKey()之前调用。

此外,CRYPT_DES_MODE_3CBC不是有效的签名模式。私钥是加密密钥的事实将由phpseclib自动检测,因为私钥中的DEK-Info:DES-EDE3-CBC,A3CEE9AD263E2C7F线。 $ rsa> setSignatureMode()的有效参数是CRYPT_RSA_SIGNATURE_PSS和CRYPT_RSA_SIGNATURE_PKCS1。