/** * 使用jdk实现base64加密 */ public static void jdkBase64() { try { BASE64Encoder encoder = new BASE64Encoder(); String encode = encoder.encode(src.getBytes()); System.out.println("encode:" + encode); BASE64Decoder decoder = new BASE64Decoder(); System.out.println("decode:" + new String(decoder.decodeBuffer(encode))); } catch (IOException e) { e.printStackTrace(); } }
在使用这种方式的情况,可能会出现找不到BASE64Encoder的情况,这样子解决:
右键项目-->属性-->Java build path-->Libraries-->jre System Library-->Access rules-->Edit-->Add-->Resolution选择Accessible,Rule Pattern填入**-->ok即可。
/** * 使用commonsCodes实现base64加密 */ public static void commonsCodesBase64() { byte[] encodeBytes = Base64.encodeBase64(src.getBytes()); System.out.println("encode:" + new String(encodeBytes)); byte[] decodeBytes = Base64.decodeBase64(encodeBytes); System.out.println("decode:" + new String(decodeBytes)); }
/** * 使用BouncyCastle实现base64加密 */ public static void bouncyCastleBase64() { byte[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(src .getBytes()); System.out.println("encode:" + new String(encodeBytes)); byte[] decodeBytes = org.bouncycastle.util.encoders.Base64 .decode(encodeBytes); System.out.println("decode:" + new String(decodeBytes)); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/scarthr/article/details/47090219