用Java语言,在一个文件选择器上可以自由选择RSA或者AES或者DES加解密,并将密钥和加解密文件保存在本地
时间: 2024-04-12 11:30:15 浏览: 148
FileEncryption.rar C#文件加密解密
5星 · 资源好评率100%
你可以使用Java语言来实现一个文件选择器,然后结合RSA、AES和DES算法来进行加解密操作,并将密钥和加解密文件保存在本地。下面是一个简单的示例代码:
```java
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class FileEncryptorApp extends JFrame {
private JButton encryptButton;
private JButton decryptButton;
private JTextArea logArea;
public FileEncryptorApp() {
setTitle("File Encryptor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
encryptButton = new JButton("Encrypt");
encryptButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(FileEncryptorApp.this);
if (result == JFileChooser.APPROVE_OPTION) {
File fileToEncrypt = fileChooser.getSelectedFile();
String algorithm = chooseAlgorithm();
String key = generateKey(algorithm);
try {
encryptFile(fileToEncrypt, algorithm, key);
log("File encrypted successfully.");
} catch (Exception ex) {
log("Failed to encrypt file: " + ex.getMessage());
}
}
}
});
decryptButton = new JButton("Decrypt");
decryptButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(FileEncryptorApp.this);
if (result == JFileChooser.APPROVE_OPTION) {
File fileToDecrypt = fileChooser.getSelectedFile();
String algorithm = chooseAlgorithm();
String key = readKeyFromFile();
try {
decryptFile(fileToDecrypt, algorithm, key);
log("File decrypted successfully.");
} catch (Exception ex) {
log("Failed to decrypt file: " + ex.getMessage());
}
}
}
});
logArea = new JTextArea();
logArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(logArea);
setLayout(new BorderLayout());
add(encryptButton, BorderLayout.NORTH);
add(decryptButton, BorderLayout.SOUTH);
add(scrollPane, BorderLayout.CENTER);
pack();
}
private String chooseAlgorithm() {
String[] options = {"RSA", "AES", "DES"};
return (String) JOptionPane.showInputDialog(null, "Choose encryption algorithm:", "Algorithm",
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
}
private String generateKey(String algorithm) throws NoSuchAlgorithmException {
KeyGenerator keyGenerator;
if (algorithm.equals("RSA")) {
keyGenerator = KeyGenerator.getInstance("RSA");
} else if (algorithm.equals("AES")) {
keyGenerator = KeyGenerator.getInstance("AES");
} else if (algorithm.equals("DES")) {
keyGenerator = KeyGenerator.getInstance("DES");
} else {
throw new IllegalArgumentException("Invalid algorithm: " + algorithm);
}
keyGenerator.init(128); // Change the key size as needed
SecretKey secretKey = keyGenerator.generateKey();
return Base64.getEncoder().encodeToString(secretKey.getEncoded());
}
private void encryptFile(File file, String algorithm, String key) throws Exception {
Cipher cipher = Cipher.getInstance(algorithm);
SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), algorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] fileBytes = readFileBytes(file);
byte[] encryptedBytes = cipher.doFinal(fileBytes);
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(encryptedBytes);
outputStream.close();
}
private void decryptFile(File file, String algorithm, String key) throws Exception {
Cipher cipher = Cipher.getInstance(algorithm);
SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), algorithm);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] fileBytes = readFileBytes(file);
byte[] decryptedBytes = cipher.doFinal(fileBytes);
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(decryptedBytes);
outputStream.close();
}
private byte[] readFileBytes(File file) throws IOException {
byte[] fileBytes = new byte[(int) file.length()];
FileInputStream inputStream = new FileInputStream(file);
inputStream.read(fileBytes);
inputStream.close();
return fileBytes;
}
private String readKeyFromFile() {
// Implement your own logic to read the key from a file
// and return it as a String
return null;
}
private void log(String message) {
logArea.append(message + "\n");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new FileEncryptorApp().setVisible(true);
}
});
}
}
```
在上述代码中,我们创建了一个简单的GUI应用程序,其中包含了加密和解密按钮,并使用JFileChooser来选择要加密或解密的文件。在加密和解密方法中,我们根据选定的算法和生成的密钥初始化Cipher对象,并使用密钥对文件进行加密或解密操作。你需要实现`readKeyFromFile()`方法来读取密钥文件的逻辑。
请注意,这只是一个简单的示例,你可能需要根据实际需求进行修改和完善。此外,为了实现RSA加密和解密,你还需要生成密钥对并将其保存在本地。我希望这个示例对你有所帮助!
阅读全文