java uses Base64 to realize file encryption and decryption
- 2021-07-10 19:52:58
- OfStack
This article example for everyone to share Java implementation of Base64 to file encryption, decryption of the specific code, for your reference, the specific content is as follows
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;
/**
* Pass pictures between servers
*/
public class TestCase {
public static void main(String[] args) {
// Converts a picture file into a byte array string, and performs the Base64 Coding processing
String strImg = GetImageStr();
System.out.println(strImg);
// On a byte array string Base64 Decode and generate pictures
GenerateImage(strImg);
}
public static String GetImageStr() {
String imgFile = "d:\\java.pdf";// Pictures to be processed
InputStream in = null;
byte[] data = null;
// Read the picture byte array
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// Byte array Base64 Code
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// Return Base64 Encoded byte array string
}
public static boolean GenerateImage(String imgStr) {
if (imgStr == null){ // Image data is empty
return false;
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64 Decoding
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// Adjust exception data
b[i] += 256;
}
}
// Generate jpg Picture
String imgFilePath = "d:\\new.pdf"; // Newly generated picture
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}
This site also shares 1 segment of java code to realize base64 encryption and decryption of files
Base64 encoding method: Every 3 bytes of 8bit are converted into 4 bytes of 6bit, in which every 6 valid Bbit of the 4 bytes after conversion are valid data, and the remaining 2 bytes are supplemented with 0 to become 1 byte. java can directly call the algorithm for base64 encryption and decryption.
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);
}
// File to string
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;
}
}
// Encryption
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;
}
// Decryption
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;
}
}