PHP 通过 URL 发送加密数据

PHP sending encrypted data via the URL(PHP 通过 URL 发送加密数据)
本文介绍了PHP 通过 URL 发送加密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 url 将加密数据发送到另一个站点(使用 file_get_contents("anotherUrl.php?hash=$encryptedString").问题是,有时,加密包含一些特殊字符,如 +,这会导致解密失败.

I'm trying to send encrypted data over the url to another site (using file_get_contents("anotherUrl.php?hash=$encryptedString"). The problem is, sometimes, the encryption contains some special characters, like +, and this causes the decryption to fail.

这是我的加密/解密方法:

Here are my encryption / decryption methods:

public function encrypt($string, $key)
{
    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
}

public function decrypt($encrypted, $key)
{
    return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "");
}

这是一个包含 + 的示例加密字符串,我猜这会导致解密失败.

Here's an example encrypted string which contains a +, and I'm guessing that this causes the decryption to fail.

oWCrVPaS+5GbxcQFc0fulUk/zRAkDD60av4zlPiWskE=

任何想法我应该如何解决这个问题?我尝试对哈希执行 urlencode()urldecode() ,但这似乎也会导致加密中断.有没有办法改变加密算法让它只返回 url 安全字符?

Any ideas how I should solve this? I've tried to do urlencode() and urldecode() on the hash, however that also seems to cause the encryption to break. Is there a way to change the encryption algorithm to get it to only return url safe characters?

推荐答案

看看这个帖子:

在 URL 中传递 base64 编码字符串

基本上你DO想要在发送字符串之前urlencode(),但是你确实想要urldecode() 在另一端.

Essentially you DO want to urlencode() before sending the string, however you do NOT want to urldecode() at the other end.

这篇关于PHP 通过 URL 发送加密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)