dpzjl68484
dpzjl68484
2013-11-13 23:41
浏览 43
已采纳

PHP phpseclib库和全局变量vs引用变量

I'm having a strange little problem accessing an array inside a function using global. I know I can try using a reference instead but I'd still like to understand why what I'm doing below doesn't work.

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);

produces the following:

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
(
)

Notice the second attempt to print_r($keyArray) contains nothing. Why exactly, does it have something to do with the way the content from phpseclib is being stored in $keyArray?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsh1956
    dsh1956 2013-11-13 23:58
    已采纳

    I think that code placed inside some function via include. There two scopes: inside function and outside function. But they are local both. You should add global $keyArray;before $keyArray = array();.

    But it's better to use this array as function argument.

    点赞 评论

相关推荐