Base64 四種方式的編碼和解碼

base64與字符串String之間的相互轉換

package com.wzq;import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.util.Base64;/*** @ClassName Base64Utils* @Description: TODO* @Author wzq* @CreateDate 2019/10/14* @UpdateDate 2019/10/16* @Version V1.0**/
public class Base64Utils {/*** 測試編碼  OK*/public static void main(String[] args) throws Throwable {System.out.println("*************************方式一*********************");String encode1 = Base64Encode("在線加密解密測試", "UTF-8");System.out.println("Encode1 " + encode1);String decode1 = Base64Decode(encode1, "UTF-8");System.out.println("decode1 " + decode1);System.out.println("*************************方式二*********************");String encode2 = encode2("在線加密解密測試", "UTF-8");System.out.println("encode2 "+encode2);String decode2 = decode2(encode2, "UTF-8");System.out.println("decode2 "+decode2);System.out.println("*************************方式三*********************");String encode3 = encode3("在線加密解密測試", "UTF-8");System.out.println("encode3 "+encode3);String decode3 = decode3(encode3, "UTF-8");System.out.println("decode3 "+decode3);System.out.println("*************************方式四*********************");String encode4 = encode4("在線加密解密測試", "UTF-8");System.out.println("encode4 "+encode4);String decode4 = decode3(encode4, "UTF-8");System.out.println("decode4 "+decode4);}/*****************************************方式一**********************************************//***************使用jdk自帶的DatatypeConverter.java類實現,但是jdk版本必須>=1.6。****************//*** base64編碼** @param result* @param encodeType* @return* @throws UnsupportedEncodingException*/public static String Base64Encode(String result, String encodeType) throws Throwable {try {byte[] data = result.getBytes(encodeType);return DatatypeConverter.printBase64Binary(data);} catch (Exception e) {throw new Throwable("base64編碼錯誤");}}/*** base64解碼** @param encodeType* @param result* @return*/public static String Base64Decode(String result, String encodeType) throws Throwable {try {byte[] data = DatatypeConverter.parseBase64Binary(result);return new String(data, encodeType);} catch (Exception e) {throw new Throwable("base64解碼");}}/*****************************************方式二**********************************************//*******************使用jdk自帶的Base64.java類實現,但是jdk版本必須>=1.8。***********************//*** base64編碼** @param str* @param encodeType* @return* @throws UnsupportedEncodingException*/public static String encode2(String str, String encodeType) throws UnsupportedEncodingException {byte[] data = str.getBytes(encodeType);return Base64.getEncoder().encodeToString(data);}/*** base64解碼** @param result* @param encodeType* @return*/public static String decode2(String result, String encodeType) throws UnsupportedEncodingException {byte[] data = result.getBytes(encodeType);byte[] decode = Base64.getDecoder().decode(data);return new String(decode,encodeType);}/*****************************************方式三**********************************************//** 使用cn.hutool.core.codec.Base64類實現* <dependency>*     <groupId>cn.hutool</groupId>*     <artifactId>hutool-all</artifactId>*     <version>4.5.0</version>* </dependency>*//*** base64編碼** @param str* @param encodeType* @return* @throws UnsupportedEncodingException*/public static String encode3(String str, String encodeType) throws UnsupportedEncodingException {return cn.hutool.core.codec.Base64.encode(str,encodeType);}/*** base64解碼** @param result* @param encodeType* @return*/public static String decode3(String result, String encodeType) throws UnsupportedEncodingException {return cn.hutool.core.codec.Base64.decodeStr(result,encodeType);}/*****************************************方式四**********************************************//** 使用org.apache.commons.codec.binary.Base64類實現* <dependency>*     <groupId>commons-codec</groupId>*     <artifactId>commons-codec</artifactId>*     <version>1.11</version>* </dependency>*//*** base64編碼** @param str* @param encodeType* @return* @throws UnsupportedEncodingException*/public static String encode4(String str, String encodeType) throws UnsupportedEncodingException {return org.apache.commons.codec.binary.Base64.encodeBase64String(str.getBytes(encodeType));}/*** base64解碼** @param result* @param encodeType* @return*/public static String decode4(String result, String encodeType) throws UnsupportedEncodingException {return new String(org.apache.commons.codec.binary.Base64.decodeBase64(result),encodeType);}}

