📄 fileencrypt.java
字号:
/*
* FileEncrypt.java
*
* Created on 2007年12月20日, 上午9:17
*/
package fileencrpt;
import javax.swing.*;
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
*
* @author Administrator
*/
public class FileEncrypt extends javax.swing.JFrame {
/** Creates new form FileEncrypt */
public FileEncrypt() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jPasswordField1 = new javax.swing.JPasswordField();
jPasswordField2 = new javax.swing.JPasswordField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("\u738b\u6c0f\u52a0\u5bc6\u89e3\u5bc6");
setBounds(new java.awt.Rectangle(300, 100, 0, 0));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setIconImage(getIconImage());
setResizable(false);
jLabel1.setText("\u6e90\u6587\u4ef6\uff1a");
jLabel1.getAccessibleContext().setAccessibleName("");
jButton1.setText("\u6d4f\u89c8");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton3.setText("\u52a0\u5bc6");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("\u89e3\u5bc6");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jLabel3.setText("\u5bc6\u7801\uff1a");
jLabel4.setText("\u786e\u8ba4\uff1a");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 127, Short.MAX_VALUE)
.addComponent(jButton4))
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1))
.addComponent(jPasswordField2, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton4))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO 将在此处添加您的处理代码:
String password = new String(jPasswordField1.getPassword());
String confirm = new String(jPasswordField2.getPassword());
if(!password.equals(confirm))
{
JOptionPane.showMessageDialog(this, "两次密码不一致");
return;
}
if(password.length() > 16)
{
JOptionPane.showMessageDialog(this, "密码不能多于16个字符");
return;
}
if(!new File(jTextField2.getText()).exists())
{
JOptionPane.showMessageDialog(this, "源文件不存在");
return;
}
if(!jTextField2.getText().endsWith(".wa"))
{
JOptionPane.showMessageDialog(this, "请确定你选择的是密文文件");
return;
}
try
{
byte[] bs = Md5(password);
Key key = new SecretKeySpec(bs, "AES");
Cipher cp = Cipher.getInstance("AES");
cp.init(Cipher.DECRYPT_MODE, key);
FileOutputStream out = new FileOutputStream(jTextField2.getText().replaceAll(".wa", ""));
CipherInputStream in = new CipherInputStream(new FileInputStream(jTextField2.getText()), cp);
int i;
while((i=in.read())!=-1)
{
out.write(i);
}
out.flush();
JOptionPane.showMessageDialog(this, "解密成功");
in.close();
out.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}//GEN-LAST:event_jButton4ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO 将在此处添加您的处理代码:
String password = new String(jPasswordField1.getPassword());
String confirm = new String(jPasswordField2.getPassword());
if(!password.equals(confirm))
{
JOptionPane.showMessageDialog(this, "两次密码不一致");
return;
}
if(password.length() > 16)
{
JOptionPane.showMessageDialog(this, "密码不能多于16个字符");
return;
}
if(!new File(jTextField2.getText()).exists())
{
JOptionPane.showMessageDialog(this, "源文件不存在");
return;
}
try
{
byte[] bs = Md5(password);
Key key = new SecretKeySpec(bs, "AES");
Cipher cp = Cipher.getInstance("AES");
cp.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream out = new CipherOutputStream(new FileOutputStream(jTextField2.getText()+".wa"), cp);
FileInputStream in = new FileInputStream(jTextField2.getText());
int i;
while((i=in.read())!=-1)
{
out.write(i);
}
out.flush();
in.close();
out.close();
JOptionPane.showMessageDialog(this, "加密成功");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO 将在此处添加您的处理代码:
JFileChooser jfc = new JFileChooser();
if(jfc.showOpenDialog(this) == 0)
jTextField2.setText(jfc.getSelectedFile().getAbsolutePath());
}//GEN-LAST:event_jButton1ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FileEncrypt().setVisible(true);
}
});
}
private static byte[] Md5(String plainText ) {
byte[] b;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
b = md.digest();
return b;
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JPasswordField jPasswordField2;
private javax.swing.JTextField jTextField2;
// 变量声明结束//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -