【问题标题】:base64_encode() from php function to Java does not return same data从 php 函数到 Java 的 base64_encode() 不返回相同的数据
【发布时间】:2018-07-02 12:40:32
【问题描述】:

我正在开发一个需要对本地网站上创建的帐户进行身份验证的 java 项目。这些帐户的密码在插入数据库之前由 php 函数加密:

public function crypter($mdp){
    $j = 0;
    $tmp = 0;
    $key = $mdp;

    $res = "";

    for($i = 0; $i < strlen($mdp) ; $i++)
    {
        $tmp = ord($mdp[$i]) + ord($key[$i]);
        if($tmp > 255){
            $tmp = $tmp - 256;
        }

        $res[$i] = chr($tmp);

        if($j == strlen($key)-1){
            $j = 0;
        }
        else{
            $j = (($j % (strlen($key))) +1 );
        }
    }

    $res = base64_encode($res);

    return $res;
}

注意:这个函数不是我写的,所以如果你们弄清楚$j 变量的用途,请告诉我。我也(显然)无法更改此代码。

所以我尝试在 Java 中翻译这个函数:

public String cryptMdp(String mdp){
    String res = "";
    String key = mdp;

    int j = 0;
    int tmp = 0;

    for (int i = 0; i < mdp.length(); i++) {
        char c = mdp.charAt(i);
        char c2 = key.charAt(i);

        tmp = (int) c + (int) c2;
        if (tmp > 255) {
            tmp = tmp - 256;
        }

        res += (char) tmp;

        if (j == key.length() - 1){
            j = 0;
        }
        else{
            j = (j % key.length()) + 1;
        }
    }
    return Base64.getMimeEncoder().encodeToString(res.getBytes());
}

不幸的是,那些不返回相同的字符串(例如:对于字符串a,php 返回wg==,Java 返回w4I=。),即使根据this question 这应该是一个标准.

你们能看出我做错了什么吗?

【问题讨论】:

  • @Penguine 已经看到了,但是这个家伙的解决方案是使用 hmac,我没有在我的情况下使用它
  • 检查 java 和 php 中的字符集。它们可以不同。还有一件事 - java 在其字符串中使用 wchar。

标签: java php base64 encode


【解决方案1】:

使用 Base64 的现有 Java 功能: https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html

您可能正在寻找这种方法:

编码(字节[] src) 使用 Base64 编码方案将指定字节数组中的所有字节编码为新分配的字节数组。

【讨论】:

  • Sill 不工作 :( 我试过 Base64.getMimeEncoder().encode()Base64.getMimeEncoder().encodeToString()Base64.getEncoder().encode()Base64.getEncoder().encodeToString() 我得到的结果都是一样的
  • 您是否 150% 确定输入相同?
  • 直接比较它们,确保任何地方都没有不可见的字符。
  • 您也可以尝试调试这两种方法,以查看输出开始偏离的确切时间。这应该会提示您在哪里找到问题。
【解决方案2】:

我已经能够使用 URLConnection 向我的 PHP 页面发送 HTTP 请求来做我想做的事情,如下所示:

public String cryptMdp(String mdp){
    try {
        URL url = new URL("http://myaddress/someproject/create_pwd.php?pwd=" + mdp);
        URLConnection conn = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        String inputline = "";

        while((inputline = in.readLine()) != null){
            return inputline;
        }
        in.close();
    } catch (MalformedURLException ex) {
        System.out.println("error URL");
    } catch (IOException ex) {
        System.out.println("error connection");
    }
    return null;
}

问题是 PHP 在编码之前使用了未知字符,这意味着所有密码都被错误地加密...

编辑:

实际上密码在 IDE (Netbeans) 和构建的应用程序 (.jar) 中的加密方式不同,因此Base64.getMimeEncoder().encodeToString(res.getBytes()); 实际上产生的结果与 PHP 方法相同...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多