我将为我的合作伙伴网站提供api密钥,他们将使用我给他们的代码来生成“令牌”。
这些令牌将自动出现在合作伙伴网站的用户点击并访问我的网站的表单上。 当他们到达我的网站时,我需要验证他们确实来自合作伙伴网站。
我该如何验证? apikey将是秘密的,但表单中的内容不会出现,因此智能用户无法对我的算法进行逆向工程。
鲷
选项1:我通过md5($ apikey。$ time)和$ time(以纯文本格式)发送客户端页面。 当我得到它时,我使用时间和我的apikey副本来生成md5($ apikey。$ time)。 如果它匹配并且在1小时内(或其他),我让请求继续。
选项2:我已经有了$ userid,$ requestcommandoption。 我可以做以下事情:$input = $userid.'-'.$requestcommandoption.'-'.$time;
$encrypted_data = mcrypt_ecb (MCRYPT_3DES, $apikey, $input, MCRYPT_ENCRYPT);
当我得到它的时候,我可以这样做:$decrypted_data = mcrypt_ecb (MCRYPT_3DES, $apikey, $encrypted_data, MCRYPT_DECRYPT);
如果它们相同则检查2个输入,如果它在1小时内则检查第3个输入?
EDITB// on client
$apikey="test123";
$userid = '577';
$requestcommandoption = 'delete-all';
$time = mktime();
echo "time = $time
";
$input = $userid.'-'.$requestcommandoption.'-'.$time;
// Encryption Algorithm
$cipher_alg = MCRYPT_RIJNDAEL_128;
// Create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg, MCRYPT_MODE_ECB), MCRYPT_RAND);
// Encrypt $string
$encrypted_string = mcrypt_encrypt($cipher_alg, $apikey, $input, MCRYPT_MODE_CBC, $iv);
$transmitted = bin2hex($encrypted_string);
// sent from client to server
print "Encrypted string: ".$transmitted."
";
// received on server
$encrypted_string = pack("H*" , $transmitted);
$decrypted_string = mcrypt_decrypt($cipher_alg, $apikey, $encrypted_string, MCRYPT_MODE_CBC, $iv);
print "Decrypted string: $decrypted_string";