輸出打印:

?

在線測試:成功

微信公眾號,感謝關注。持續更新...

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/273691.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/273691.shtml
英文地址,請注明出處:http://en.pswp.cn/news/273691.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

郵件服務器“單點登錄”功能

現狀分析&#xff1a; 相信我們每個人都有這樣的經歷&#xff0c;比方說銀行卡多的人要具體記憶每張卡的密碼挺麻煩&#xff1b;現代人興 趣廣泛&#xff0c;愛好多多&#xff0c;運動健身俱樂部、娛樂游戲休閑、購物理財等各種場合少不了獲得入門的“通行證”&#xff0c;要輸…

【kafka】 windows平臺搭建及使用

一、安裝 kafka 需要java環境&#xff1b;kafka 最新版本內置了 zookeeper&#xff0c;所以不需要安裝zookeeper&#xff1b;下載kafka最新版本&#xff0c;因為下載的是tgz文件&#xff0c;所以不需要安裝&#xff0c;解壓到相應的地方就可以了。下載地址&#xff1a;http://…

Unix/Linux環境C編程入門教程(16) LinuxMint CCPP開發環境搭建

1.Linux Mint由Linux Mint Team團隊于2006年開始發行&#xff0c;是一份基于Debian和Ubuntu的Linux發行版。其目標是提供一種更完整的即刻可用體驗&#xff0c;這包括提供瀏覽器插件、多媒體編解碼器、對DVD播放的支持、Java和其他組件&#xff0c;它也增加了一套定制桌面及各種…

@TableLogic注解表示邏輯刪除

TableLogic注解表示邏輯刪除 效果&#xff1a;在字段上加上這個注解再執行BaseMapper的刪除方法時&#xff0c;刪除方法會變成修改 場景&#xff1a; 1.實體類中屬性加上TableLogic, TableLogic private Integer dataStatus; 2.調用BaseMapper的deleteById(id)或…

圖片上傳至本地

1.配置文件 bootstrap.yml file:path:httpResURL: http://122.246.1.148:19086SAVE_PATH: /data/imagesresURI: /wzq/ 2.文件工具類 import cn.hutool.core.io.FileUtil; import com.vinsuan.park.platform.biz.FilePathConfig; import lombok.AllArgsConstructor; import lo…

html調用靜態json例子

html調用靜態json例子 1.json {"current": 2,"result": "success" } 1.html <!doctype html> <html> <head> <meta charset"utf-8"> <title></title> <script src"http://code.jquery.co…

Extjs發票管理系統

技術特點&#xff1a;Extjs框架&#xff0c;三層架構&#xff0c;Ajax,json 1.仿office2007菜單.介面美觀大方,可動態更改皮膚保存至cookie。 2,json數據源與實體類的相互轉換. 3.可下載桌面版登錄方式,統計在線人數. 4, chm操作手冊,方便操作人員熟悉. 功能描述&#xff1a; 1…

jQuery調用WebService返回JSON數據

相信大家都比較了解JSON格式的數據對于ajax的方便&#xff0c;不了解的可以從網上找一下這方面的資料來看一下&#xff0c;這里就不多說了&#xff0c;不清楚的可以在網上查一下&#xff0c;這里只說一下因為參數設置不當引起的取不到返回值的問題。 在用jQuery調用WebService的…

Docker---問題1:bash: vi: command not found/bash: vim: command not found

在使用docker時&#xff0c;有時候我們需要編輯配置文件&#xff0c;需要使用vim或者vi命令&#xff0c;發現&#xff1a; root64801a03b121:/etc/nginx/conf.d# vi default.conf bash: vi: command not found root64801a03b121:/etc/nginx/conf.d# vim default.conf bash: …

C# 獲取配置文件節點值

<?xml version"1.0" encoding"utf-8" ?><configuration> <appSettings> <add key"ManagerName" value"XXX" /> </appSettings> <connectionStrings> <add name"DataBase"…

