1

ここに私のphpスクリプトがあります

<?php
$pubfile = fopen("public_key.pem", "r");
$public = fread($pubfile, 8192);
fclose($pubfile);
openssl_get_publickey($public);

$prifile = fopen("private_key.pem", "r");
$private = fread($prifile, 8192);
fclose($prifile);
openssl_get_privatekey($private);

$plaintext = "hello";

openssl_public_encrypt ($plaintext , $crypted , $public);

openssl_private_decrypt($crypted, $decrypted, $private);
echo $decrypted;
?>

それぞれの公開鍵と秘密鍵

private_key.pem

    -----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtwxvPpbvbeJCztWlQCC0w2mQTNICarpIkbFTzlEooXA8Zo30
sN9cdH0Ifa/pIhfx0Lbm+e03Fz5NZelJZ5QRiRfoN9tcb5zYrwvVWx1SR6Zo5moS
S5YEvVSWVpu3D7x3IO2XVtTHlWTTRZk2c+ZuFR3eIwDZbmuOiTmuJMQCJiRd8pqf
4sVCoKzWrUmBuT2pc/sdUQ4IPylGK3yGVyxHNa8gCkBs/zVq3I0Zu7Nx8mW8DTn9
EvP02mkFHynJZgKE0SJftu6Mp9BDl4qR4T8896K+Z1Fi3qx3iK6nkjYjXCYYZB90
6uWiqm7BK/4zbzuc604+BZ4LOPgCsO+aZjx3ywIDAQABAoIBABgTfp5Q4/UkCX2D
BFVG9/+r5trz1HWNQvZGyKseaaWySX+DtGd/OX3o5DED7joZxCibpg3BcbmQ7EU3
RfxlsVjC2DxgkevsSWPcvpdZZ/UcojeMRBPFC6KLeaqymIVtWI3wMjOtkQLJXDCP
UmY0LM6COzGHbxHi+8B44brcPRwe+zJK5ALhPfFEyPsnz9Rw2PLBWceXNeZYN0ja
C699r+a/t0kdJ0+49LLTvFjFECZLArZTK1GZbzACeM/fFl7XvK3tsdAkizys0Dwl
eyDtINkWlg+hzt8uijVY78MvP36RM8k4QIjSX/2p/jupsyZD4I2R9OW+ml9sImok
Ud5YKaECgYEA7xbLLvZ5ddpf/yBSVpGytfU/PzWK5L9cPRbrQPll6iKgimAOTULi
VQAQYaCDL/er/z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wMqhKiRbM83Olxepk8XBNyRimvypxaPhcEiQYFlirVq
/jvyUaJg01VjXxp1PKqXwIurF+Eu+W6b4OotLHY0EAPckNiP4XOHMNWd/NG5jV3F
mEzz5Cn9IdMDHSewcA6/rsOqVx0bZOBnSsmVLu9fPsCyrs7eBSXZSlIFKHD39HR6
eQIDAQAB
-----END PUBLIC KEY-----

$crypted暗号化されたデータを返しますが、復号化は何もエコーしません。

私は結果のエコーを行いましたが、それはNULL何かアイデアとして戻ってきますか?

4

2 に答える 2

1

解決しました。何が起こったのかわかりませんが、新しいキーペアを生成した後、スクリプトが機能しました。キーペアを混同した可能性があります

于 2013-03-03T05:51:09.977 に答える
0

を使用openssl rsaしてキーの内容をダンプすると、上で投稿した秘密キーと公開キーのモジュラスが異なることがわかります。一致する鍵ペアの場合、モジュラスは秘密鍵と公開鍵で同じになります。言い換えれば、あなたは間違いなくそこで異なるキーを混同しました。

これを検出するために、OpenSSLRSA_check_key()自体には RSA キーペアが使用可能かどうかをチェックする機能がありますが、OpenSSL の PHP ラッパーはその機能をユーザーに公開していないようです。

公開鍵:

% openssl rsa -in public_key.pem -pubin -text
Public-Key: (2048 bit)
Modulus:
    00:9c:32:3e:43:fb:5b:55:3a:10:df:40:c1:07:4c:
    ba:f3:d8:af:07:4d:60:46:1b:80:11:80:aa:31:e7:
    b3:84:4f:02:af:34:0d:dd:61:ac:a3:1d:d0:de:8a:
    ed:2d:99:1b:b8:5a:cc:41:d0:4d:e9:98:c3:d7:0d:
    24:f6:eb:71:2e:bd:cc:15:16:d7:a8:0c:5e:5d:de:
    cd:94:2c:fd:6e:20:5a:43:0d:4b:6e:82:ab:d7:49:
    15:c8:43:4f:4d:14:f6:36:b4:27:7e:c1:6c:23:a5:
    9f:bc:c2:a2:bb:ad:bf:64:41:d3:f3:40:cc:52:f8:
    a5:49:2b:91:6c:d2:ab:7f:03:2a:84:a8:91:6c:cf:
    37:3a:5c:5e:a6:4f:17:04:dc:91:8a:6b:f2:a7:16:
    8f:85:c1:22:41:81:65:8a:b5:6a:fe:3b:f2:51:a2:
    60:d3:55:63:5f:1a:75:3c:aa:97:c0:8b:ab:17:e1:
    2e:f9:6e:9b:e0:ea:2d:2c:76:34:10:03:dc:90:d8:
    8f:e1:73:87:30:d5:9d:fc:d1:b9:8d:5d:c5:98:4c:
    f3:e4:29:fd:21:d3:03:1d:27:b0:70:0e:bf:ae:c3:
    aa:57:1d:1b:64:e0:67:4a:c9:95:2e:ef:5f:3e:c0:
    b2:ae:ce:de:05:25:d9:4a:52:05:28:70:f7:f4:74:
    7a:79
