【发布时间】:2018-06-06 11:21:05
【问题描述】:
例如这将得到正确的 'A' 十六进制值 0x41:
StringBuffer strBuf = new StringBuffer();
strBuf.append(toBase64("A".getBytes()));
String ciphertext = strBuf.toString();
byte[] encryted_bytes = ciphertext.getBytes();
byte[] cipherBytes = fromBase64(new String(encryted_bytes));
StringBuilder sb = new StringBuilder();
for (byte b : cipherBytes) {
sb.append(String.format("%02X ", b));
}
Log.d("hole", "hex:" + sb.toString());
但是没有base64,这会得到5B 42 40 33 33 64 62 35 61 30:
StringBuffer strBuf = new StringBuffer();
strBuf.append(("A".getBytes()));
String ciphertext = strBuf.toString();
byte[] encryted_bytes = ciphertext.getBytes();
byte[] cipherBytes = new String(encryted_bytes).getBytes();
StringBuilder sb = new StringBuilder();
for (byte b : cipherBytes) {
sb.append(String.format("%02X ", b));
}
Log.d("hole", "hex:" + sb.toString());
base64的方法:
public static String toBase64(byte[] bytes) {
return Base64.encodeToString(bytes, Base64.NO_WRAP);
}
public static byte[] fromBase64(String base64) {
return Base64.decode(base64, Base64.NO_WRAP);
}
第二个代码生成5B 42 40 33 33 64 62 35 61 30的步骤是什么?以及base64如何使它生成正确的十六进制?
【问题讨论】:
标签: java arrays base64 byte stringbuffer