0

我在使用global访问函数内部的数组时遇到了一个奇怪的小问题。我知道我可以尝试使用参考,但我仍然想了解为什么我在下面做的事情不起作用。

include "./phpseclib/Math/BigInteger.php";
include "./phpseclib/Crypt/Random.php";
include "./phpseclib/Crypt/RSA.php";

$keyArray = array();

function key_gen($exponent=65537, $bits=1024) {
    global $keyArray;

    $rsa = new Crypt_RSA();
    define('CRYPT_RSA_EXPONENT', $exponent);
    $rsa->setHash("sha256");
    $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);

    extract($rsa->createKey($bits));
    $keyArray["clientPublicKey"] = $publickey;
    $keyArray["clientPrivateKey"] = $privatekey;
    $keyArray["clientPassPhrase"] = bin2hex(crypt_random_string(128));

    extract($rsa->createKey($bits));
    $keyArray["serverPublicKey"] = $publickey;
    $keyArray["serverPrivateKey"] = $privatekey;
    $keyArray["serverPassPhrase"] = bin2hex(crypt_random_string(128));
    print_r($keyArray);

}

key_gen();
print_r($keyArray);

产生以下内容:

Array
(
    [clientPublicKey] => -----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCWWmihVabPYOTMYTUUPwpUdcND
hI7k7F6cnbKeohzIZF6v82ZaieSPfO3QtQtL3pev/iagKZCLpC0NHvWSyV7EO5IT
od4uHFeJk97XwxSumPxamZ+deW8dOyCrFTDF9ibwe3Fq9dyLQ4yhjsr73DgAvAOR
9YfvV5xypZ+5mWa95wIDAQAB
-----END PUBLIC KEY-----
    [clientPrivateKey] => -----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCWWmihVabPYOTMYTUUPwpUdcNDhI7k7F6cnbKeohzIZF6v82Za
ieSPfO3QtQtL3pev/iagKZCLpC0NHvWSyV7EO5ITod4uHFeJk97XwxSumPxamZ+d
eW8dOyCrFTDF9ibwe3Fq9dyLQ4yhjsr73DgAvAOR9YfvV5xypZ+5mWa95wIDAQAB
AoGAD+7z0J50PNzZUllo18Tt0iaKs2CHCMvjI44eDEFEyJhuZQ9nxOeBERMIXB+b
hgZIT+515xo7Mbk2dwIAEfavc/Q9EZWy7YDNjeHgl/Np6vNPLPPvFgV7wLmdYryz
ZzDDa1UzHzwYBMeN2IHnNXI+EyK1ziTSudhXVzTlvpHOEyUCQQCXAuLy2ZYJfeJo
nz2CjANUdAWvB4M/lteJXOy8e1AIKzBJFnQp8jStm7dHUHxQOQjPMLtyhZkzDjtk
/9YaLqTFAkEA/uJj3DuNHh8EWp1ZtayWjmO4SyK5D1wIlDeVAsOhmlAbnJaGvvfX
APwJ2/SgrgeH2O7+ALBMSU+ArN3CeXz6uwJBAIymyp0o3l8u2d9aWh8qq7tQaS7S
D4P9yfkODQ2sySkimNGqUT30cu40d+u7Yl4SEUtxd1W+a6erHNUAmPk/8oUCQQCa
cVVvGkqNCOBbg+La+aQUzYnbRUP7cZnlLbTGAwKF/aptfKxTRNvrxcy3kghWy0te
gJo6kpj3BwyCMrFnafqHAkB/p20073eicx8Zu8R2HiFTbVT8xZpPaAAJq/2spUWw
0btk2Zy5Usr4kx2PLdY2ig4vE9358u25FMM/DgKz7jA1
-----END RSA PRIVATE KEY-----
    [clientPassPhrase] => 1841479148f770551e3cd4ce442789c0870a73235309bf07de00f216f6aa1d6aecd070c94ef175446d00d036c9a61dba14da41985ecff69a8922e95d1807970960dc8696c14ba630328f04c05c578dd16e3bf915ae13c0c568fa0ddffbae08a2d7092ebcc7222e07949258b0fc05cf30b23ba7ca4c40b3910bbf016feac7e6a5
    [serverPublicKey] => -----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUhxVLlpOTVNOhDUnFrqihNH8b
grXhWzl0IpsW3pnvwo+fQTwmBSKsCXKMd7k7QVYQ4/amu+tCC5ZTe8ngQbSQHSBd
jLQqg+C9AHucQw+HIkKrKaPvTD32m2QSIMvARLdJij/3ziT/3hB68pbwZYWpiAUr
/lujXvUoeTzasZWpxQIDAQAB
-----END PUBLIC KEY-----
    [serverPrivateKey] => -----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCUhxVLlpOTVNOhDUnFrqihNH8bgrXhWzl0IpsW3pnvwo+fQTwm
BSKsCXKMd7k7QVYQ4/amu+tCC5ZTe8ngQbSQHSBdjLQqg+C9AHucQw+HIkKrKaPv
TD32m2QSIMvARLdJij/3ziT/3hB68pbwZYWpiAUr/lujXvUoeTzasZWpxQIDAQAB
AoGAHQFmXL0as86Ah7afHg5WWyjz260ClPBqP25UqDoviL2Pq7OjKe+dzkgICTuG
Pnardp/2NqiDMIIvyLzndzaNqrzO9xvHqHTCpspEcUBATpx5h2O3No8wDkfs8Nrr
zsIyxirkzNhNXvuPbqq7ihqxb+9lOQpsphFQ00NAAPBakA8CQQDR8gOieYpZNUC0
UFxugSNjWpR8jnb8mxB2d92D0s0FDvE63ci7b3oF/OqDKxsq1pTiLqla6q4IVJhy
0R6tQWRLAkEAtRwCv/VuzXJgudZPG7QRGbRV0ZDrPxyxo3Fs7rqRvLOb5QgeWVzv
zs6aAMi8sROzK3NPWXgCZAA7dCNsFJTALwJAXXvEPo6cGgjqXOADqHj8RvtIbwYI
2YY6mGXbTx2IiR4UMEAdZ8jFrvDlg66oFDUkamqYopEpGTCCCW4wmLkrbQJAbDNp
6SlGo3N8GkrRRGnV9Zdp1iRQh4orDszLGe1Y+HYN401wZE9D7jg8hc9n/Bsa5hCk
DX3HX9tLqd0nyzv7fQJBAKu9N6m/bCpegi3lKFvXZ/N30DJCog9l4u9adkaKq7du
6cPrw7mBM0msM5XxVUa5c7K6H5v+ZI7rsk7q9Qrt+Tk=
-----END RSA PRIVATE KEY-----
    [serverPassPhrase] => 7db35ef1985e8313c057441d5445fcb16426836cc1d127a06d578cfb4740d5be66b1cf7c60f9dc501b3059fe150173d4fc662f2c6579b6a20d48f44f9b8e55bd8529f88d72bb5ec57e07e58033a95baa621313cefa4439d1c22961bc0d9026f4582ad654cabb55224c948cb983954cf2564bb8f567fb9b081b333feafdba7bae
)
Array
(
)

注意 print_r($keyArray) 的第二次尝试不包含任何内容。为什么,它与 phpseclib 中的内容存储在 $keyArray 中的方式有​​关吗?

4

1 回答 1

3

我认为代码通过包含放置在某个函数中。有两个作用域:内部函数和外部函数。但它们都是本地的。您应该global $keyArray;$keyArray = array();.

但最好将此数组用作函数参数。

于 2013-11-13T23:58:24.853 回答