java使用Base64實現(xiàn)文件加密解密
本文實例為大家分享了Java實現(xiàn)Base64給文件加密、解密的具體代碼,供大家參考,具體內(nèi)容如下
package test.base64; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * 服務(wù)器之間傳遞圖片 */ public class TestCase { public static void main(String[] args) { // 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進行Base64編碼處理 String strImg = GetImageStr(); System.out.println(strImg); // 對字節(jié)數(shù)組字符串進行Base64解碼并生成圖片 GenerateImage(strImg); } public static String GetImageStr() { String imgFile = "d:\\java.pdf";// 待處理的圖片 InputStream in = null; byte[] data = null; // 讀取圖片字節(jié)數(shù)組 try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 對字節(jié)數(shù)組Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// 返回Base64編碼過的字節(jié)數(shù)組字符串 } public static boolean GenerateImage(String imgStr) { if (imgStr == null){ // 圖像數(shù)據(jù)為空 return false; } BASE64Decoder decoder = new BASE64Decoder(); try { // Base64解碼 byte[] b = decoder.decodeBuffer(imgStr); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) {// 調(diào)整異常數(shù)據(jù) b[i] += 256; } } // 生成jpg圖片 String imgFilePath = "d:\\new.pdf"; // 新生成的圖片 OutputStream out = new FileOutputStream(imgFilePath); out.write(b); out.flush(); out.close(); return true; } catch (Exception e) { return false; } } }
小編另分享一段java代碼實現(xiàn)對文件的base64加密解密
Base64編碼方法:將每三個8bit的字節(jié)轉(zhuǎn)換為四個6bit的字節(jié),其中,轉(zhuǎn)換之后的這四個字節(jié)中每6個有效Bbit為有效數(shù)據(jù),空余的那2個用0補上成為一個字節(jié),java中可直接調(diào)用算法進行base64加密解密。
public class base64 { public static void main(String[] args){ File file = new File("D:\\base64.txt"); String result = getFromBase64(file2String(file)); System.out.println(result); } //文件轉(zhuǎn)字符串 public static String file2String(File file){ try { BufferedReader buffer = new BufferedReader(new FileReader(file)); StringBuilder sb = new StringBuilder(); String temp; while((temp = buffer.readLine()) !=null ){ sb.append(temp); } return sb.toString(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } //加密 public static String getBase64(String str){ byte[] b = null; String s = null; try { b = str.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if( b != null){ s = new BASE64Encoder().encode(b); } return s; } //解密 public static String getFromBase64(String str){ byte[] b = null; String result = null; if(str != null){ BASE64Decoder decoder = new BASE64Decoder(); try { b = decoder.decodeBuffer(str); result = new String(b, "utf-8"); } catch (Exception e) { e.printStackTrace(); } } return result; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實現(xiàn)md5和base64加密解密的示例代碼
這篇文章主要介紹了Java實現(xiàn)md5和base64加密解密的示例代碼,幫助大家更好的利用Java加密解密文件,感興趣的朋友可以了解下2020-09-09關(guān)于阿里巴巴TransmittableThreadLocal使用解讀
文章主要介紹了三種ThreadLocal的使用:ThreadLocal、InheritableThreadLocal和TransmittableThreadLocal,ThreadLocal和InheritableThreadLocal在單線程和部分情況下可以正常工作,但TransmittableThreadLocal在處理線程池時表現(xiàn)更佳2025-02-02SpringBoot對接Twilio實現(xiàn)發(fā)送驗證碼和驗證短信碼
Twilio是一家提供云通信服務(wù)的公司,旨在幫助開發(fā)者和企業(yè)通過簡單的API實現(xiàn)各種通信功能,下面我們來看看如何對接Twilio實現(xiàn)發(fā)送驗證碼和驗證短信碼吧2025-03-03Spring cloud gateway設(shè)置context-path服務(wù)路由404排查過程
這篇文章主要介紹了Spring cloud gateway設(shè)置context-path服務(wù)路由404排查過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08