Java和PHP中的Base64

Java和PHP中的Base64,java,php,base64,Java,Php,Base64,以下Java代码: String ss = Base64.encodeBase64URLSafeString(xmlRequest.getBytes()); System.out.println(ss); 产生: PHJlcXVlc3Q-PG1lcmNoYW50X2lkPjQ2PC9tZXJjaGFudF9pZD48b3JkZXJfaWQ-MzM8L29yZGVyX2lkPjxhbW91bnQ-MzwvYW1vdW50PjxkZXNjcmlwdGlvbj5oZWhlPC9kZXNjcmlwdG

以下Java代码:

String ss = Base64.encodeBase64URLSafeString(xmlRequest.getBytes());
System.out.println(ss);
产生:

PHJlcXVlc3Q-PG1lcmNoYW50X2lkPjQ2PC9tZXJjaGFudF9pZD48b3JkZXJfaWQ-MzM8L29yZGVyX2lkPjxhbW91bnQ-MzwvYW1vdW50PjxkZXNjcmlwdGlvbj5oZWhlPC9kZXNjcmlwdGlvbj48L3JlcXVlc3Q-
而这段PHP代码:

$xml='<request><merchant_id>46</merchant_id><order_id>33</order_id><amount>3</amount><description>hehe</description></request>';
$xml_encoded = base64_encode($xml);
$xml='46333hehe';
$xml_encoded=base64_encode($xml);
产生: PHJlcXVlc3Q+PG1lcmNoYW50X2lkPjQ2PC9tZXJjaGFudF9pZD48b3JkZXJfaWQ+MZM8L29YZGVYX2LKJXBW91BNQ+MZWVYW1VDW50PJKZXNJCMLWDGLBVBJ5OZWHLPC9KZXNJCMLWDGLBVBJ48L3JLCXVLC3Q+

其中一个有
-
字符,而另一个有
+
。差异从何而来?

有关使用中的一些变体,请参阅。当表示需要安全地包含在URL中(例如作为查询参数)时,似乎使用了
-
。另请参见源代码:

/**
 * This array is a lookup table that translates 6-bit positive integer index values into their "Base64 Alphabet"
 * equivalents as specified in Table 1 of RFC 2045.
 *
 * Thanks to "commons" project in ws.apache.org for this code.
 * http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
 */
private static final byte[] STANDARD_ENCODE_TABLE = {
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};

/**
 * This is a copy of the STANDARD_ENCODE_TABLE above, but with + and /
 * changed to - and _ to make the encoded Base64 results more URL-SAFE.
 * This table is only used when the Base64's mode is set to URL-SAFE.
 */
private static final byte[] URL_SAFE_ENCODE_TABLE = {
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
};
有关正在使用的一些变体,请参阅。当表示需要安全地包含在URL中(例如作为查询参数)时,似乎使用了
-
。另请参见源代码:

/**
 * This array is a lookup table that translates 6-bit positive integer index values into their "Base64 Alphabet"
 * equivalents as specified in Table 1 of RFC 2045.
 *
 * Thanks to "commons" project in ws.apache.org for this code.
 * http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
 */
private static final byte[] STANDARD_ENCODE_TABLE = {
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};

/**
 * This is a copy of the STANDARD_ENCODE_TABLE above, but with + and /
 * changed to - and _ to make the encoded Base64 results more URL-SAFE.
 * This table is only used when the Base64's mode is set to URL-SAFE.
 */
private static final byte[] URL_SAFE_ENCODE_TABLE = {
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
};
这是因为所使用的“URL安全Base64变体”将
+
/
替换为
-

使用base64算法的URL安全变体对二进制数据进行编码,但不分块输出。url安全变体发出
-
-
而不是
+
/
字符。注意:未添加任何填充

您只需将
+
/
更改为
-
\
即可翻译这两种格式

要获得与PHP相同的输出,只需使用。

这是因为所使用的“URL安全Base64变量”将
+
/
替换为
-
\uuu

使用base64算法的URL安全变体对二进制数据进行编码,但不分块输出。url安全变体发出
-
-
而不是
+
/
字符。注意:未添加任何填充

您只需将
+
/
更改为
-
\
即可翻译这两种格式


要获得与PHP相同的输出,只需使用。

Base 64是字符
a-zA-Z0-9
,以及后两个字符的
+
/

为了使编码URL安全,我们不能使用
/
,因此最后两个字符将替换为URL安全变体
\uu
-

如果您使用:

final Base64.Encoder encoder = Base64.getEncoder();
encoder.encodeToString(...)
您将获得与PHP示例相同的输出


另外,这是JDK内置的新的
java.util.Base64
类-无需使用神秘的公共库。

Base64是字符
a-zA-Z0-9
,以及后两个字符的
+
/

为了使编码URL安全,我们不能使用
/
,因此最后两个字符将替换为URL安全变体
\uu
-

如果您使用:

final Base64.Encoder encoder = Base64.getEncoder();
encoder.encodeToString(...)
您将获得与PHP示例相同的输出

另外,这是JDK内置的新
java.util.Base64
类-无需使用奥术公共库。

请参阅-当JDK中有设施时不要使用奥术第三部分库。请参阅-当JDK中有设施时不要使用奥术第三部分库。