解码包含空格的 AES 字符串

发布于 2024-12-21 04:08:40 字数 579 浏览 6 评论 0原文

在从 iPhone 应用程序接收 URL 时,我们正在解密在 Objective-C 中创建的字符串,并作为 GET 变量传递到我们的 PHP 网站。

我们使用以下方法进行解码:

mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $padded_key, base64_decode($base64encoded_ciphertext), 'ecb');

大多数情况下工作正常但有时,我们会收到一个包含空格的字符串,例如:

mypage.php?score=IEZrdQ5iUECe9 xyfTY5Cg==

然后解密失败,我们得到如下结果:

结果 http://mattbee.co.uk/temp/mess.png

有谁知道如何处理字符串中间有空格,应该有空格甚至存在?我本以为编码可能与此有关,但转换为 UTF-8/UTF-16 没有帮助。

任何建议都非常感激。

In receiving a URL from an iPhone app, we are decrypting a string created in Objective-C and passed to our PHP website as a GET variable.

We are decoding using:

mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $padded_key, base64_decode($base64encoded_ciphertext), 'ecb');

Which is working fine the majority of the time But sometimes, we receive a string containing a space, for example:

mypage.php?score=IEZrdQ5iUECe9 xyfTY5Cg==

Then the decryption fails and we are left with a result like this:

the result http://mattbee.co.uk/temp/mess.png

Has anyone any idea how to handle strings with spaces in the middle of them, should spaces even exist? I would have thought encoding might have something to do with it but converting to UTF-8/UTF-16 didn't help.

Any advice greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉栀 2024-12-28 04:08:40

Base64 数据每 64 个字符包含空格,末尾有 0-2 个等号。因此,iPhone 应用程序应该在作为 GET 变量发送之前对其进行百分比编码。

Base64 data contains whitespace every 64 characters, and 0-2 equal signs at the end. So the iPhone app should percent-encode it before sending as a GET variable.

给妤﹃绝世温柔 2024-12-28 04:08:40

Base64可以表示任意字节,这正是密文所必需的。不要重新编码为其他任何内容。

某些软件会在 Base64 编码的字符串中插入空格和换行符,以便它们适合电子邮件和其他行长度有限的格式。解码时应忽略或删除这些。

如果base64编码的字符串中有空格,您可以在解码之前将其删除,尽管我认为 base64_decode 应该自动执行此操作。

另请注意:不要使用 ECB 模式,它是不安全的。使用 CBC 模式(带有与消息一起发送的随机初始化向量)。或者更好的是,不要加密 URL 的部分内容,而是使用 SSL(或 TLS),这将为您处理所有详细信息。

Base64 can represent arbitrary bytes, which is just what is necessary for ciphertext. Do not recode to anything else.

Some software inserts spaces and line-breaks in base64-encoded strings, so they fit into emails and other line-length limited formats. These should be ignored or removed when decoding.

If there are spaces inside the base64-encoded string, you could strip them out before decoding, though I think that base64_decode should do this automatically.

Another note: Do not use ECB-mode, it is insecure. Use CBC-mode (with a random initialization vector sent with the message). Or even better, don't encrypt parts of a URL, use SSL (or TLS) instead, which will handle all the details for you.

自演自醉 2024-12-28 04:08:40

您可以使用 bin2hex 然后使用 pack('H*', hex) 来解码字符串。我知道这种方法的唯一问题是字符串可能会变得很大。

You could go with bin2hex then use pack('H*', hex) to decode the string. The only issue I know with this method is that the string can grow very big.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文