XOR Cipher加密与我的GUI搞混了?

时间:2015-03-10 04:28:42

标签: java encryption

我已经设置了一个简单的GUI系统来使用这个XOR加密系统。当我通过GUI解密时,解密出错是错误的,但是当我对字符串进行硬编码以解密时,它就出来了。例如:

例如,这会打印" this_is_to_test_if_the_program_in_the_gui_is_working_which_it_wont":

System.out.println(Skeleton.xorDecrypt("BX_Dg_CgLVkCVKLfQUfC^SgHFWSFY^oP]iBPShSC]g^KgO[D\\Q]Qg@PQTPk^MiA_VD", "638460047789894"));

但是,如果我通过GUI进行操作,它就会显示为" this_is_to_test_if_the_program_in_the_gui_is_workdiia_xdglf \ g {Qykns"

Skeleton.java

public class Skeleton {

    public static String xorEncrypt(String data, String key) {

        data = data.replaceAll(" ", "_");

        byte m_cData[] = data.getBytes();
        byte m_cKey[] = key.getBytes();

        int keyPointer = 0;
        for (int i = 0; i < m_cData.length; i++) {
            m_cData[i] ^= m_cKey[keyPointer];
            keyPointer += m_cData[i];
            keyPointer %= m_cKey.length;
        }

        return new String(m_cData).replaceAll("\\\\", "\\\\\\\\");
    }

    public static String xorDecrypt(String data, String key) {
        byte m_cData[] = data.getBytes();
        byte m_cKey[] = key.getBytes();

        int keyPointer = 0;
        byte keyPointerAdd = 0;
        for (int i = 0; i < m_cData.length; i++) {
            keyPointerAdd = m_cData[i];
            m_cData[i] ^= m_cKey[keyPointer];
            keyPointer += keyPointerAdd;
            keyPointer %= m_cKey.length;
        }

        return new String(m_cData);
    }

    public static long generateRandom(int length) {
        Random random = new Random();
        char[] digits = new char[length];
        digits[0] = (char) (random.nextInt(9) + '1');
        for (int i = 1; i < length; i++) {
            digits[i] = (char) (random.nextInt(10) + '0');
        }
        return Long.parseLong(new String(digits));
    }

}

Window.java

public class Window extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textPasscode;

    public Window() {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException exception) {
            exception.printStackTrace();
        }

        setTitle("Saypar");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 381);
        setLocationRelativeTo(null);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textPasscode = new JTextField();
        textPasscode.setBounds(94, 278, 241, 20);
        contentPane.add(textPasscode);
        textPasscode.setColumns(10);

        JLabel lblPasscode = new JLabel("Passcode (#):");
        lblPasscode.setBounds(10, 281, 74, 14);
        contentPane.add(lblPasscode);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 11, 424, 256);
        contentPane.add(scrollPane);

        JTextArea textAreaMessage = new JTextArea();
        scrollPane.setViewportView(textAreaMessage);
        textAreaMessage.setLineWrap(true);
        textAreaMessage.setWrapStyleWord(true);

        JButton btnEncrypt = new JButton("Encrypt");
        btnEncrypt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                if (textAreaMessage.getText().trim().length() == 0) {
                    JOptionPane.showMessageDialog(null, "You need to enter in a message before encrypting.", "Saypar Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (textPasscode.getText().trim().length() == 0) {
                    JOptionPane.showMessageDialog(null, "You need to enter in a passcode before encrypting.", "Saypar Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                StringSelection stringSelection = new StringSelection(Skeleton.xorEncrypt(textAreaMessage.getText(), textPasscode.getText()));
                Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
                clpbrd.setContents(stringSelection, null);

                JOptionPane.showMessageDialog(null, "Message Encrypted... Copied to clipboard.", "Encrypted Message", JOptionPane.PLAIN_MESSAGE);
            }
        });
        btnEncrypt.setBounds(345, 277, 89, 23);
        contentPane.add(btnEncrypt);

        JButton btnDecrypt = new JButton("Decrypt");
        btnDecrypt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                if (textAreaMessage.getText().trim().length() == 0) {
                    JOptionPane.showMessageDialog(null, "You need to enter in a message before decrypting.", "Saypar Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (textPasscode.getText().trim().length() == 0) {
                    JOptionPane.showMessageDialog(null, "You need to enter in a passcode before decrypting.", "Saypar Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                StringSelection stringSelection = new StringSelection(Skeleton.xorDecrypt(textAreaMessage.getText(), textPasscode.getText()));
                Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
                clpbrd.setContents(stringSelection, null);


                JOptionPane.showMessageDialog(null, "Message Decrypted... Copied to clipboard.", "Decrypted Message", JOptionPane.PLAIN_MESSAGE);
            }
        });

        btnDecrypt.setBounds(345, 311, 89, 23);
        contentPane.add(btnDecrypt);

        JButton btnGeneratePasscode = new JButton("Generate Random Passcode");
        btnGeneratePasscode.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                textPasscode.setText("" + Skeleton.generateRandom(15));
            }
        });

        btnGeneratePasscode.setBounds(107, 311, 228, 23);
        contentPane.add(btnGeneratePasscode);

        JLabel lblRecommended = new JLabel("RECOMMENDED:");
        lblRecommended.setBounds(10, 315, 99, 14);
        contentPane.add(lblRecommended);

    }
}

0 个答案:

没有答案