Hi,
I am trying to login on mobile server through PHP language. I was able to get the server public key using this code (sending my public key on Connect method). This is working:
private const DH_PRIME = 'F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88B31C7C5B2D8EF6F3C923C043F0A55B188D8EBB558CB85D38D334FD7C175743A31D186CDE33212CB52AFF3CE1B1294018118D7C84A70A72D686C40319C807297ACA950CD9969FABD00A509B0246D3083D66A45D419F9C7CBD894B221926BAABA25EC355E92F78C7';
private const DH_GENERATOR = '02';
$privateKey = gmp_init(uniqid(), 32);
$publicKey = gmp_powm(self::DH_GENERATOR, $privateKey, base_convert( self::DH_PRIME, 16,10));
Then, I understand that I must compute the server public key with my private key using the PRIME value to get the shared key and then encrypt the username and password (this is not working):
// Decode server public returned key (encoded in base 64)
$serverPKBase64Decoded = base64_decode($apiResponse['response']['Command']['OutputParams']['Param']['7']['@attributes']['Value']);
$serverPublicKey16 = bin2hex($serverPKBase64Decoded);
// Calculate shared key
$prime = gmp_init(self::DH_PRIME,16);
$serverPk = gmp_init($serverPublicKey, 16);
$localPk = gmp_init($localPrivateKey,32);
$sharedKey = gmp_powm($serverPk, $localPk, $prime);
// Get shared key as hexadecimal value
$sharedKeyHex = gmp_strval($sharedKey, 16);
Now, I has following your JS samples to know what is required to get from shared key to have the key and the initialization vector to make the encryption, but I always get error code 16 with text “Incorrect public key”.
// I need an IV of 16 bytes and key with 32 bytes.
$iv = substr($sharedKeyHex,0,16);
$key = substr($sharedKeyHex,16, 32);
$options = OPENSSL_RAW_DATA;
$encryptedUsername = base64_encode(openssl_encrypt($username,$cipher,$key,$options,$iv));
$encryptedPassword = base64_encode(openssl_encrypt($password,$cipher,$key,$options,$iv));
I don’t know what is wrong here. I have tried with multiple possibilities, but it is not working yet. Please, could you help me or explain the detailed process here?
We cannot use JS for creating bookmarks because we need to execute this code on server side, not client side.
Thank you very much
