加解密算法之Base64
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加解密算法之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的主要内容,如果未能解决你的问题,请参考以下文章