搜索
查看: 794|回复: 16
打印 上一主题 下一主题

[原创] 搞了半天做出来的……

[复制链接]
楼主
发表于 2014-9-6 10:56:32 | 显示全部楼层
破译成功
沙发
发表于 2014-9-6 10:56:35 | 显示全部楼层
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;

public class Encry implements ActionListener {
        static JFrame w = new JFrame();
        public static void main(String args[]) {
               
                JButton btnKey = new JButton("生成密钥");
                JButton btnEnc = new JButton("文件加密");
                JButton btnDec = new JButton("文件解密");
               
                Encry e = new Encry();
                btnKey.addActionListener(e);
                btnEnc.addActionListener(e);
                btnDec.addActionListener(e);
               
                w.setSize(150, 140);
                w.add(btnKey);
                w.add(btnEnc);
                w.add(btnDec);
                w.setLayout(new FlowLayout());
                w.setResizable(false);
                w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                w.setVisible(true);
        }
       
        @Override
        public void actionPerformed(ActionEvent arg0) {
                String s;
                if(arg0.getActionCommand() == "生成密钥") {
                        try {
                                File f = new File("C:/key.key");
                               
                                FileOutputStream fos = new FileOutputStream(f);
                               
                                for(int i = 0;i < 128; i++)
                                        fos.write((int)(Math.random()*128));
                               
                                fos.close();
                               
                                JOptionPane.showMessageDialog(null, "生成密钥成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                        }catch (Exception ee) {
                                JOptionPane.showMessageDialog(null, "生成密钥失败", "失败", JOptionPane.ERROR_MESSAGE);
                                ee.printStackTrace();
                        }
                       
                }
                if(arg0.getActionCommand() == "文件加密") {
                        w.setVisible(false);
                        s = JOptionPane.showInputDialog(null, "请输入文件路径(注意, 此处使用/代表\\):");
                        if(JOptionPane.showOptionDialog(null, "确定要加密码?加密后生成的文件将覆盖原来的文件!", "确定?", 2, 2, null, null, null) == 0) {
                                try {
                                        int key[] = new int[128];
                                        File keyFile = new File("C:/key.key");
                                        File inFile = new File(s);
                                        File outFile = new File(s + "test");
                                       
                                        FileInputStream keyFis = new FileInputStream(keyFile);
                                       
                                        for(int i = 0;i < 128;i ++)
                                                key[i] = keyFis.read();
                                       
                                        keyFis.close();
                                       
                                        FileInputStream fis = new FileInputStream(inFile);
                                        FileOutputStream fos = new FileOutputStream(outFile);
                                       
                                        int length = fis.available();
                                        for(int i = 0;i < length;i ++)
                                                fos.write(fis.read() + key[i%128]);
                                       
                                        fis.close();
                                        fos.close();
                                       
                                        FileInputStream fis2 = new FileInputStream(outFile);
                                        FileOutputStream fos2 = new FileOutputStream(inFile);
                                       
                                        int length2 = fis2.available();
                                        for(int i = 0;i < length2;i ++)
                                                fos2.write(fis2.read());
                                       
                                        fis2.close();
                                        fos2.close();
                                       
                                        outFile.delete();
                                        JOptionPane.showMessageDialog(null, "加密文件成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                                }catch (Exception ee) {
                                        JOptionPane.showMessageDialog(null, "加密文件失败", "失败", JOptionPane.ERROR_MESSAGE);
                                        ee.printStackTrace();
                                }
                                w.setVisible(true);
                        }
                }
                if(arg0.getActionCommand() == "文件解密") {
                        w.setVisible(false);
                        s = JOptionPane.showInputDialog(null, "请输入文件路径(注意, 此处使用/代表\\):");
                        if(JOptionPane.showOptionDialog(null, "确定要解密码?解密后生成的文件将覆盖原来的文件!", "确定?", 2, 2, null, null, null) == 0) {
                                try {
                                        int key[] = new int[128];
                                        File keyFile = new File("C:/key.key");
                                        File inFile = new File(s);
                                        File outFile = new File(s + "test");
                                       
                                        FileInputStream keyFis = new FileInputStream(keyFile);
                                       
                                        for(int i = 0;i < 128;i ++)
                                                key[i] = keyFis.read();
                                       
                                        keyFis.close();
                                       
                                        FileInputStream fis = new FileInputStream(inFile);
                                        FileOutputStream fos = new FileOutputStream(outFile);
                                       
                                        int length = fis.available();
                                        for(int i = 0;i < length;i ++)
                                                fos.write(fis.read() - key[i%128]);
                                       
                                        fis.close();
                                        fos.close();
                                       
                                        FileInputStream fis2 = new FileInputStream(outFile);
                                        FileOutputStream fos2 = new FileOutputStream(inFile);
                                       
                                        int length2 = fis2.available();
                                        for(int i = 0;i < length2;i ++)
                                                fos2.write(fis2.read());
                                       
                                        fis2.close();
                                        fos2.close();
                                       
                                        outFile.delete();
                                        JOptionPane.showMessageDialog(null, "解密文件成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                                }catch (Exception ee) {
                                        JOptionPane.showMessageDialog(null, "解密文件失败", "失败", JOptionPane.ERROR_MESSAGE);
                                        ee.printStackTrace();
                                }
                                w.setVisible(true);
                        }
                }
        }
}
板凳
发表于 2014-9-6 10:57:10 | 显示全部楼层
放c盘里不怕系统运行缓慢
地板
发表于 2014-9-20 11:18:04 | 显示全部楼层
cad20020601 发表于 2014-9-20 08:59
谣言止于智者,聊天止于呵呵

我一直被Copy,但从未被surpass
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表