Exponent: 65537 (0x10001)
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnDI+Q/tbVToQ30DBB0y6
89ivB01gRhuAEYCqMeezhE8CrzQN3WGsox3Q3ortLZkbuFrMQdBN6ZjD1w0k9utx
Lr3MFRbXqAxeXd7NlCz9biBaQw1LboKr10kVyENPTRT2NrQnfsFsI6WfvMKiu62/
ZEHT80DMUvilSSuRbNKrfwMqhKiRbM83Olxepk8XBNyRimvypxaPhcEiQYFlirVq
/jvyUaJg01VjXxp1PKqXwIurF+Eu+W6b4OotLHY0EAPckNiP4XOHMNWd/NG5jV3F
mEzz5Cn9IdMDHSewcA6/rsOqVx0bZOBnSsmVLu9fPsCyrs7eBSXZSlIFKHD39HR6
eQIDAQAB
-----END PUBLIC KEY-----

秘密鍵:

% openssl rsa -in private_key.pem -text
Private-Key: (2048 bit)
modulus:
    00:b7:0c:6f:3e:96:ef:6d:e2:42:ce:d5:a5:40:20:
    b4:c3:69:90:4c:d2:02:6a:ba:48:91:b1:53:ce:51:
    28:a1:70:3c:66:8d:f4:b0:df:5c:74:7d:08:7d:af:
    e9:22:17:f1:d0:b6:e6:f9:ed:37:17:3e:4d:65:e9:
    49:67:94:11:89:17:e8:37:db:5c:6f:9c:d8:af:0b:
    d5:5b:1d:52:47:a6:68:e6:6a:12:4b:96:04:bd:54:
    96:56:9b:b7:0f:bc:77:20:ed:97:56:d4:c7:95:64:
    d3:45:99:36:73:e6:6e:15:1d:de:23:00:d9:6e:6b:
    8e:89:39:ae:24:c4:02:26:24:5d:f2:9a:9f:e2:c5:
    42:a0:ac:d6:ad:49:81:b9:3d:a9:73:fb:1d:51:0e:
    08:3f:29:46:2b:7c:86:57:2c:47:35:af:20:0a:40:
    6c:ff:35:6a:dc:8d:19:bb:b3:71:f2:65:bc:0d:39:
    fd:12:f3:f4:da:69:05:1f:29:c9:66:02:84:d1:22:
    5f:b6:ee:8c:a7:d0:43:97:8a:91:e1:3f:3c:f7:a2:
    be:67:51:62:de:ac:77:88:ae:a7:92:36:23:5c:26:
    18:64:1f:74:ea:e5:a2:aa:6e:c1:2b:fe:33:6f:3b:
    9c:eb:4e:3e:05:9e:0b:38:f8:02:b0:ef:9a:66:3c:
    77:cb
publicExponent: 65537 (0x10001)
privateExponent:
    18:13:7e:9e:50:e3:f5:24:09:7d:83:04:55:46:f7:
    ff:ab:e6:da:f3:d4:75:8d:42:f6:46:c8:ab:1e:69:
    a5:b2:49:7f:83:b4:67:7f:39:7d:e8:e4:31:03:ee:
    3a:19:c4:28:9b:a6:0d:c1:71:b9:90:ec:45:37:45:
    fc:65:b1:58:c2:d8:3c:60:91:eb:ec:49:63:dc:be:
    97:59:67:f5:1c:a2:37:8c:44:13:c5:0b:a2:8b:79:
    aa:b2:98:85:6d:58:8d:f0:32:33:ad:91:02:c9:5c:
    30:8f:52:66:34:2c:ce:82:3b:31:87:6f:11:e2:fb:
    c0:78:e1:ba:dc:3d:1c:1e:fb:32:4a:e4:02:e1:3d:
    f1:44:c8:fb:27:cf:d4:70:d8:f2:c1:59:c7:97:35:
    e6:58:37:48:da:0b:af:7d:af:e6:bf:b7:49:1d:27:
    4f:b8:f4:b2:d3:bc:58:c5:10:26:4b:02:b6:53:2b:
    51:99:6f:30:02:78:cf:df:16:5e:d7:bc:ad:ed:b1:
    d0:24:8b:3c:ac:d0:3c:25:7b:20:ed:20:d9:16:96:
    0f:a1:ce:df:2e:8a:35:58:ef:c3:2f:3f:7e:91:33:
    c9:38:40:88:d2:5f:fd:a9:fe:3b:a9:b3:26:43:e0:
    8d:91:f4:e5:be:9a:5f:6c:22:6a:24:51:de:58:29:
    a1