WHENEVER SQLERROR EXIT SQL.SQLCODE

1.SQL>WHENEVER SQLERROR EXIT SQL.SQLCODE出現錯誤后終止SQL*Plus運行。2.SQL>WHENEVER SQLERROR SQL.SQLCODE EXIT ROLLBACK 除了之前的含義&#xff0c;還包括讓SQL*Plus在推出之前把所有還沒提交的變化回滾處理。轉載于:https://www.cnblogs.com/nuaa/p/3828473.html…

IDEA 快捷鍵終極大全

一、自動代碼 常用的有fori/sout/psvmTab即可生成循環、System.out、main方法等boilerplate樣板代碼 。 例如要輸入for(User user : users)只需輸入user.forTab &#xff1b; 再比如&#xff0c;要輸入Date birthday user.getBirthday()只需輸入user.getBirthday().varTab即…

數組中只出現一次的數字

題目&#xff1a;一個整型數組里&#xff0c;除了兩個數字以外&#xff0c;其他數字都出現了兩次&#xff0c;請寫程序找到這兩個只出現一次的數字。要求&#xff1a;時間復雜度為O(n),空間復雜度為O(1). 分析&#xff1a;看到這題&#xff0c;首先要明白&#xff0c;這是求兩個…

iOS工作筆記之NSClassFromString

id myObj [[NSClassFromString("MySpecialClass") alloc] init]; 和 id myObj [[MySpecialClass alloc] init]; 是一樣的。但是&#xff0c;如果你的程序中并不存在MySpecialClass這個類&#xff0c;下面的寫法會出錯&#xff0c;而上面的寫法只是返回一個空對象而…

Maven 使用bat批量清除本地倉庫的lastUpdated文件

echo off set REPOSITORY_PATHC:\Users\Administrator\.m2\repository rem 正在搜索... for /f "delims" %%i in (dir /b /s "%REPOSITORY_PATH%\*lastUpdated*") do ( del /s /q %%i ) rem 搜索完畢 pause 新建一個文件txt文件&#xff0c;把.txt后綴…

“ddl”有一個無效 SelectedValue,因為它不在項目列表中。

“ddl_ekt”有一個無效 SelectedValue,因為它不在項目列表中。 怎么回事 現象&#xff1a; 在用戶控件的page_load事件里綁定下拉框&#xff0c;報上面錯誤 解決&#xff1a; 將下拉框綁定&#xff0c;放在page_Init事件里 這可能跟服務器加載控件的生命過程有關系轉載于:https…

springbot 注入多實例

方式一&#xff1a; 在需要多實例的類上加入注解&#xff1a; Scope("prototype") 方式二&#xff1a; 在啟動類上加入&#xff1a; BeanScope(value ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode ScopedProxyMode.TARGET_CLASS)public PrototypeClass…

javascript日歷插件

javascript日歷插件 原文:javascript日歷插件javascript日歷插件 最近在嘗試著寫javascript日歷插件&#xff0c;所以也到github上看國外人日歷源碼&#xff0c;或者國內人寫的好點的&#xff0c;也在研究點&#xff0c;雖然看到網上有一大把的日歷控件&#xff0c;但是沒有幾個…

idea 重啟

1、通過File–>Invalidate Caches/Restar...進入重啟窗口 2、選擇自己所需要的重啟方式&#xff0c;四個按鈕&#xff0c;一共三種重啟方式&#xff1a; Invalidate and Restart 清空緩存并重啟。Invalidate 清除緩存&#xff0c;下次打開重啟。Cancel 取消。Just Restart …

Kernel Page Global Directory (PGD) of Page table of Process created in Linux Kernel

Kernel Page Global Directory (PGD) of User process created 在早期版本: 在fork一個進程的時候&#xff0c;必須建立進程自己的內核頁目錄項&#xff08;內核頁目錄項要與用戶空間的頁目錄放在同一個物理地址連續的頁面上&#xff0c;所以不能共享&#xff0c;但所有進程的內…