使用RC4加密方式对文件流进行加密解密
/**
* 文件加密
*/
public static File encryFile(String path1, String path2, String key) {
try {
Long start = System.currentTimeMillis();
//String path = request.getSession().getServletContext().getRealPath(File.separator+"upload");
File file_in = new File(path1);
File file_out = new File(path2);
FileInputStream in = new FileInputStream(file_in);
FileOutputStream out = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
while (in.read(bytes) != -1) {
byte[] bout = RC4Util.encry_RC4_byte(bytes, key);
System.out.println("bout="+bout);
out.write(bout,0, bout.length);
}
in.close();
out.close();
Long end = System.currentTimeMillis() - start;
System.out.println("diff="+end);
return file_out;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密文件
*/
public static File decryFile(String path1, String path2, String key) {
try {
Long start = System.currentTimeMillis();
//String path = request.getSession().getServletContext().getRealPath(File.separator+"upload");
File file_in = new File(path1);
File file_out = new File(path2);
FileInputStream in = new FileInputStream(file_in);
FileOutputStream out = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
while (in.read(bytes) != -1) {
byte[] bout = RC4Util.decry_RC4_byte(bytes, key);
System.out.println("bout="+bout);
out.write(bout,0, bout.length);
}
in.close();
out.close();
Long end = System.currentTimeMillis() - start;
System.out.println("diff="+end);
return file_out;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 读取http链接文件进行下载本地
*/
public static File readHttpFile() {
String videoUrl = "https://xxx/movie.mp4";
String fileName = "movie.mp4";
String path = "D:\\target\\ROOT\\WEB-INF\\classes\\upload";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
try {
URL url = new URL(videoUrl);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
byte[] getData = readInputStream(is);
//输出流
File file_out = new File(path + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file_out);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (is != null) {
is.close();
}
return file_out;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 从输入流中获取字节数组
* */
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] b = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(b)) != -1) {
bos.write(b, 0, len);
}
bos.close();
return bos.toByteArray();
}
/**
* RC4加解密工具类
*/
public class RC4Util {
/**
* 解密
* @param data
* @param key
* @return
*/
public static byte[] decry_RC4_byte(byte[] data, String key) {
if (null == data || null == key) {
return null;
}
return RC4Base(data, key);
}
public static String decry_RC4(byte[] data, String key) {
if (null == data || null == key) {
return null;
}
return asString(RC4Base(data, key));
}
public static String decry_RC4(String data, String key) {
if (null == data || null == key) {
return null;
}
return new String(RC4Base(HexString2Bytes(data), key));
}
/**
* 加密
* @param data
* @param key
* @return
*/
public static byte[] encry_RC4_byte(byte[] data, String key) {
if (null == data || null == key) {
return null;
}
return RC4Base(data, key);
}
public static byte[] encry_RC4_byte(String data, String key) {
if (null == data || null == key) {
return null;
}
byte b_data[] = data.getBytes();
return RC4Base(b_data, key);
}
public static String encry_RC4_string(String data, String key) {
if (null == data || null == key) {
return null;
}
byte b_data[] = data.getBytes();
return toHexString(asString(encry_RC4_byte(data, key)));
}
private static String asString(byte[] buf) {
StringBuffer stringBuffer = new StringBuffer(buf.length);
for (int i = 0; i < buf.length; i++) {
stringBuffer.append((char) buf[i]);
}
return stringBuffer.toString();
}
private static byte[] RC4Base(byte[] input, String mkKey) {
int x = 0;
int y = 0;
byte key[] = initKey(mkKey);
int xorIndex;
byte[] result = new byte[input.length];
for (int i = 0; i < input.length; i++) {
x = (x + 1) & 0xff;
y = ((key[x] & 0xff) + y) & 0xff;
byte tmp = key[x];
key[x] = key[y];
key[y] = tmp;
xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;
result[i] = (byte) (input[i] ^ key[xorIndex]);
}
return result;
}
private static byte[] initKey(String aKey) {
byte[] b_key = aKey.getBytes();
byte[] state = new byte[256];
for (int i = 0; i < 256; i++) {
state[i] = (byte) i;
}
int index1 = 0;
int index2 = 0;
if (null == b_key || b_key.length ==0) {
return null;
}
for (int i = 0; i < 256; i++) {
index2 = ((b_key[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
byte tmp = state[i];
state[i] = state[index2];
state[index2] = tmp;
index1 = (index1 + 1) % b_key.length;
}
return state;
}
private static byte[] HexString2Bytes(String src) {
int size = src.length();
byte[] ret = new byte[size / 2];
byte[] tmp = src.getBytes();
for (int i = 0; i < size / 2; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
private static String toHexString(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
String s4 = Integer.toHexString(ch & 0xFF);
if (s4.length() == 1) {
s4 = '0' + s4;
}
str = str + s4;
}
// 0x表示十六进制
return str;
}
private static byte uniteBytes(byte src0, byte src1) {
char _b0 = (char)Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();
_b0 = (char) (_b0 << 4);
char _b1 = (char)Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
public static void main(String[] arg) {
String userid = "B3BB90C9F89B46B49DDA9B132A95F5DE";
String akey = "fHVLJry92RR9B03n8KvOYRSmvb8PTsHm_"+userid;
String md5 = MD5Util.toMD5(akey);
System.out.println(md5);
String inputStr = "加密";
String key = md5;
String str = encry_RC4_string(inputStr, key);
System.out.println(str);
System.out.println(decry_RC4(str, key));
}
}