解决批量解密DES文件(以图片为例子)
来源:互联网 发布:h5制作软件下载 编辑:程序博客网 时间:2024/06/15 06:17
1、我之前遇到一个问题,就是客户要求要解密存放在服务器上的多大10G的DES加密后的图片文件,而且这些图片分布在不同的文件下,而且我去上网找DES解密的小工具,但是都是只能对单一文件,所以我写一小段java程序去做,效果还行。
2、大概思路:获取所有要解密的文件名,之后对文件解密,然后删除掉源文件,之后把解密好的文件重命名为原文件名。
3、只要稍微修改也可以批量DES加密文件。
代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.SecretKeySpec;
public class FileDES {
private Key mKey;
private Cipher mDecryptCipher;
private Cipher mEncryptCipher;
private static String key = “javadevelop”;
public FileDES(String key) throws Exception {
initKey(key);
initCipher();
}
private void initKey(String key) { byte[] keyByte = key.getBytes(); byte[] byteTemp = new byte[8]; for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) { byteTemp[i] = keyByte[i]; } mKey = new SecretKeySpec(byteTemp, "DES");}private void initCipher() throws Exception { mEncryptCipher = Cipher.getInstance("DES"); mEncryptCipher.init(Cipher.ENCRYPT_MODE, mKey); mDecryptCipher = Cipher.getInstance("DES"); mDecryptCipher.init(Cipher.DECRYPT_MODE, mKey);}
//加密文件方法实现
public void doEncryptFile(InputStream in, String savePath) {
if (in == null) {
System.out.println(“inputstream is null”);
return;
}
try {
CipherInputStream cin = new CipherInputStream(in, mEncryptCipher);
OutputStream os = new FileOutputStream(savePath);
byte[] bytes = new byte[1024];
int len = -1;
while ((len = cin.read(bytes)) > 0) {
os.write(bytes, 0, len);
os.flush();
}
os.close();
cin.close();
in.close();
System.out.println(“加密成功”);
} catch (Exception e) {
System.out.println(“加密失败”);
e.printStackTrace();
}
}
/** * 加密文件 * * @param filePath * 需要加密的文件路径 * @param savePath * 加密后保存的位置 * @throws FileNotFoundException */public void doEncryptFile(String filePath, String savePath) throws FileNotFoundException { doEncryptFile(new FileInputStream(filePath), savePath);}/** * 解密文件 * * @param filePath * 需要解密的文件路径 * @param savePath * 解密后保存的位置 * @throws FileNotFoundException */public void doDecryptFile(String filePath, String savePath) throws FileNotFoundException { doDecryptFile(new FileInputStream(filePath), savePath);}/** * 解密文件 * * @param in */public void doDecryptFile(InputStream in, String savePath) { if (in == null) { System.out.println("inputstream is null"); return; } try { CipherInputStream cin = new CipherInputStream(in, mDecryptCipher); OutputStream os = new FileOutputStream(savePath); byte[] bytes = new byte[1024]; int len = -1; while ((len = cin.read(bytes)) > 0) { os.write(bytes, 0, len); os.flush(); } os.close(); cin.close(); in.close(); System.out.println("解密成功"); } catch (Exception e) { System.out.println("解密失败"); e.printStackTrace(); }}/** * 解密文件 * * @param in */public void doDecryptFile(InputStream in) { if (in == null) { System.out.println("inputstream is null"); return; } try { CipherInputStream cin = new CipherInputStream(in, mDecryptCipher); BufferedReader reader = new BufferedReader(new InputStreamReader( cin)); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); cin.close(); in.close(); System.out.println("解密成功"); } catch (Exception e) { System.out.println("解密失败"); e.printStackTrace(); }}
//获取文件夹下的所有文件名
private static void getFile(String path, String key) throws Exception {
File file = new File(path);
File[] array = file.listFiles();
for (int i = 0; i < array.length; i++) {
if (array[i].isFile()) {
handleFile(array[i], key);
} else if (array[i].isDirectory()) {
File[] fileList = array[i].listFiles();
for (int j = 0; j < fileList.length; j++) {
File readfile = fileList[j];
if (!readfile.isDirectory()) {
handleFile(readfile, key);
} else if (readfile.isDirectory()) {
getFile(readfile.getPath(), key);
}
} } }}
//解密文件的方法集合
public static void handleFile(File file, String key) throws Exception {
FileDES fileDES = new FileDES(key);
String fileName = file.getName();
String newFileName = “gwsheng_” + fileName;
String filePath = file.getPath();
String newFilePath = null;
newFilePath = filePath.substring(0, filePath.lastIndexOf(“\”));
fileDES.doDecryptFile(filePath, newFilePath + “\” + newFileName);
deleteFile(filePath);
renameFile(newFilePath + “\” + newFileName, filePath);
}
//重命名文件名方法
private static void renameFile(String oldFileName, String newFileName) {
File oldFile = new File(oldFileName);
File newFile = new File(newFileName);
if (!oldFile.exists()) {
return;// 重命名文件不存在
}
if (newFile.exists())
System.out.println(“已经存在!”);
else {
oldFile.renameTo(newFile);
}
}
//删除原文件
private static void deleteFile(String path) {
// TODO Auto-generated method stub
File file = new File(path);
if (file.isFile() && file.exists()) {
file.delete();
}
}
/** * 解密文件 * * @param filePath * 文件路径 * @throws Exception */public void doDecryptFile(String filePath) throws Exception { doDecryptFile(new FileInputStream(filePath));}
//主方法
public static void main(String[] args) throws Exception {
getFile(“C:\Users\yt\Desktop\picture_for_prd”,key);
}
}
- 解决批量解密DES文件(以图片为例子)
- Des加密解密例子
- DES加解密例子
- 对称加密的例子(DES)——针对文件进行加解密
- 对文件压缩加密/解密解压缩的例子,DES/RSA
- 文件DES加密解密
- des文件加密、解密
- DES加密解密文件
- DES加解密文件
- DES文件加解密
- DES加密解密算法文件
- java DES加密解密文件
- java DES加密解密文件
- Java DES文件加密解密
- 遍历文件夹中的文件(以图片为例)
- 下载,更新及删除文件(以图片为例)
- ajax上传文件(以图片为例) SpringMVC
- DES加密解密的详细例子
- USACO 2.4 D Bessie Come Home 题解
- LibreOJ504
- 对话框报错 You need to use a Theme.AppCompat theme (or descendant) with this activity.
- 安卓复杂滑动案例 自定义behavior源码分析 实现头布局图片的缩放透明度变化,RecycleView的滑动布局,坐标变化
- nginx、php-fpm等添加自启动
- 解决批量解密DES文件(以图片为例子)
- RequireJS
- 北大大数据机试题
- 有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 <configuration>\<system.web>\<
- 25 个你需要知道的人工智能术语
- 接口自动化-步骤、遇到的问题及解决方法(jmeter、ant、Jenkins)
- 网站网址图标制作
- for in 循环的输出顺序问题
- 鸣人的影分身