php生成一次性连接,通过PHP中的mcrypt生成一次性令牌?

本文探讨了两种API安全验证方法:使用MD5哈希和时间戳,以及使用3DES加密。这两种方法旨在确保来自合作伙伴网站的请求经过验证且未被篡改。在MD5方法中,服务器通过比较接收到的哈希值和本地生成的哈希值来验证请求。而在3DES方法中,敏感数据经过加密传输,然后在服务器端解密验证。这两种方法都考虑了时间限制以增强安全性。
摘要由CSDN通过智能技术生成

我将为我的合作伙伴网站提供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";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值