base64是把8位字符打散,轉換成不被人直接識別的形式,嚴格來講它並非加密算法,只能算作一種編碼方式java
['A', 'B', 'C', ... 'a', 'b', 'c', ... '0', '1', ... '+', '/']算法
/** * BASE64解密 * * @param key * @return * @throws Exception */ public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } /** * BASE64加密 * * @param key * @return * @throws Exception */ public static String encryptBASE64(byte[] key) throws Exception { return (new BASE64Encoder()).encodeBuffer(key); }
package com.gbz.lemon.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyBase64 { private static Logger log = LoggerFactory.getLogger(MyBase64.class); private static final int SIGN_GREP_SIZE = 3; private static final int UNSIGN_GREP_SIZE = 4; private static final String HIGH_FILL = "0" ; private static final String COVER = "="; public static final String[] BASE_CHARS = { "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", "+", "/" }; /** * 加密 * * @param src * @return */ public static String encoder(String src) { String[][] grepString = signGrepString(src); if ( grepString == null) { return null; } StringBuffer ret = new StringBuffer(); String end = ""; int grepi = grepString. length; log.debug( "字符串 {} 被分紅了 {} 份" , src , grepi); for ( int j = 0; j < grepi; j++) { String[] strings = grepString[ j]; String grepStrToBinary = grepStrToBinary(strings); int indexOf = grepStrToBinary.indexOf(COVER ); if ( indexOf > 0) { end = grepStrToBinary.substring(indexOf ); grepStrToBinary = grepStrToBinary.substring(0, indexOf ); } String[][] targat = grepString(grepStrToBinary, 6); log.debug( "{}-->{},分組:{}" , strings , grepStrToBinary, targat ); int targatSize = targat. length; for ( int i = 0; i < targatSize; i++) { String[] strings2 = targat[ i]; StringBuffer sb = new StringBuffer(); for ( int h = 0; h < strings2. length; h++) { sb.append( strings2[ h]); } String s = sb.toString(); int index = Integer. parseInt(s, 2); ret.append( BASE_CHARS[index ]); } } return ret.toString() + end; } public static String decoder(String src) { int coverIndex = src.indexOf( COVER); String noCoverSrc = src; String cover = ""; if ( coverIndex > 0) { cover = src.substring( coverIndex); noCoverSrc = src.substring(0, coverIndex); } else { coverIndex = 0; } String[][] nusignGrepString = nusignGrepString(noCoverSrc); if ( nusignGrepString == null) { return null; } StringBuffer indexs = new StringBuffer(); int strlen = nusignGrepString. length; log.debug( "待解密字符串被 分爲{}組" , strlen ); for ( int i = 0; i < strlen; i++) { String[] strings = nusignGrepString[ i]; String index = signStrToBaseChasreIndex(strings); indexs.append( index); log.debug( "{}-->{}", strings , index ); } StringBuffer decoderStr = new StringBuffer(); String[][] targat = grepString(indexs.toString(), 8); String[] tar = null; int maxlen = targat. length - cover.length(); for ( int i = 0; i < maxlen; i++) { tar = targat[ i]; String stringFromBinary = binaryToString(tar); decoderStr.append( stringFromBinary); } return decoderStr.toString(); } private static String binaryToString(String[] tar) { StringBuffer str = new StringBuffer(); for (String c : tar) { str.append( c); } char c = ( char) Integer.parseInt(str.toString(),2); return c + ""; } /** * 把待解密字符轉換成字符庫下標 * * @param str * @return */ private static String signStrToBaseChasreIndex(String[] str ) { StringBuffer sb = new StringBuffer(); int len = str. length; int baseLen = BASE_CHARS.length ; for ( int i = 0; i < len; i++) { String string = str[ i]; for ( int j = 0; j < baseLen; j++) { if ( BASE_CHARS[j ].equals(string )) { String binaryString = Integer.toBinaryString(j); String highFILL = highFILL(binaryString, 6); sb.append( highFILL); } } } return sb.toString(); } /** * 把字符數組轉成二進制形式 * * @param grepStr * @return */ private static String grepStrToBinary(String[] grepStr) { String endStr = ""; String binaryStr = ""; String str = ""; for ( int i = 0; i < SIGN_GREP_SIZE; i ++) { str = grepStr[ i]; if ( str == null) { str = HIGH_FILL; endStr += COVER; } char charAt = str.charAt(0); String binaryString = Integer.toBinaryString(charAt); binaryString = highFILL(binaryString); binaryStr += binaryString; } return binaryStr + endStr; } /** * 對二進制補碼 * * @param binaryStr * @param size * @return */ private static String highFILL(String binaryStr, int size) { int length = binaryStr.length(); for (; length < size; length = binaryStr.length()) { binaryStr = HIGH_FILL + binaryStr ; } return binaryStr; } /** * 對二進制碼,不足8位的進行補充 * * @param binaryStr * @return */ private static String highFILL(String binaryStr) { return highFILL(binaryStr, 8); } private static String[][] nusignGrepString(String str) { return grepString(str, UNSIGN_GREP_SIZE); } private static String[][] signGrepString(String str) { return grepString(str, SIGN_GREP_SIZE); } /** * 將字符串n個一組 * * @param str * @param grepSize * @return */ private static String[][] grepString(String str, int grepSize) { if ( str == null || "".equals( str)) { log.error( "待分組數組爲空" ); return null; } int size = str.length(); int grepArraySize = size / grepSize; if ( size % grepSize > 0) { grepArraySize++; } String[][] grep = new String[ grepArraySize][]; int grepi = 0; for ( int i = 0; i < size;) { grep[ grepi] = new String[ grepSize]; for ( int subGrepi = 0; subGrepi < grepSize && i < size ; subGrepi ++) { grep[ grepi][ subGrepi] = str.substring( i, ++ i); } grepi++; } return grep; } public static void main(String[] args) { String s = encoder("aasdg44asgs"); System. out.println( s); System. out.println( decoder(s)); } }