我试图弄清楚我写的PHP脚本的问题。本质上,此脚本使用phpseclib library使用ASP.NET应用程序传递给它的公共RSA密钥来加密密码。密码好像加密了,但是当我试图将加密的字符串放入json字符串时,它变成了NULL。
代码:
// Create our RSA encryption object
$rsa = new Crypt_RSA();
// Get modulus and exponent for our public key
// ($keyInner was gathered from the keygen server)
$modulus = new Math_BigInteger(base64_decode($keyInner->{'Modulus'}), 256);
$exponent = new Math_BigInteger(base64_decode($keyInner->{'Exponent'}), 256);
// Generate our public key and padding scheme
$rsa->loadKey(array('n' => $modulus, 'e' => $exponent));
$rsa->setPublicKey();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
// Gather user data
// In reality this is gathered from a form but for
// example's sake I'll be using static values
$username = 'foo';
$password = 'boo';
// Encrypt password
$password = $rsa->encrypt($password);
// $password == <some encrypted string>
// gettype($password) == string
// mb_detect_encoding == UTF_8
// Create json
$arr = json_encode(array(
'payload' => array(
'username' => $username,
'password' => $password,
)
));
// $arr = {"payload":{"username":"Bob","password":null}}
答案 0 :(得分:3)
json_encode
无法很好地处理二进制数据,您最好base64_encode
:
// Create json
$arr = json_encode(array(
'payload' => array(
'username' => $username,
'password' => base64_encode($password),
)
));
然后在接收端解码它。
答案 1 :(得分:1)
听起来数据不是UTF-8有效,因此无法编码。
如果$ arr返回null,您可以使用json_last_error来检测实际问题是什么。