📄 base64.java
字号:
/****************************************************************************
* Package : com.ecSolutions.ecAppServer.util
* File : Base64.java
* Create Date : 2007-7-27
* Author : Steven Chen
*
* Copyright(C) 2006 ecSolutions(shanghai) Co.,Limited.All Rights Reserved.
*
***************************************************************************/
package com.ecSolutions.ecAppServer.util;
/**
*
* @author Steven Chen
* @version $Id$
*
*/
public class Base64 {
/**
* 使用Base64算法加密字符串,使用系统字符集取byte数组,
* 如要加密中文字符串,请使用方法bytesToBase64(byte[] b)。
*
* @param 原字符串
* @return Base64加密的字符串
*/
public static String encode(String s) throws Exception {
return bytesToBase64(s.getBytes());
}
/**
* 解密使用Base64算法加密的字符串
*
* @param Base64加密的字符串
* @return 原字符串
*/
public static String decode(String s) throws Exception {
byte encodechar[] = base64ToBytes(s);
return new String(encodechar);
}
/**
* 解密使用Base64算法加密的字符串
*
* @param Base64加密的字符串
* @return 原字符串的byte数组
*/
public static final byte[] base64ToBytes(String s) {
int k = 0;
char c[] = s.toCharArray();
int i = c.length;
int j = 0;
if (c[i - 1] == '=') {
j++;
}
if (c[i - 2] == '=') {
j++;
}
i = (i / 4) * 3 - j;
byte[] bt = new byte[i];
int l = 0;
int m = 0;
while(l + 3 < c.length) {
k = base64Int(c[l + 0]) << 18 | base64Int(c[l + 1]) << 12 | base64Int(c[l + 2]) << 6
| base64Int(c[l + 3]);
l += 4;
if(m >= i - 2)
break;
bt[m++] = (byte)(k >> 16 & 0xff);
bt[m++] = (byte)(k >> 8 & 0xff);
bt[m++] = (byte)(k & 0xff);
}
if (m < bt.length) {
bt[m++] = (byte)(k >> 16 & 0xff);
}
if (m < bt.length) {
bt[m++] = (byte)(k >> 8 & 0xff);
}
return bt;
}
/**
* 使用Base64算法加密原byte数组
*
* @param 原byte数组
* @return 加密字符串
*/
public static final String bytesToBase64(byte[] b) {
int m = 0;
int n = 0;
int i = (((b.length + 3) - 1) / 3) * 4;
char c[] = new char[i];
for (int j = b.length - 2; m < j;) {
int k = (b[m + 0] & 0xff) << 16 | (b[m + 1] & 0xff) << 8 | b[m + 2] & 0xff;
m += 3;
c[n++] = base64Char(k >> 18 & 0x3f);
c[n++] = base64Char(k >> 12 & 0x3f);
c[n++] = base64Char(k >> 6 & 0x3f);
c[n++] = base64Char(k & 0x3f);
}
if (m < b.length - 1) {
int l = (b[m + 0] & 0xff) << 16 | (b[m + 1] & 0xff) << 8;
c[n++] = base64Char(l >> 18 & 0x3f);
c[n++] = base64Char(l >> 12 & 0x3f);
c[n++] = base64Char(l >> 6 & 0x3f);
c[n++] = '=';
} else if (m < b.length) {
int o = (b[m + 0] & 0xff) << 16;
c[n++] = base64Char(o >> 18 & 0x3f);
c[n++] = base64Char(o >> 12 & 0x3f);
c[n++] = '=';
c[n++] = '=';
}
return new String(c, 0, n);
}
private static final char base64Char(int i) {
if (i >= 52) {
if (i < 62) {
return (char)((i - 52) + 48);
}
if (i == 62) {
return '+';
}
return i != 63 ? '?' : '/';
}
if (i >= 26) {
return (char)((i - 26) + 97);
}
if (i >= 0) {
return (char)((i - 0) + 65);
} else {
return '?';
}
}
private static final int base64Int(char c) {
if (c >= 'a') {
if (c <= 'z') {
return (c - 97) + 26;
} else {
return -1;
}
}
if (c >= 'A') {
if (c <= 'Z') {
return (c - 65) + 0;
} else {
return -1;
}
}
if (c >= '0') {
if (c <= '9') {
return (c - 48) + 52;
}
return c != '=' ? -1 : 0;
}
if (c == '+') {
return 62;
}
return c != '/' ? -1 : 63;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -