简体   繁体   English

Java中的Drupal样式base64编码

[英]Drupal style base64 encoding in java

I'm trying to convert this: 我正在尝试将其转换为:

function _password_itoa64() {
  return './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
}

function _password_base64_encode($input, $count) {
  $output = '';
  $i = 0;
  $itoa64 = _password_itoa64();
  do {
    $value = ord($input[$i++]);
    $output .= $itoa64[$value & 0x3f];
    if ($i < $count) {
      $value |= ord($input[$i]) << 8;
    }
    $output .= $itoa64[($value >> 6) & 0x3f];
    if ($i++ >= $count) {
      break;
    }
    if ($i < $count) {
      $value |= ord($input[$i]) << 16;
    }
    $output .= $itoa64[($value >> 12) & 0x3f];    
    if ($i++ >= $count) {
      break;
    }
    $output .= $itoa64[($value >> 18) & 0x3f];
  } while ($i < $count);       

  return $output;     
}

php code into Java and currently have this: php代码转换为Java,目前具有以下功能:

private static String _password_itoa64(){
    return "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
}

private String _password_base64_encode(byte[] hash, int count){
    StringBuffer output = new StringBuffer();
    String input = new String(hash);
    String itoa64 = _password_itoa64();
        int i = 0, value;
        do {
            value = input.charAt(i++);
            output.append(itoa64.charAt(value & 0x3f));
            if (i < count) {
                value |= (int)(input.charAt(i) << (char)8);
            }
            output.append(itoa64.charAt((value >> 6) & 0x3f));
            if (i++ >= count) {
                break;
            }
            if (i < count) {
                value |= (int)(input.charAt(i) << (char)16);
            }
            output.append(itoa64.charAt((value >> 12) & 0x3f));
            if (i++ >= count) {
                break;
            }
            output.append(itoa64.charAt((value >> 18) & 0x3f));
        }
        while (i < count);

        return output.toString();
}

When supplied with the same thing this happened: 当提供相同的东西时,会发生:

php - 6gL/BBSRbbJS7V.avvpcInZvukU4scZRsdWGwlPCG7R PHP- 6gL/BBSRbbJS7V.avvpcInZvukU4scZRsdWGwlPCG7R

java - 6gL/BBSRbbJS7V.rvvpcInZvukU4scZRsdWGwlPCG7R Java- 6gL/BBSRbbJS7V.rvvpcInZvukU4scZRsdWGwlPCG7R

input string - as hex its this 087b054de375e75979490898fb5ea3d45cee3a0c1a385a76782a4a7cbc3952d21d8b9523175d95d21c5eddb3efebb88733d8cb9de121b11683a41175429e1170 输入字符串-以十六进制表示的形式087b054de375e75979490898fb5ea3d45cee3a0c1a385a76782a4a7cbc3952d21d8b9523175d95d21c5eddb3efebb88733d8cb9de121b11683a41175429e1170

Any clues as to what caused the random non conforming character? 关于什么原因导致随机不合格字符的任何线索?

EDIT: 编辑:

Tracing down where it went wrong goes to value |= (int)(input.charAt(i) << (char)16); value |= (int)(input.charAt(i) << (char)16);错误的地方value |= (int)(input.charAt(i) << (char)16); in PHP this line sets value to be 9963593 with ord($input[$i]) is 152 and in Java it sets it to be 47974473 when input.charAt(i) is 732 在PHP中,此行将值设置为9963593,其中ord($input[$i])为152,在Java中,当input.charAt(i)为732时,其值设置为47974473

Guess this means I havnt chosen the right way of converting ord($input[$i]) into Java 猜猜这意味着我选择了将ord($input[$i])转换为Java的正确方法

I don't see a bug in your code. 我看不到您的代码中的错误。 Java's byte type is a signed 8 bit integer, so it stores values -128 to 127. Please ensure that you're passing the correct byte array into the function. Java的字节类型是一个带符号的8位整数,因此它存储-128到127的值。请确保将正确的字节数组传递给该函数。 This gets me a lot. 这让我受益匪浅。

There may be an issue where the byte array is being turned into a string: 字节数组被转换为字符串时可能存在问题:

String input = new String(hash);

It will use your default character set, and who knows what that may be. 它将使用您的默认字符集,谁知道这可能是什么。 Instead, I'd use the raw byte array so you don't have to worry about character sets. 相反,我将使用原始字节数组,因此您不必担心字符集。

I'd turn this: 我会把这个:

value = input.charAt(i++);

Into this: 变成这个:

value := (int)(hash[i++] & 0xFF);

Which is how we convert a byte that is storing a character (unsigned byte) into an integer. 这就是我们将存储字符的字节(无符号字节)转换为整数的方式。

As both PHP and Java use signed integers this can not be the difference, but may be the shift operators. 由于PHP和Java都使用带符号的整数,因此这不是什么区别,但可能是移位运算符。 Java has (in difference to PHP) two types of right shift operators, signed and unsigned: Java具有(与PHP不同)两种类型的右移运算符:有符号和无符号:

<<  Signed left shift
>>  Signed right shift
>>> Unsigned right shift

May be you need to use unsigned right shift operator? 可能您需要使用无符号右移运算符?

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

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