用java语言实现,文件加密、粉碎类;要求该程序具有图形用户界面,给出完整源码,并且提示部分和注释部分用中文提示,并给出详细的使用说明
时间: 2023-07-16 08:12:55 浏览: 96
基于JAVA的文件加密程序设计
以下是用Java语言实现文件加密、粉碎类的源码:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Random;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
public class FileEncryptAndShredder {
private static final int KEY_LENGTH = 256; // 加密密钥长度
private static final int BLOCK_SIZE = 16; // 加密块大小
private static final int ITERATIONS = 65536; // 迭代次数
private static final int SHREDDER_PASSES = 7; // 粉碎次数
/**
* 加密文件
*
* @param inputFile 要加密的文件
* @param outputFile 加密后的文件
* @param password 密码
* @throws Exception
*/
public static void encryptFile(File inputFile, File outputFile, char[] password) throws Exception {
// 生成加密密钥
byte[] salt = generateSalt();
SecretKey secretKey = generateSecretKey(password, salt);
// 生成随机IV
byte[] iv = generateIV();
// 加密文件
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv));
FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(salt);
outputStream.write(iv);
byte[] buffer = new byte[BLOCK_SIZE];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
byte[] encryptedBlock = cipher.update(buffer, 0, bytesRead);
if (encryptedBlock != null) {
outputStream.write(encryptedBlock);
}
}
byte[] finalBlock = cipher.doFinal();
if (finalBlock != null) {
outputStream.write(finalBlock);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}
/**
* 解密文件
*
* @param inputFile 要解密的文件
* @param outputFile 解密后的文件
* @param password 密码
* @throws Exception
*/
public static void decryptFile(File inputFile, File outputFile, char[] password) throws Exception {
// 读取加密密钥和IV
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] salt = new byte[KEY_LENGTH / 8];
inputStream.read(salt);
byte[] iv = new byte[BLOCK_SIZE];
inputStream.read(iv);
// 生成解密密钥
SecretKey secretKey = generateSecretKey(password, salt);
// 解密文件
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
FileOutputStream outputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[BLOCK_SIZE];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
byte[] decryptedBlock = cipher.update(buffer, 0, bytesRead);
if (decryptedBlock != null) {
outputStream.write(decryptedBlock);
}
}
byte[] finalBlock = cipher.doFinal();
if (finalBlock != null) {
outputStream.write(finalBlock);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}
/**
* 粉碎文件
*
* @param file 要粉碎的文件
*/
public static void shredFile(File file) {
Random random = new Random();
byte[] buffer = new byte[4096];
for (int i = 0; i < SHREDDER_PASSES; i++) {
try (FileOutputStream outputStream = new FileOutputStream(file, false)) {
while (outputStream.getChannel().position() < file.length()) {
random.nextBytes(buffer);
outputStream.write(buffer);
}
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
file.delete();
}
/**
* 生成加密盐
*
* @return 加密盐
*/
private static byte[] generateSalt() {
byte[] salt = new byte[KEY_LENGTH / 8];
new Random().nextBytes(salt);
return salt;
}
/**
* 生成加密密钥
*
* @param password 密码
* @param salt 加密盐
* @return 加密密钥
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
private static SecretKey generateSecretKey(char[] password, byte[] salt)
throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec keySpec = new PBEKeySpec(password, salt, ITERATIONS, KEY_LENGTH);
SecretKey secretKey = new SecretKeySpec(keyFactory.generateSecret(keySpec).getEncoded(), "AES");
return secretKey;
}
/**
* 生成随机IV
*
* @return 随机IV
*/
private static byte[] generateIV() {
byte[] iv = new byte[BLOCK_SIZE];
new Random().nextBytes(iv);
return iv;
}
public static void main(String[] args) {
// 创建文件选择器
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// 创建密码输入框
JPanel panel = new JPanel();
JPasswordField passwordField = new JPasswordField(20);
panel.add(passwordField);
// 显示加密/解密对话框
JFrame frame = new JFrame();
int result = JOptionPane.showConfirmDialog(frame, panel, "请输入密码", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result != JOptionPane.OK_OPTION) {
return;
}
char[] password = passwordField.getPassword();
// 选择要加密/解密的文件
result = fileChooser.showOpenDialog(frame);
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File inputFile = fileChooser.getSelectedFile();
// 选择加密后的输出文件
result = fileChooser.showSaveDialog(frame);
if (result != JFileChooser.APPROVE_OPTION) {
return;
}
File outputFile = fileChooser.getSelectedFile();
try {
if (inputFile.equals(outputFile)) {
throw new IllegalArgumentException("输入文件和输出文件不能相同");
}
if (Arrays.equals(password, new char[0])) {
throw new IllegalArgumentException("密码不能为空");
}
if (result == JFileChooser.APPROVE_OPTION) {
if (outputFile.exists()) {
result = JOptionPane.showConfirmDialog(frame, "输出文件已经存在,是否覆盖?", "警告",
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (result != JOptionPane.YES_OPTION) {
return;
}
}
if (outputFile.createNewFile()) {
encryptFile(inputFile, outputFile, password);
shredFile(inputFile);
} else {
throw new IOException("无法创建输出文件");
}
}
} catch (IOException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException
| NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException
| BadPaddingException e) {
JOptionPane.showMessageDialog(frame, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
该程序具有图形用户界面,使用说明如下:
1. 运行程序,会弹出一个对话框提示输入密码。
2. 输入密码后,会弹出文件选择器,选择要加密/解密的文件。
3. 再次弹出文件选择器,选择加密后的输出文件。
4. 程序会将输入文件加密后保存到输出文件,并将输入文件粉碎,以保证安全。
5. 如果输入文件和输出文件相同,或者密码为空,会弹出错误提示。
6. 如果输出文件已经存在,会弹出警告对话框询问是否覆盖。
以上是用Java语言实现文件加密、粉碎类的源码和使用说明。
阅读全文