Android 无法解码 PHP base64 编码

发布于 2024-11-26 09:27:31 字数 1519 浏览 0 评论 0原文

我有一个 PHP 脚本,可以根据用户名和时间戳创建 QR 码。我需要证明 QR 码来自我的服务器,因此我使用 RSA 私钥对用户名和时间戳进行加密。为了将其放入 QR 码中,我对其进行了 Base64 编码。

我首先在 Android 上进行试用。我可以从二维码中获取字符串,但是当我在 Android 中对它进行 Base64 解码时,它返回 null。调试后看来是因为字符串中有两个空格。当解码器检查非法字符是否能被4整除时,显然失败了。我绝望地删除了空格,但随后它改变了长度,因此计算仍然无法完成。我可以将空格更改为“安全”字符吗?或者特定的编码/解码对不兼容?

PHP 代码:

$data = base64_encode($username."`".$timestamp);
$fp = fopen("private.pem", "r");
$private_key = fread($fp, 8192);
fclose($fp);
openssl_private_encrypt($data, &$encrypted_data, $private_key);
$encrypted_data_64 = base64_encode($encrypted_data);
// create QR code

Android 代码:

String s = data.getStringExtra("SCAN_RESULT");
byte[] b = Base64.decode(s.toCharArray());
// b is null at this point

错误所在的 Base64 代码:是

// Check special case
int sLen = str != null ? str.length() : 0;
if (sLen == 0)
    return new byte[0];

// Count illegal characters (including '\r', '\n') to know what size the returned array will be,
// so we don't have to reallocate & copy it later.
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
for (int i = 0; i < sLen; i++)  // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
    if (IA[str.charAt(i)] < 0)
    sepCnt++;

// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
if ((sLen - sepCnt) % 4 != 0)
    return null;

I have a PHP script that creates a QR code out of a username and timestamp. I need to prove the QR code came from my server so I am encrypting the username and timestamp with a private RSA key. In order to get it into the QR code I am then base64 encoding it.

I am trialling it with Android first. I can get the string out of the QR code but when I base64 decode it in Android, it returns null. After debugging it seems it's because there are two whitespaces in the string. When the decoder comes to check the illegal characters are divisible by 4, it obviously fails. Getting desperate I removed the whitespaces but then it changed the length so the calculations still didn't work out. Can I change the whitespace for a 'safe' character? Or is the particular encode/decode pair not compatible??

PHP code:

$data = base64_encode($username."`".$timestamp);
$fp = fopen("private.pem", "r");
$private_key = fread($fp, 8192);
fclose($fp);
openssl_private_encrypt($data, &$encrypted_data, $private_key);
$encrypted_data_64 = base64_encode($encrypted_data);
// create QR code

Android code:

String s = data.getStringExtra("SCAN_RESULT");
byte[] b = Base64.decode(s.toCharArray());
// b is null at this point

Base64 code that it bugs out on:is

// Check special case
int sLen = str != null ? str.length() : 0;
if (sLen == 0)
    return new byte[0];

// Count illegal characters (including '\r', '\n') to know what size the returned array will be,
// so we don't have to reallocate & copy it later.
int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
for (int i = 0; i < sLen; i++)  // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out.
    if (IA[str.charAt(i)] < 0)
    sepCnt++;

// Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
if ((sLen - sepCnt) % 4 != 0)
    return null;

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

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

发布评论

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

评论(1

原谅我要高飞 2024-12-03 09:27:31

PHP base64 编码使用“+”符号。当将其放入 QR 码时,“+”会显示为空格。将“”替换为“+”,解码正常。

PHP base64 encode uses the '+' symbol. When this gets put into a QR code, the '+' comes through as space. Replaced ' ' with '+' and it got decoded fine.

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