base64是一种编码格式,不算是加密算法.
下面呢是Base64编码的过程,这张图是从...忘记哪里拷贝的了.
原有字符串每三个一组,
将这一组转成ASCII码
然后将ASCII码转二进制(一个字节8位,不足在前边补0)
然后分为6个一组的二进制数.
然后将这四组二进制转化为10进制
A | B | C | D | E | F | G | H | I | G | 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 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
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 |
26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | + | / |
52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
然后转化为对应的Base64编码
还有就是一组不到3个的字符的处理
是先把有的进行转化,最后分6位二进制位的时候,最后一个不够补0,补够6位即可.
最后转化为Base64编码,4位一组,不够用=号代替
下面的东西就比较尴尬了,我不知道有很多包装好的base64的类....
下面拉上我自己写的base64编码类.主要实现都是一些基本的语法.逻辑就是上面的编码逻辑.可能会比较绕,但是你仔细看,其实挺简单的...
public class Base64Encode {
/**
* Base64编码
* 我表示代码自己写的,但是我估计看不懂了...尬,这个逻辑好绕.我刚写完脑子就有点懵了,不支持中文,不会解码
*/
public String Encode(String clearText){
String clear= clearText; //明文存放处
String pwdString = ""; //编码后的字符串
char[] clearChar = clear.toCharArray(); //明文字符数组
Integer length = clearChar.length; //明文长度
Integer group = (length % 3 == 0 ) ? (length / 3) : ((length / 3 + 1)); //组数
Integer[][] clearInteger = new Integer[group][3]; //ASCII码数组
String[][] clearString = new String[group][3]; //二进制数组(8位)
String[][] clearSixString = new String[group][4]; //二进制数组(6位)大小是这样的
System.out.println("编码前:" + clear + '\n');
//明文转ASCII码,每三个一组
Integer groupNum = 0;
Integer elementNum = 0;
for (Integer i = 0; i < clearChar.length; i++) {
clearInteger[groupNum][elementNum] = (int)clearChar[i];
elementNum++;
if (elementNum % 3 == 0) {
groupNum++;
elementNum = 0;
}
}
//测试数据
System.out.println("ASII码:");
for (Integer[] i : clearInteger) {
System.out.println(Arrays.toString(i));
}
System.out.println();
//ASCII码转二进制,三个一组
for (int i = 0; i < group; i++) {
for (int j = 0; j < clearInteger[i].length; j++) {
if (i == group - 1) { //为空的判断,因为明文可能不是3的倍数
if (clearInteger[i][j] == null) {
break;
}
}
clearString[i][j] = Integer.toBinaryString(clearInteger[i][j]); //十进制转二进制
//对二进制进行补全
for (int n = 0; n <= 8 - clearString[i][j].length(); n++) {
clearString[i][j] = "0" + clearString[i][j];
}
}
}
//测试数据
System.out.println("二进制编码(8bit一组):");
for (String[] s : clearString) {
System.out.println(Arrays.toString(s));
}
System.out.println();
//对二进制数据进行分割为6位一个的编码
for (int i = 0; i < group; i++) {
String sixString = ""; //每一组的二进制代码组合,暂时存放
for (int j = 0; j < clearString[i].length; j++) {
if (i == group - 1) { //为空的判断,因为明文可能不是3的倍数
if (clearString[i][j] == null) {
break;
}
}
sixString += clearString[i][j];
}
//判断字符串满足一组24位的,分别处理
int sixLength = sixString.length(); //正常处理的组数
for (int j = 0; j < sixString.length() / 6 * 6; j += 6) {
clearSixString[i][j / 6] = subString(j, j + 6, sixString);
}
Boolean firstNull = false;
if (sixLength < 24) {
for (int n = 0; n < clearSixString[clearSixString.length - 1].length; n++) {
if (clearSixString[clearSixString.length - 1][n] == null) {
if (!firstNull) {
clearSixString[clearSixString.length - 1][n] = subString(sixLength - sixLength % 6, sixLength, sixString);
for (int k = 0; k < 6 - sixLength % 6; k++) {
clearSixString[clearSixString.length - 1][n] += "0";
}
firstNull = true;
} else {
clearSixString[clearSixString.length - 1][n] = "=";
}
}
}
}
}
//测试数据
System.out.println("二进制编码(6bit一组)");
for (String[] s : clearSixString) {
System.out.println(Arrays.toString(s));
}
System.out.println();
//将二进制转十进制,十进制转Base64编码
System.out.println("编码后:");
for (int i = 0; i < clearSixString.length; i++) {
for (int j = 0; j < clearSixString[i].length; j++) {
if (clearSixString[i][j].equals("=")) {
pwdString += "=";
} else {
char pwd = base64(Integer.parseInt(clearSixString[i][j], 2));
pwdString += pwd;
}
}
}
return pwdString;
}
//Base64码表转化
public char base64(Integer code) {
char base64;
if (code >= 0 && code <= 25) {
base64 = (char) (65 + code);
} else if (code >= 26 && code <= 51) {
base64 = (char) (97 - 26 + code);
} else if (code >= 52 && code <= 61) {
base64 = (char) (52 - 48 + code);
} else if (code == 62) {
base64 = (char) 43;
} else {
base64 = (char) 47;
}
return base64;
}
//按索引截取字符串的方法
public String subString(Integer beginIndex, Integer endIndex, String str) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > str.length()) {
throw new StringIndexOutOfBoundsException(endIndex);
}
char[] strChar = str.toCharArray();
String sub = "";
for (Integer i = beginIndex; i < endIndex; i++) {
sub += strChar[i];
}
return sub;
}
}
代码就是上面的了,大体的实现可以了,但是还有些东西没写上......
还有就是解码,不会写.
写这个东西是因为一个Base64编码的手机号......
然后后来在java包里面有这个类,,,当时就想吐血了......