java 对视频和图片进行加密解密
import java.io.File;
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.io.RandomAccessFile;
public class FileEncryptAndDecrypt {
public static void main(String[] args) throws Exception {
String filename = "奔马.avi";
String flag = readFileLastByte("c:\\" + filename, 6);
if (flag.indexOf("hello") > 0) {
// 加密过了;
} else {
// 没有加密
new FileEncryptAndDecrypt().encrypt("c:\\" + filename, "hello");
}
System.out.println(new FileEncryptAndDecrypt().decrypt("c:\\"
+ filename, "c:\\" + filename, 6));
// System.out.println(readFileLastByte("c:\\3.jpg", 6));
}
/**
* 文件file进行加密
*
* @param fileUrl
* 文件路径
* @param key
* 密码
* @throws Exception
*/
public static void encrypt(String fileUrl, String key) throws Exception {
File file = new File(fileUrl);
String path = file.getPath();
if (!file.exists()) {
return;
}
int index = path.lastIndexOf("\\");
String destFile = path.substring(0, index) + "\\" + "abc";
System.out.println(destFile);
File dest = new File(destFile);
InputStream in = new FileInputStream(fileUrl);
OutputStream out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int r;
byte[] buffer2 = new byte[1024];
while ((r = in.read(buffer)) > 0) {
for (int i = 0; i < r; i++) {
byte b = buffer[i];
buffer2[i] = b == 255 ? 0 : ++b;
}
out.write(buffer2, 0, r);
out.flush();
}
in.close();
out.close();
file.delete();
dest.renameTo(new File(fileUrl));
appendMethodA(fileUrl, key);
System.out.println("加密成功");
}
/**
*
* @param fileName
* @param content
* 密钥
*/
public static void appendMethodA(String fileName, String content) {
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
// 将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 解密
*
* @param fileUrl
* 源文件
* @param tempUrl
* 临时文件
* @param ketLength
* 密码长度
* @return
* @throws Exception
*/
public static String decrypt(String fileUrl, String tempUrl, int keyLength)
throws Exception {
File file = new File(fileUrl);
if (!file.exists()) {
return null;
}
File dest = new File(tempUrl);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
InputStream is = new FileInputStream(fileUrl);
OutputStream out = new FileOutputStream(tempUrl);
byte[] buffer = new byte[1024];
byte[] buffer2 = new byte[1024];
byte bMax = (byte) 255;
long size = file.length() - keyLength;
int mod = (int) (size % 1024);
int div = (int) (size >> 10);
int count = mod == 0 ? div : (div + 1);
int k = 1, r;
while ((k <= count && (r = is.read(buffer)) > 0)) {
if (mod != 0 && k == count) {
r = mod;
}
for (int i = 0; i < r; i++) {
byte b = buffer[i];
buffer2[i] = b == 0 ? bMax : --b;
}
out.write(buffer2, 0, r);
k++;
}
out.close();
is.close();
return tempUrl;
}
/**
* 判断文件是否加密
*
* @param fileName
* @return
*/
public static String readFileLastByte(String fileName, int keyLength) {
File file = new File(fileName);
if (!file.exists())
return null;
StringBuffer str = new StringBuffer();
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFile.length();
// 将写文件指针移到文件尾。
for (int i = keyLength; i >= 1; i--) {
randomFile.seek(fileLength - i);
str.append((char) randomFile.read());
}
randomFile.close();
return str.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2012-04-10 ASP.NET会话(Session)模式