当前位置: 移动技术网 > IT编程>开发语言>Java > java使用Base64实现文件加密解密

java使用Base64实现文件加密解密

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了java实现base64给文件加密、解密的具体代码,供大家参考,具体内容如下

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) {
// 将图片文件转化为字节数组字符串,并对其进行base64编码处理
string strimg = getimagestr();
system.out.println(strimg);
// 对字节数组字符串进行base64解码并生成图片
generateimage(strimg);
}
public static string getimagestr() {
string imgfile = "d:\\java.pdf";// 待处理的图片
inputstream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new fileinputstream(imgfile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (ioexception e) {
e.printstacktrace();
}
// 对字节数组base64编码
base64encoder encoder = new base64encoder();
return encoder.encode(data);// 返回base64编码过的字节数组字符串
}
public static boolean generateimage(string imgstr) {
if (imgstr == null){ // 图像数据为空
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) {// 调整异常数据
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代码实现对文件的base64加密解密

base64编码方法:将每三个8bit的字节转换为四个6bit的字节,其中,转换之后的这四个字节中每6个有效bbit为有效数据,空余的那2个用0补上成为一个字节,java中可直接调用算法进行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);
  }

  //文件转字符串
  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;
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网