简体   繁体   English

额外的尾随零(或缺少零)Java / PHP

[英]Extra Trailing Zero (or missing zero) Java/PHP

Background 背景

I'm working on a Bukkit Plugin (Minecraft Server-side). 我正在开发Bukkit插件(Minecraft服务器端)。 The plugin allows for players to send messages back and forth to each other. 该插件允许玩家相互之间来回发送消息。 I am working on a web interface as well. 我也在网络界面上工作。 In order to view their 'inbox' they must first login from a password the can set in-game. 为了查看他们的“收件箱”,他们必须首先使用可以在游戏中设置的密码登录。

This password isn't stored raw, it is converted into a long string of unicode values, then broken up into pieces, each converted to hex and appended to a different string. 此密码不是原始存储的,它会转换为一长串unicode值,然后分解成段,每个都转换为十六进制并附加到其他字符串中。

Java version Java版本

//This isn't the best method, I know, but it's still going to take a genius to crack it.
//The resulting number (before somewhat converted to hex) is really
//long, there isn't an easy way of knowing the sequence of characters.
//This conversion is much different than straight up converting to hex,
//as PHP has certain limitations
public static String encodePassword(String password) {
    String longNumber = "";
    for(int i = 0; i < password.length(); i++) {
        longNumber += ((int) password.charAt(i));
    }
    //System.out.println("long = " + longNumber);
    String result = "";
    int splitLength = 5;
    int iterations = longNumber.length() / splitLength;
    if(longNumber.length() % splitLength > 0)
        iterations++;
    for(int i = 0; i < iterations; i++) {
        //System.out.println(result);
        int start = splitLength * i;
        if(longNumber.length() - start <= splitLength) {
            String sub = longNumber.substring(start);
            result += Integer.toHexString(Integer.parseInt(sub));
            continue;
        }
        String sub = longNumber.substring(start, start + splitLength);
        result += Integer.toHexString(Integer.parseInt(sub));
    }
    return result;
}

PHP version PHP版本

function encodePassword($pw){
    $unicode = "";
    for($i=0; $i<strlen($pw); $i++){
        $char = $pw{$i};
        $val = unicode_value($char);
        $unicode = $unicode.$val;
    }
    $result = "";
    $splitLength = 5;
    $iterations = strlen($unicode) / $splitLength;
    if(strlen($unicode) % $splitLength > 0)
        $iterations++;
    for($i = 0; $i < $iterations; $i++) {
        $start = $splitLength * $i;
        if(strlen($unicode) - $start <= $splitLength) {
            $sub = substr($unicode, $start);
            $result = $result.base_convert($sub, 10, 16);
            continue;
        }
        $sub = substr($unicode, $start, $splitLength);
        $result = $result.base_convert($sub, 10, 16);
    }
    return $result;
}

If I 'encode' the password "partychat" (the name of the plugin, it has a group chat functionality as well) I get 2c212c93ef23163a91bcc in Java, and 2c212c93ef23163a91bcc0 (same except for trailing 0) in PHP. 如果我对口令“ partychat”(插件的名称,也具有群聊功能)进行“编码”,那么我在Java中会得到2c212c93ef23163a91bcc ,在PHP中会得到2c212c93ef23163a91bcc0 (除了结尾0以外,其他都相同)。 Anything I'm doing wrong? 我做错了什么吗?

Note: This doesn't always happen, most 'encoding' works fine, but for some reason this case occurs sometimes 注意: 这种情况并非总是会发生,大多数“编码”都可以正常工作,但是由于某种原因,这种情况有时会发生

您为什么还要这么做,我只使用userpassword的哈希,例如: 这个关于SHA-256的stackoverflow问题 ,我知道它不能解决您的问题,但是更安全的是不发明自己的加密标准:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM