Bean64加密算法

package com.yunxiao.copyBase;


public class Base64 {
public static char BASETABLE[] = { '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', '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', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '+', '/', '=' };


/*
* 编码算法 1.将数据按3个字节一组分成数块; 2.每块将3个8位的数据转换成4个6位数据段; 11111111 00000000 11111111
* ---- 111111 110000 000011 111111 3.根据Base64字符表得到4个6位数据段对应的字符;
* 4.如果最后一块只有两个字节,则添加两个0位,转换成对应Base64字符表的三个字符,并在结尾添一个 '= '字符;
* 如果最后一块只有一个字节,则添加四个0位,转换成对应Base64字符表的两个字符,并在结尾添两个 '= '字符。
*/
public static String encode(String text) {
StringBuffer code = new StringBuffer();
int textLength = text.length();
int blockCount = (int) textLength / 3;
char[] textChars = new char[textLength];
int ch, bits;
text.getChars(0, textLength, textChars, 0);


for (int i = 0; i < blockCount; i++) {
ch = (int) textChars[0 + i * 3];
ch = ch >>> 2;
code.append(BASETABLE[ch]);
bits = (int) textChars[0 + i * 3] - ch * 4;
ch = (int) textChars[1 + i * 3];
ch = ch >>> 4;
code.append(BASETABLE[ch + bits * 16]);
bits = (int) textChars[1 + i * 3] - ch * 16;
ch = (int) textChars[2 + i * 3];
ch = ch >>> 6;
code.append(BASETABLE[ch + bits * 4]);
bits = (int) textChars[2 + i * 3] - ch * 64;
code.append(BASETABLE[bits]);
}
if ((textLength % 3) != 0) {
ch = (int) textChars[blockCount * 3];
ch = ch >>> 2;
code.append(BASETABLE[ch]);
bits = (int) textChars[blockCount * 3] - ch * 4;
switch (textLength % 3) {
case 1:
code.append(BASETABLE[bits * 16]);
code.append(BASETABLE[64]);
code.append(BASETABLE[64]);
break;
case 2:
ch = (int) textChars[1 + blockCount * 3];
ch = ch >>> 4;
code.append(BASETABLE[ch + bits * 16]);
bits = (int) textChars[1 + blockCount * 3] - ch * 16;
code.append(BASETABLE[bits * 4]);
code.append(BASETABLE[64]);
break;
}
}


return code.toString();
}


/*
* 解码算法 1.将数据按4个字节一组分成数块; 2.每块将4个字符去掉最高两位并转换成3个8位的数据段;
* 注意:数据块中的字符取值不是ASCII集的值,而是Base64字符表中相对应的索引值! 00 111111 00 110000 00
* 000011 00 111111 ---- 11111111 00000000 11111111
* 3.根据ASCII字符集得到3个8位数据段对应的字符; 4.如果最后一块只有两个 '= ',去掉两个 '=
* ',并去掉最低两位,转换成对应ASSCII字符集的两个字符; 如果最后一块只有一个 '= ',去掉 '=
* ',并去掉最低四位,转换成对应ASSCII字符集的一个字符。
*/
public static String decode(String code) {

StringBuffer text = new StringBuffer();
int codeLength = code.length();
int blockCount = (int) codeLength / 4;
char[] codeChars = new char[codeLength];
int ch, bits;
code.getChars(0, codeLength, codeChars, 0);


for (int i = 0; i < blockCount; i++) {
bits = indexOfBase64Table(codeChars[1 + i * 4]) >>> 4;
ch = indexOfBase64Table(codeChars[0 + i * 4]) * 4 + bits;
text.append((char) ch);
if (codeChars[2 + i * 4] != '=') {
ch = (indexOfBase64Table(codeChars[1 + i * 4]) - bits * 16) * 16;
bits = indexOfBase64Table(codeChars[2 + i * 4]) >>> 2;
ch = ch + bits;
text.append((char) ch);
if (codeChars[3 + i * 4] != '=') {
ch = (indexOfBase64Table(codeChars[2 + i * 4]) - bits * 4)
* 64 + indexOfBase64Table(codeChars[3 + i * 4]);
text.append((char) ch);
}
}
}


return text.toString();
}


private static int indexOfBase64Table(char ch) {
for (int i = 0; i < 64; i++)
if (ch == BASETABLE[i])
return i;
return -1;
}


public static void main(String[] args) {
try {
String mingwen = "admin";
// String mingwen="pwd123";
String miwen = "";
Base64 base = new Base64();
System.out.println("加密前:[" + mingwen + "]");
miwen = base.encode(mingwen);
System.out.println("加密后:[" + miwen + "]");
System.out.println("解密后:[" + base.decode(miwen) + "]");


} catch (Exception e) {
e.printStackTrace();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盒马coding

你的支持是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值