首页 > 代码库 > 加解密算法之Base64
加解密算法之Base64
Base64不算严格的加密算法,因为加解密的算法都是公开的.
Base64的的三种提供者:
1.jdk (不推荐)
2.commonsCodes
3.bouncy castle
例:
import java.io.IOException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Demo
{
public static void main(String[] args) throws Exception
{
jdkBase64("测试 test");
commonsCodecBase64("测试 test");
bouncyCastleBase64("测试 test");
}
public static void jdkBase64(String src) throws IOException
{
BASE64Encoder encoder = new BASE64Encoder();
String encodeStr = encoder.encode(src.getBytes());
System.out.println("encode:" + encodeStr);
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodeBuffer = decoder.decodeBuffer(encodeStr);
String decodeStr = new String(decodeBuffer);
System.out.println("decode:" + decodeStr);
}
public static void commonsCodecBase64(String src)
{
byte[] encodeBase64 = org.apache.commons.codec.binary.Base64.encodeBase64(src.getBytes());
String encodeStr = new String(encodeBase64);
System.out.println("encode:" + encodeStr);
byte[] decodeBase64 = org.apache.commons.codec.binary.Base64.decodeBase64(encodeStr);
String decodeStr = new String(decodeBase64);
System.out.println("decode:" + decodeStr);
}
public static void bouncyCastleBase64(String src)
{
byte[] encodeBase64 = org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
String encodeStr = new String(encodeBase64);
System.out.println("encode:" + encodeStr);
byte[] decodeBase64 = org.bouncycastle.util.encoders.Base64.decode(encodeStr);
String decodeStr = new String(decodeBase64);
System.out.println("decode:" + decodeStr);
}
}
加解密算法之Base64