我正在使用以下代码,但它不起作用。
String source = "password";
byte[] byteArray = source.getBytes("UTF-16");
Base64 bs = new Base64();
//bs.encodeBytes(byteArray);
System.out.println(bs.encodeBytes(byteArray));
//bs.decode(bs.encodeBytes(byteArray));
System.out.println(bs.decode(bs.encodeBytes(byteArray)));
First:
- 选择一种编码。 UTF-8 通常是一个不错的选择;坚持对双方都有效的编码。很少使用 UTF-8 或 UTF-16 以外的其他格式。
发送端:
- 将字符串编码为字节(例如
text.getBytes(encodingName)
)
- 使用以下命令将字节编码为 base64
Base64
class
- 传输base64
接收端:
- 接收base64
- 使用以下命令将 base64 解码为字节
Base64
class
- 将字节解码为字符串(例如
new String(bytes, encodingName)
)
所以像这样:
// Sending side
byte[] data = text.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
Or with StandardCharsets
:
// Sending side
byte[] data = text.getBytes(StandardCharsets.UTF_8);
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, StandardCharsets.UTF_8);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)