code不会由Android获得德codeD

code不会由Android获得德codeD

本文介绍了PHP的base64 EN code不会由Android获得德codeD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建一个QR code OUT用户名和时间戳的PHP脚本。我需要证明的QR code从我的服务器来,所以我加密的用户名和时间戳与RSA私钥。为了得到它进入QR code我那么Base64编码它。

我与Android首次试用它。我能得到串出的QR code,但是当我去采用base64 code它的Andr​​oid,则返回null。调试后,现在看来,这是因为有串两个空格。当去codeR来检查非法字符都被4整除,它显然失败。越来越绝望我删除了空格但后来它改变长度,以便计算仍然没有发挥出来。我可以改变空白的'安全'的性格吗?或者是特定的EN code /德code对不兼容?

PHP code:

  $数据= base64_en code($用户名`$时间戳);
$计划生育=的fopen(private.pem,R);
$ PRIVATE_KEY = FREAD($ FP,8192);
FCLOSE($ FP);
openssl_private_encrypt($的数据,和放大器; $ encrypted_data,$ PRIVATE_KEY);
$ encrypted_data_64 = base64_en code($ encrypted_data);
//创建QR code

Android的code:

 一个String = data.getStringExtra(SCAN_RESULT);
字节[] B = Base64.de code(s.toCharArray());
// b为在这一点空

Base64的code,这虫子出来:是

  //检查特例
INT SLEN = str中!= NULL? str.length():0;
如果(SLEN == 0)
    返回新的[0]字节;//计数非法字符(包括'\\ r','\\ n')知道返回的数组将是什么规模,
//所以我们不必重新分配和放大器;后来复制。
INT sepCnt = 0; //的分隔符数。 (实际上非​​法字符,但是这是一个奖金...)
的for(int i = 0; I< SLEN;我++)//如果输入为纯(即无行分隔符或非法字符)的base64这个循环可以被注释掉。
    如果(ⅠA[str.charAt(ⅰ)]℃的)
    sepCnt ++;//检查使合法的字符在RFC 2045规定(包括'=')均匀divideable 4。
如果((SLEN - !sepCnt)%4 = 0)
    返回null;


解决方案

PHP的base64 EN code使用的+符号。当这个被放入一个QR code时,+为空间来通过。 '与'+'代替',它得到了德codeD的罚款。

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;
解决方案

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.

这篇关于PHP的base64 EN code不会由Android获得德codeD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:44