sun.misc.BASE64Encoder详解
生活随笔
收集整理的這篇文章主要介紹了
sun.misc.BASE64Encoder详解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#(一)、BASE64編碼規(guī)則及JAVA中的使用
##1、編碼規(guī)則:
Base64編碼要求把3個(gè)8位字節(jié)(38=24)轉(zhuǎn)化為4個(gè)6位的字節(jié)(46=24),之后在6位的前面補(bǔ)兩個(gè)0,形成8位一個(gè)字節(jié)的形式。
例如字符串“張3” :
11010101 11000101 00110011
用十進(jìn)制表示即為:53 34 20 51
這個(gè)并不是最終的結(jié)果,還需要根據(jù)Base64的編碼表查詢出轉(zhuǎn)換后值。
下面就是BASE64編碼表:
##2、編碼和解碼
在JAVA中要實(shí)現(xiàn)Base64的編碼和解碼是非常容易的,因?yàn)镴DK中已經(jīng)有提供有現(xiàn)成的類:
編碼:
解碼:
sun.misc.BASE64Decoder dec = newsun.misc.BASE64Decoder(); byte[] data = dec.decodeBuffer(decodeStr);更多知識(shí)點(diǎn)參考請(qǐng)點(diǎn)擊這里
filename = "=?UTF-8?B?"+new BASE64Encoder().encoder(filename.getBytes("utf-8"))+"?=";###BASE64Encoder 加密和解密
public class B64Demo {public static String getBASE64(String s) { if (s == null) return null; return (new sun.misc.BASE64Encoder()).encode(s.getBytes()); } // 將 BASE64 編碼的字符串 s 進(jìn)行解碼public static String getFromBASE64(String s) { if (s == null) return null; sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } } // 將 BASE64 編碼的字符串 s 進(jìn)行加密,即對(duì)字符串進(jìn)行三次的BASE64編碼public static String encryption(Object obj){return B64Demo.getBASE64(B64Demo.getBASE64(B64Demo.getBASE64((String)obj)));}// 將 BASE64 編碼的字符串 s 進(jìn)行解密,即對(duì)字符串進(jìn)行三次的BASE64解碼public static String decryption(String 3b64){return B64Demo.getFromBASE64(B64Demo.getFromBASE64(B64Demo.getFromBASE64(3b64)));}public static void main(String[] args) {String a = encryption("100000.89".toString());System.out.println(a);//加密System.out.println(decryption(a));//解密} }總結(jié)
以上是生活随笔為你收集整理的sun.misc.BASE64Encoder详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STM32两轮自平衡小车(学习记录)——
- 下一篇: 吃透一切整流滤波电路(转)