好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

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加密解密。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

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;

   }

 

}

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

原文链接:https://blog.csdn.net/u012488189/article/details/17709797

查看更多关于java使用Base64实现文件加密解密的详细内容...

  阅读:15次