prime1:
    00:ef:16:cb:2e:f6:79:75:da:5f:ff:20:52:56:91:
    b2:b5:f5:3f:3f:35:8a:e4:bf:5c:3d:16:eb:40:f9:
    65:ea:22:a0:8a:60:0e:4d:42:e2:55:00:10:61:a0:
    83:2f:f7:ab:ff:38:d5:53:e6:b6:2b:1b:f5:1b:cc:
    97:fc:b5:c7:5e:da:52:6f:12:15:be:04:ea:b4:0b:
    86:61:3d:21:1a:40:85:b5:44:1a:be:22:46:cf:fa:
    f7:0e:71:a2:16:ef:a5:f0:53:f1:c3:e0:90:12:07:
    9e:c8:d1:b8:f5:3c:f2:03:b6:8e:0e:cc:16:9b:7a:
    3a:69:61:25:a6:d2:43:75:0f
prime2:
    00:c3:fe:e9:45:09:20:2d:a2:b3:2c:d8:9d:08:d0:
    cd:ab:fe:3f:f3:39:89:a0:c7:ba:19:52:c6:97:e1:
    d5:1c:ac:e7:ea:93:d9:7d:cb:48:db:4d:72:68:9e:
    4b:6f:23:da:60:3f:f6:d8:c9:43:95:e8:38:03:dc:
    d9:20:bc:a4:4b:74:11:a3:f7:b1:dc:7f:bb:7b:16:
    00:e2:a4:1c:2e:a5:c9:4f:c0:f9:a0:c9:4d:bc:58:
    85:e8:d5:87:bb:b5:89:b5:79:6d:ab:4e:6f:70:15:
    dd:47:2e:ac:dd:bc:b5:f9:25:f2:1f:8d:87:e8:7f:
    77:d0:02:04:47:5d:fa:e9:85
exponent1:
    31:7c:b9:4f:9e:2a:a3:cf:30:81:81:32:29:c2:fc:
    87:97:1c:a1:3b:b6:e5:04:0a:d6:86:30:e9:80:f5:
    ac:51:c0:83:03:c0:d2:13:1e:ff:d3:04:91:00:85:
    46:df:6f:d5:62:f1:1c:ed:ab:15:1f:33:4b:ae:bc:
    62:cd:49:58:03:be:7e:48:f3:01:22:c9:65:bc:e0:
    f2:ad:91:75:7a:97:72:5e:87:5e:85:7f:bc:ba:c8:
    7a:4f:e0:e9:f8:6e:93:60:fc:b0:bb:81:76:89:e7:
    3e:cf:d2:05:c8:b8:9b:36:25:0f:41:05:08:e7:72:
    c1:af:1e:f8:f9:9a:86:d7
exponent2:
    18:45:95:35:cb:6f:84:b7:45:61:24:62:2e:19:4d:
    9f:23:19:60:51:9b:cd:a1:6e:56:7c:7d:c3:89:60:
    0e:f1:0a:b7:6e:1f:dd:08:33:b9:94:d9:ba:43:00:
    2c:7f:50:27:e3:43:fa:8c:d2:25:da:60:35:7a:40:
    19:61:c3:26:e4:f4:da:4d:6e:65:54:1c:2f:9e:05:
    f6:0b:44:c2:32:1d:c0:16:b1:d3:4b:9a:a8:dc:7d:
    98:db:f8:b7:32:40:1f:8d:2e:1b:ac:d6:f8:16:7d:
    14:b7:9f:08:c6:b9:f2:64:d4:9b:3b:b7:bc:ff:2c:
    23:42:5c:ad:49:e2:2c:d1
coefficient:
    00:d4:8d:5d:76:05:df:68:50:f0:22:6d:fc:ba:9b:
    b5:06:91:71:f7:dd:c2:8c:ad:15:5a:6c:d6:52:25:
    49:f9:ae:fa:1b:b1:67:9f:f2:2e:e6:6d:01:6c:bd:
    39:be:52:88:05:de:11:b3:d3:a9:80:36:e2:a9:fc:
    00:26:0b:fc:0a:f6:0a:c9:18:db:61:d1:27:3f:e9:
    a1:0d:45:b8:c4:63:7f:ef:ea:b1:13:f4:54:5d:9a:
    a5:2e:32:9f:bb:bf:88:a0:92:21:18:5e:15:8f:30:
    fe:ef:20:cf:1c:7f:ad:29:2c:ff:11:26:a0:10:a0:
    3a:af:27:82:e4:24:a9:ad:61
writing RSA key
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtwxvPpbvbeJCztWlQCC0w2mQTNICarpIkbFTzlEooXA8Zo30
sN9cdH0Ifa/pIhfx0Lbm+e03Fz5N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