欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用Base64實現(xiàn)文件加密解密

 更新時間:2019年03月28日 11:46:28   作者:簡簡單2783  
這篇文章主要為大家詳細介紹了java實現(xiàn)Base64給文件加密、解密,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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;
/**
* 服務器之間傳遞圖片
*/
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論