现在的位置: 首页 > 综合 > 正文

给资源图片加密java

2018年04月06日 ⁄ 综合 ⁄ 共 1480字 ⁄ 字号 评论关闭

package com.cn.ant;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 图片加密 解密
 * 
 * @author L
 * 
 */
public class PicDecode {
private static final int KEY = 0x99;

public static void main(String arg[]) {
encrypt("Koala.jpg");
decrypt("ABCD.JDB");
}
public static String getCurTime(){
Date d=new Date(System.currentTimeMillis());
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss:SSS");
return df.format(d);
}
public static void encrypt(String filePath) {
System.out.println("*******************");
System.out.println("******正在加密*****"+getCurTime());
byte[] tempbytes = new byte[5000];
try {
InputStream fis = new FileInputStream(filePath);
OutputStream fos = new FileOutputStream("ABCD.JDB");
int read;
while ((read = fis.read()) > -1) {
fos.write(read ^ KEY);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("******加密结束*****"+getCurTime());
}

public static void decrypt(String filePath) {
System.out.println("*******************");
System.out.println("******正在解密*****"+getCurTime());
try {
InputStream is = new FileInputStream(filePath);
OutputStream out = new FileOutputStream("DCBA.jpg");
int read;
int i=0;
byte[] arr = new byte[is.available()];
while ((read = is.read()) > -1) {
read = read ^ KEY;
arr[i]=((byte) read);
i++;
}
out.write(arr,0,arr.length);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("******解密结束*****"+getCurTime());
}
}

抱歉!评论已关闭.