Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,可用于在HTTP环境下传递较长的标识信息。详细的Base64信息,可以参见维基百科:https://en.wikipedia.org/wiki/Base64。今天,我们开始java中base64算法的使用。
结构导航
Java自带的Base64算法 commons-codec的Base64算法 bouncycastle的Base64算法 自定义的base64算法 友情链接项目结构如下
一、引入jdk自带的rt.jar包
要使用Base64Encoder,却发现在Eclipse中无法找到该类,原来Base64Encoder并不属于JDK标准库范畴,但是又包含在了JDK中。
问题的解决方案:Project->Properties,选择JavaBuildPath设置项,再选择Libraries标签,AddExternalJars添加%JAVA_HOME%\jre\lib\rt.jar就行。
二、我们通过junit方法,来加以对Base64算法的测试,明文:mynameishuhx。
privatestaticStringsrc="mynameishuhx"; @Test publicvoidhuhxBase64(){ try{ BASE64Encoderencoder=newBASE64Encoder(); Stringencode=encoder.encode(src.getBytes()); System.out.println("encode:"+encode); BASE64Decoderdecoder=newBASE64Decoder(); Stringdecode=newString(decoder.decodeBuffer(encode)); System.out.println("decode:"+decode); }catch(IOExceptione){ e.printStackTrace(); } }三、运行结果如下:
encode:bXkgbmFtZSBpcyBodWh4 decode:mynameishuhx一、引入commons-codec-1.10.jar包,提供对base64算法的支持
commons-codec-1.10.jar地址:https://www.bouncycastle.org/latest_releases.html
二、我们通过junit方法,来加以对Base64算法的测试,明文:mynameishuhx。
@Test publicvoidcommonCodeBase64(){ byte[]encodeBytes=Base64.encodeBase64(src.getBytes()); System.out.println("encode:"+newString(encodeBytes)); byte[]decodeBytes=Base64.decodeBase64(encodeBytes); System.out.println("decode:"+newString(decodeBytes)); }三、运行结果如下:
encode:bXkgbmFtZSBpcyBodWh4 decode:mynameishuhx一、引入bcprov-jdk15on-154.jar,提供对base64算法的支持
bcprov-jdk15on-154.jar地址:https://commons.apache.org/proper/commons-codec/download_codec.cgi
二、我们通过junit方法,来加以对Base64算法的测试,明文:mynameishuhx。
@Test publicvoidbouncyCodeBase64(){ byte[]encodeBytes=org.bouncycastle.util.encoders.Base64.encode(src.getBytes()); System.out.println("encode:"+newString(encodeBytes)); byte[]decodeBytes=org.bouncycastle.util.encoders.Base64.decode(encodeBytes); System.out.println("decode:"+newString(decodeBytes)); }三、运行结果如下:
encode:bXkgbmFtZSBpcyBodWh4 decode:mynameishuhx一、HuhxBase64.java的自定义算法,内容如下:
packagecom.huhx.base64.jdk; /** *writer:huhx */ publicclassHuhxBase64{ privatestaticfinalStringCODES="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; //base64解密 privatestaticbyte[]base64Decode(Stringinput){ if(input.length()%4!=0){ thrownewIllegalArgumentException("Invalidbase64input"); } bytedecoded[]=newbyte[((input.length()*3)/4)-(input.indexOf(=)>0?(input.length()-input.indexOf(=)):0)]; char[]inChars=input.toCharArray(); intj=0; intb[]=newint[4]; for(inti=0;i<inChars.length;i+=4){ //Thiscouldbemadefaster(butmorecomplicated)byprecomputing //theseindexlocations. b[0]=CODES.indexOf(inChars[i]); b[1]=CODES.indexOf(inChars[i+1]); b[2]=CODES.indexOf(inChars[i+2]); b[3]=CODES.indexOf(inChars[i+3]); decoded[j++]=(byte)((b[0]<<2)|(b[1]>>4)); if(b[2]<64){ decoded[j++]=(byte)((b[1]<<4)|(b[2]>>2)); if(b[3]<64){ decoded[j++]=(byte)((b[2]<<6)|b[3]); } } } returndecoded; } //base64加密 privatestaticStringbase64Encode(byte[]in){ StringBuilderout=newStringBuilder((in.length*4)/3); intb; for(inti=0;i<in.length;i+=3){ b=(in[i]&0xFC)>>2; out.append(CODES.charAt(b)); b=(in[i]&0x03)<<4; if(i+1<in.length){ b|=(in[i+1]&0xF0)>>4; out.append(CODES.charAt(b)); b=(in[i+1]&0x0F)<<2; if(i+2<in.length){ b|=(in[i+2]&0xC0)>>6; out.append(CODES.charAt(b)); b=in[i+2]&0x3F; out.append(CODES.charAt(b)); }else{ out.append(CODES.charAt(b)); out.append(=); } }else{ out.append(CODES.charAt(b)); out.append("=="); } } returnout.toString(); } publicstaticvoidmain(String[]args){ Stringinput="Iloveyou,huhx!"; Stringencode=base64Encode(input.getBytes()); System.out.println("encode:"+encode); Stringdecode=newString(base64Decode(encode)); System.out.println("decode:"+decode); } }二、运行结果如下:
encode:SSBsb3ZlIHlvdSwgaHVoeCE= decode:Iloveyou,huhx!本文内容总结:Java自带的Base64算法,commons-codec的Base64算法,bcprov的Base64算法,自定义的base64算法,友情链接,
原文链接:https://www.cnblogs.com/huhx/p/javaBase64.html