我尝试使用Crypt :: OpenSSL:RSA加密perl中的数据并在php中解密数据。 我能够在perl中解密,但在php中得到NULL。
perl代码:
use strict;
use MIME::Base64;
use Crypt::OpenSSL::RSA;
print "Content-Type: text/html\n\n";
my $string = '123';
my $key_string = "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2n9QlRt3ERZXt6CQ5ufb
eKIlzaJFqr7LxgxiRSy/ZdMnxvCdRoKtW4JbVM62WaEBuOVZIxlJRYRTplloDl9l
7yksxEfN5/AosIJzOPqjj1U1ICvoFS1ORS/8FwAqBd3HeL8BW3gIH+03WTh+bKE/
65u7hyTEi/bb3k1JtxUVSKI04EovCZVivR/nhAZ7lJrjhW23vmhTwlIoIijZfOre
ctE00rcJ9I5KY4V4djQM8mDGFWQTNrdBku+DpGt4//lw/i5w0/MCr9mHqSw3Nxty
/PnMG7dpiA+WjVA9W1TYCVcRZBD+Wsd3OjXxgl46OZYdI5dKYNLKE7BUMccEJj9l
eQIDAQAB
-----END PUBLIC KEY-----";
my $public = Crypt::OpenSSL::RSA->new_public_key($key_string);
$string = $public->encrypt($string);
$string = MIME::Base64::encode_base64($string);
print $string;
php代码:
<?php
$encrypted = 'ZkEVc2US6/mqeix3409VYXSTmJtycVLxXztsKpCFpbb6Adp3MBRfYL4nblsWLND+17xaLhVqk4+h ZPw97gzAqlp8YNPA4vgCVdzamq84+kVd7Ykqot9UcDq9zRSpo7S/8EenZO8Cu9OlAwvTavb2pSnX z0w9a9mJqmIJ+zYtao6L1tV3+WlcMIYix9vaIiWU5qZigjNAlff+wT20pEh7Lqu2iAsd1h8aKt5l 3NBRqHG9M0WqTdhzrqk0Rvb9i/a2Zoo7XWC/jz9OR8FaThCM7Gyw8+jWL+z+aL2qveeAPW9e7Pwa GSk1SibTs1L0lTeQk8FmjtC2IW1j7Qn8NzUcZA==';
$encrypted = base64_decode($encrypted);
$key_content = "-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----";
$private_key_res = openssl_get_privatekey($key_content);
openssl_private_decrypt($encrypted, $decrypted, $private_key_res);
var_dump($decrypted);
?>
我在PHP中得到NULL
答案 0 :(得分:0)
使用add $ public-&gt; use_sslv23_padding()解决了这个问题; 加密数据可以在php中解密。