普通加密

public class MyBase64 {
/**
* 执行加密操作
*/
public static byte[] myEncode(byte[] data){
byte[] result; //加密结果
int modulus = data.length % 3;
if (modulus == 0) { //每三位补一位,剩余不为0则不够三位补一位 来确定加密后byte的长度
result = new byte[(4 * data.length) / 3];
} else {
result = new byte[4 * ((data.length / 3) + 1)];
}
int dataLength = (data.length - modulus); // 得到有多少个三位
int datatemp1;
int datatemp2;
int datatemp3;
for (int i = 0, j = 0; i < dataLength; i += 3, j += 4) { //加密前dataLength组三位元数据
datatemp1 = data[i] & 0xff;
datatemp2 = data[i + 1] & 0xff;
datatemp3 = data[i + 2] & 0xff;
result[j] = encodingTable[(datatemp1 >>> 2) & 0x3f];
result[j + 1] = encodingTable[((datatemp1 << 4) | (datatemp2 >>> 4)) & 0x3f];
result[j + 2] = encodingTable[((datatemp2 << 2) | (datatemp3 >>> 6)) & 0x3f];
result[j + 3] = encodingTable[datatemp3 & 0x3f];
}
//下面开始加密剩余不够三位的数据
int resulttemp1;
int resulttemp2;
int resulttemp3;
int datalasttemp1;
int datalasttemp2;
switch (modulus) {
case 0: // nothing left to do
break;
case 1: // left one to do
datalasttemp1 = data[data.length - 1] & 0xff;
resulttemp1 = (datalasttemp1 >>> 2) & 0x3f;
resulttemp2 = (datalasttemp1 << 4) & 0x3f;
result[result.length - 4] = encodingTable[resulttemp1];
result[result.length - 3] = encodingTable[resulttemp2];
result[result.length - 2] = (byte) '=';
result[result.length - 1] = (byte) '=';
break;
case 2: // left two to do
datalasttemp1 = data[data.length - 2] & 0xff;
datalasttemp2 = data[data.length - 1] & 0xff;
resulttemp1 = (datalasttemp1 >>> 2) & 0x3f;
resulttemp2 = ((datalasttemp1 << 4) | (datalasttemp2 >>> 4)) & 0x3f;
resulttemp3 = (datalasttemp2 << 2) & 0x3f;
result[result.length - 4] = encodingTable[resulttemp1];
result[result.length - 3] = encodingTable[resulttemp2];
result[result.length - 2] = encodingTable[resulttemp3];
result[result.length - 1] = (byte) '=';
break;
}
return result;
}
/**
* 执行解密操作
*/
public static byte[] myDecode(String data) {
byte[] result;
byte datatemp1;
byte datatemp2;
byte datatemp3;
byte datatemp4;
data = discardNonBase64Chars(data); //去除非本加密用到的字符
if (data.charAt(data.length() - 2) == '=') { //加密后的字符串倒数第二位为'=' 则原字符串的长度是三的倍数+1
result = new byte[(((data.length() / 4) - 1) * 3) + 1];
} else if (data.charAt(data.length() - 1) == '=') { //加密后的字符串倒数第一位为'=' 则原字符串的长度是三的倍数+2
result = new byte[(((data.length() / 4) - 1) * 3) + 2];
} else {
result = new byte[((data.length() / 4) * 3)]; //加密后的字符串倒数第二位和倒数第二位都不为'=' 则原字符串的长度是三的倍数
}
for (int i = 0, j = 0; i < (data.length() - 4); i += 4, j += 3) { //把前N组四位加密后的字符串转回前N组三位原字符串
datatemp1 = decodingTable[data.charAt(i)];
datatemp2 = decodingTable[data.charAt(i + 1)];
datatemp3 = decodingTable[data.charAt(i + 2)];
datatemp4 = decodingTable[data.charAt(i + 3)];
result[j] = (byte) ((datatemp1 << 2) | (datatemp2 >> 4));
result[j + 1] = (byte) ((datatemp2 << 4) | (datatemp3 >> 2));
result[j + 2] = (byte) ((datatemp3 << 6) | datatemp4);
}
if (data.charAt(data.length() - 2) == '=') { //加密后的字符串倒数第二位为'=' 则原字符串的长度是三的倍数+1 把加密后的字符串的倒数3、4位转回原字符串的最后一位
datatemp1 = decodingTable[data.charAt(data.length() - 4)];
datatemp2 = decodingTable[data.charAt(data.length() - 3)];
result[result.length - 1] = (byte) ((datatemp1 << 2) | (datatemp2 >> 4));
} else if (data.charAt(data.length() - 1) == '=') { //加密后的字符串倒数第一位为'=' 则原字符串的长度是三的倍数+2 把加密后的字符串的倒数2、3、4位转回原字符串的最后二位
datatemp1 = decodingTable[data.charAt(data.length() - 4)];
datatemp2 = decodingTable[data.charAt(data.length() - 3)];
datatemp3 = decodingTable[data.charAt(data.length() - 2)];
result[result.length - 2] = (byte) ((datatemp1 << 2) | (datatemp2 >> 4));
result[result.length - 1] = (byte) ((datatemp2 << 4) | (datatemp3 >> 2));
}
return result;
}
/**
* 去除非本加密用到的字符
*/
private static String discardNonBase64Chars(String data) {
StringBuffer sb = new StringBuffer();
int length = data.length();
for (int i = 0; i < length; i++) {
if (isValidBase64Byte((byte) (data.charAt(i)))) {
sb.append(data.charAt(i));
}
}
return sb.toString();
}
/**
* 判断是否是非本加密用到的字符
*/
private static boolean isValidBase64Byte(byte b) {
if (b == '=') {
return true;
} else if ((b < 0) || (b >= 128)) {
return false;
} else if (decodingTable[b] == -1) {
return false;
}
return true;
}
/**
* 解密用
*/
private static final byte[] decodingTable;
static {
decodingTable = new byte[128];
for (int i = 0; i < 128; i++) {
decodingTable[i] = (byte) -1;
}
for (int i = 'A'; i <= 'Z'; i++) {
decodingTable[i] = (byte) (i - 'A');
}
for (int i = 'a'; i <= 'z'; i++) {
decodingTable[i] = (byte) (i - 'a' + 26);
}
for (int i = '0'; i <= '9'; i++) {
decodingTable[i] = (byte) (i - '0' + 52);
}
decodingTable['+'] = 62;
decodingTable['/'] = 63;
}
/**
* 加密结果中的字符 64个字符
*/
private static final byte[] encodingTable = {
(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
(byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
(byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O',
(byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
(byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y',
(byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
(byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i',
(byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
(byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's',
(byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
(byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2',
(byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
(byte) '8', (byte) '9', (byte) '+', (byte) '/'
};
}

你可能感兴趣的:(F#,J#)