package com.util.encrypt;
/*******************************************************************************************
* 编码原理是将3个字节转换成4个字节( (3 X 8) = 24 = (4 X 6) )先读入3个字节,每读一个字节,左移8位,再右移四次,每次6位,这样就有4个字节了,
* 这样还不能保证得到的字符都是可见字符,为了达到此目的,Base64制定了一个编码表,进行统一的转换。码表的大小为2^6=64,这也是Base64名称的由来。
* 当剩下的字符数量不足3个字节时,则应使用0进行填充,相应的,输出字符则使用'='占位,因此编码后输出的文本末尾可能会出现1至2个'='。
* 解码原理是将4个字节转换成3个字节.先读入4个6位(用或运算),每次左移6位,再右移3次,每次8位,这样就还原了
* @ClassName: Base64
* @Description: Base64 加密和解密
* @date 2016年6月14日 上午9:53:58
********************************************************************************************/
public class Base64 {
// Base64使用A--Z,a--z,0--9,+,/ 这64个字符
final static String encodingChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/**
*
* @Title: encode
* @Description: (指定加密的字符串)
* @param source
* @return 设定文件
* @return String 返回类型
* @throws
*/
public static String encode( String source ) {
char[] sourceBytes = getPaddedBytes( source );
int numGroups = (sourceBytes.length + 2) / 3;
char[] targetBytes = new char[4];
char[] target = new char[ 4 * numGroups ];
for (int group = 0; group < numGroups; group++) {
convert3To4( sourceBytes, group*3, targetBytes );
for (int i = 0; i < targetBytes.length; i++) {
target[ i + 4*group ] = encodingChar.charAt( targetBytes[i] );
}
}
int numPadBytes = sourceBytes.length - source.length();
for (int i = target.length-numPadBytes; i < target.length; i++) target[i] = '=';
return new String( target );
}
private static char[] getPaddedBytes( String source ) {
char[] converted = source.toCharArray();
int requiredLength = 3 * ((converted.length+2) /3);
char[] result = new char[ requiredLength ];
System.arraycopy( converted, 0, result, 0, converted.length );
return result;
}
/**
*
* @Title: convert3To4
* @Description: (加密过程字符的移位)
* @param source
* @param sourceIndex
* @param target 设定文件
* @return void 返回类型
* @throws
*/
private static void convert3To4( char[] source, int sourceIndex, char[] target ) {
target[0] = (char) ( source[ sourceIndex ] >>> 2);
target[1] = (char) (((source[ sourceIndex ] & 0x03) << 4) | (source[ sourceIndex+1 ] >>> 4));
target[2] = (char) (((source[ sourceIndex+1 ] & 0x0f) << 2) | (source[ sourceIndex+2 ] >>> 6));
target[3] = (char) ( source[ sourceIndex+2 ] & 0x3f);
}
/**
*
* @Title: decode
* @Description: 对加密过后的字符串进行解密
* @param source
* @return 设定文件
* @return String 返回类型
* @throws
*/
public static String decode( String source ) {
if (source.length()%4 != 0) throw new RuntimeException( "valid Base64 codes have a multiple of 4 characters" );
int numGroups = source.length() / 4;
int numExtraBytes = source.endsWith( "==" ) ? 2 : (source.endsWith( "=" ) ? 1 : 0);
byte[] targetBytes = new byte[ 3*numGroups ];
byte[] sourceBytes = new byte[4];
for (int group = 0; group < numGroups; group++) {
for (int i = 0; i < sourceBytes.length; i++) {
sourceBytes[i] = (byte) Math.max( 0, encodingChar.indexOf( source.charAt( 4*group+i ) ) );
}
convert4To3( sourceBytes, targetBytes, group*3 );
}
return new String( targetBytes, 0, targetBytes.length - numExtraBytes );
}
/**
*
* @Title: convert4To3
* @Description: (解密时还原移位的问题)
* @param source
* @param target
* @param targetIndex 设定文件
* @return void 返回类型
* @throws
*/
private static void convert4To3( byte[] source, byte[] target, int targetIndex ) {
target[ targetIndex ] = (byte) (( source[0] << 2) | (source[1] >>> 4));
target[ targetIndex+1 ] = (byte) (((source[1] & 0x0f) << 4) | (source[2] >>> 2));
target[ targetIndex+2 ] = (byte) (((source[2] & 0x03) << 6) | (source[3]));
}
public static void main(String[] args) {
String str="Base64";
String encodeStr=Base64.encode(str);
System.out.println("base64加密======>"+encodeStr);
String decodestr= Base64.decode(encodeStr);
System.out.println("base64解密======>"+decodestr);
}
}
Base64
最新推荐文章于 2024-03-10 23:20:48 发布