Java加密系列之(二)Base64加密

來源:互聯網
上載者:User

標籤:base64   java   加密   commonscodec   bouncycastle   

sun.misc.BASE64Encoder/BASE64Decoder類不屬於JDK標準庫範疇,但在JDK中包含了該類,可以直接使用,但這個沒有java doc,屬於jdk裡不推薦使用的工具類。強烈建議不要用sun.misc,是不安全的。

bouncycastle的jar包:http://www.bouncycastle.org/latest_releases.html


package com.tvm.mrz.security.base64;import java.io.IOException;import org.apache.commons.codec.binary.Base64;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class Base64Test {private static String src = "Mr.Z Security Base64";public static void jdkBase64() {BASE64Encoder encoder = new BASE64Encoder();String encode = encoder.encode( src.getBytes() );System.out.println( "encode:" + encode );BASE64Decoder decoder = new BASE64Decoder();try {byte[] decode = decoder.decodeBuffer( encode );System.out.println( "decode:" + new String( decode ) );} catch( IOException e ) {e.printStackTrace();}}public static void commonsCodecBase64() {// String encode = Base64.encodeBase64String( src.getBytes() );byte[] encode = Base64.encodeBase64( src.getBytes() );System.out.println( "encode:" + new String( encode ) );byte[] decode = Base64.decodeBase64( encode );System.out.println( "decode:" + new String( decode ) );}public static void bouncyCastleBase64() {byte[] encode = org.bouncycastle.util.encoders.Base64.encode( src.getBytes() );System.out.println( "encode:" + new String( encode ) );byte[] decode = org.bouncycastle.util.encoders.Base64.decode( encode );System.out.println( "decode:" + new String( decode ) );}public static void main( String[] args ) {jdkBase64();commonsCodecBase64();bouncyCastleBase64();}}


Java加密系列之(二)Base64加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.