Java base64 编码、解码
public class uploadServletTest {
@Test
public void name() throws IOException {
String content = "这里是Base64编码的内容";
//编码
BASE64Encoder base64Encoder = new BASE64Encoder();
String encodeStr = base64Encoder.encode(content.getBytes("utf-8"));
System.out.println(encodeStr);
//解码
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] bytes = base64Decoder.decodeBuffer(encodeStr);
String s = new String(bytes, "utf-8");
System.out.println(s);
}
}
手动。。
public static String decipher(String s) {
int[] a = {4, 9, 6, 2, 8, 7, 3};
char[] res = new char[s.length()];
for (int i = 0, j = 0; j < s.length(); j++, i = (i + 1) % 7) {
res[j] = (char) ((byte) s.charAt(j) + a[i]);
if ((byte) res[j] > 122)
res[j] = (char) ((byte) res[j] - 90);
}
return String.valueOf(res);
}
public static String encipher(String s) {
int[] a = {4, 9, 6, 2, 8, 7, 3};
char[] res = new char[s.length()];
for (int i = 0, j = 0; j < s.length(); j++, i = (i + 1) % 7) {
res[j] = (char) ((byte) s.charAt(j) - a[i]);
if ((byte) res[j] < 32)
res[j] = (char) ((byte) res[j] + 90);
}
return String.valueOf(res);
}