Java 用 Base64 做資料加解密

感謝網友的提醒: Base64 是一種編碼,

只不過我也把 Base64 視為一種簡單的加解密機制.

而在 Java 中要將資料做 Base64 編碼, 參考如下 :

 編碼程式碼
String vData = "Hello Java";
String v_encoding = (new sun.misc.BASE64Encoder()).encodeBuffer( vData.getBytes() );

 解碼程式碼
byte[] v_unencoding = (new sun.misc.BASE64Decoder()).decodeBuffer( v_encoding );
String vData = new String(v_unencoding);

 中文編碼程式碼
String vData = "Hello 歡迎光臨";
String v_encoding = (new sun.misc.BASE64Encoder()).encodeBuffer( vData.getBytes("ISO-8859-1") );

 中文解碼程式碼
byte[] v_unencoding = (new sun.misc.BASE64Decoder()).decodeBuffer( v_encoding );
String vData = new String(v_unencoding, "ISO-8859-1");
Related Posts Plugin for WordPress, Blogger...