我必须去codeA的base64 EN codeD数据从一个PHP服务器接收。
服务器使用'base64_en code'来恩code中的数据。
在我的Android应用程序,我用android.utils.Base64类来实现解码。
原始加密数据=†+ U] M(‡?= NO
在PHP Base64编码的数据给出了...... - hisP3F1NBCgIAocQCD3x9g == 在Android的Base64编码的数据给出了 - 4oCgKw / DnF1NBCgIAuKAoRAIPcOxw7Y =
正如你所看到的,Java的EN codeD字符串比PHP连接codeD较长。请给您的宝贵建议。我需要找出它们的默认编码格式。
如何从双方得到相同的EN codeD字符串。?
的Java / Android的code:
字符串加密=†+ U] M(‡= NO?;
byte []的EN codeD = Base64.en code(encrypted.getBytes(),Base64.DEFAULT);
字符串str =新的String(EN codeD); //海峡=4oCgKw / DnF1NBCgIAuKAoRAIPcOxw7Y =
解决方案
试试这个在Java中:这会给你的长版的字符串(UTF-8)
字节[] EN codeD = Base64.en code(encrypted.getBytes(UTF-8),Base64.DEFAULT);
字符串str =新的String(EN codeD,UTF-8);
更新:
试试这个在Java中:这会给你的短版字符串(CP1252)的
//这应该给了相同的结果PHP
byte []的EN codeD = Base64.en code(encrypted.getBytes(CP1252),Base64.DEFAULT);
字符串str =新的String(EN codeD,CP1252);
另外试试这个PHP脚本:
文件:test.php的
< PHP
回声base64_en code($ _ GET ['海峡'])默认UTF-8版< BR />中。
回声base64_en code(的iconv(UTF-8,CP1252,$ _ GET ['海峡']))CP1252版< BR />中。
?>
用法:HTTP:// [SOMEDOMAIN]/test.php?str=†+Ü] M(‡= NO
I have to decode a base64 encoded data received from a php server.
The server uses 'base64_encode' to encode the data.
In my android app, I use android.utils.Base64 class to do the decoding.
original encrypted data = "†+Ü]M(‡=ñö"
Base64 encoding data in php gives...... - "hisP3F1NBCgIAocQCD3x9g==" Base64 encoding data in android gives - "4oCgKw/DnF1NBCgIAuKAoRAIPcOxw7Y="
As you can see, the java encoded string is longer than the php encoded. Please give your valuable suggestions. I need to find out their default encoding formats.
How to get the same encoded string from both.?
java/android code :
String encrypted = "†+Ü]M(‡=ñö";
byte[] encoded = Base64.encode(encrypted.getBytes(), Base64.DEFAULT);
String str = new String(encoded); //str = "4oCgKw/DnF1NBCgIAuKAoRAIPcOxw7Y="
解决方案
Try this in Java: This will give you the long version of the string (UTF-8)
byte[] encoded = Base64.encode(encrypted.getBytes("UTF-8"), Base64.DEFAULT);
String str = new String(encoded, "UTF-8");
Updated:
Try this in Java: This will give you the short version of the string (CP1252)
// This should give the same results as in PHP
byte[] encoded = Base64.encode(encrypted.getBytes("CP1252"), Base64.DEFAULT);
String str = new String(encoded, "CP1252");
Alternatively try this PHP Script:
file: test.php
<?php
echo base64_encode($_GET['str'])." Default UTF-8 version<br />";
echo base64_encode(iconv("UTF-8","CP1252",$_GET['str']))." CP1252 Version <br />";
?>
usage: http://[SOMEDOMAIN]/test.php?str=†+Ü]M(‡=ñö
相关推荐
最新文章