用java语言实现,文件加密、粉碎类;要求该程序具有图形用户界面,给出完整源码,并且提示部分和注释部分用中文提示,并给出详细的使用说明

时间: 2023-07-16 07:12:55 浏览: 56
以下是用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语言实现文件加密、粉碎类的源码和使用说明。

相关推荐

最新推荐

recommend-type

使用java自带des加密算法实现文件加密和字符串加密

主要介绍了使用java自带des加密算法实现文件加密和字符串加密的示例,需要的朋友可以参考下
recommend-type

Java上传下载文件并实现加密解密

主要介绍了Java上传下载文件并实现加密解密,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

Java实现的3des加密解密工具类示例

主要介绍了Java实现的3des加密解密工具类,结合完整实例形式分析了3des加密解密的具体步骤与相关操作技巧,需要的朋友可以参考下
recommend-type

Java实现url加密处理的方法示例

主要介绍了Java实现url加密处理的方法,涉及java基于base64、编码转换实现加密解密相关操作技巧,需要的朋友可以参考下
recommend-type

基于java实现的ECC加密算法示例

主要介绍了基于java实现的ECC加密算法,简单说明了ECC算法的概念、原理,并结合实例形式分析了java实现ECC加密算法的定义与使用技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。