重新启动程序后,我无法加密解密

时间:2019-07-19 12:52:07

标签: java swing encryption bouncycastle

我想在秋千上扩大我的知识。因此,我开始观看有关摇摆/解密的教程。我找到了本教程:

  

第1部分:https://www.youtube.com/watch?v=A41LMCEXZBo
  第2部分:https://www.youtube.com/watch?v=SIS2EbkT9AM

现在的问题是,本教程存在一些错误,例如您可以解密某些内容并将其复制。重新启动程序并想要对复制的解密进行加密后,您将获得一个NPE。

我在此程序中使用的安全提供程序:https://www.bouncycastle.org/latest_releases.html

错误

enter image description here

java.lang.NullPointerException
at view.AppView$2.actionPerformed(AppView.java:110)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我的代码

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.Security;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class AppView extends JFrame {

    private JButton decryptButton;
    private JPanel rootPanel;
    private JTextArea decryptField;
    private JTextArea encryptField;
    private JButton encryptButton;

    private Dimension screenSize;

    private byte[] input;
    private byte[] keyBytes = "12345678".getBytes();
    private byte[] ivBytes = "input123".getBytes();

    private SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
    private IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    private Cipher cipher;
    private byte[] cipherText;
    private int ctLength;

    public AppView() {

        init();

        setTitle("Decryptor");
        setSize(screenSize);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setContentPane(rootPanel);
        setVisible(true);

        action();

    }

    private void function() {

    }

    private void init() {

        screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        function();
    }

    private void action() {

        decryptButton.addActionListener(new ActionListener() {
            /**
             * Invoked when an action occurs.
             */
            @Override
            public void actionPerformed(ActionEvent e) {

                try {

                    Security.addProvider(new BouncyCastleProvider());
                    input = decryptField.getText().getBytes();
                    SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
                    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
                    cipher = Cipher.getInstance("DES/CTR/NoPadding", "BC");

                    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);

                    cipherText = new byte[cipher.getOutputSize(input.length)];
                    ctLength = cipher.update(input, 0, input.length, cipherText, 0);

                    ctLength += cipher.doFinal(cipherText, ctLength);
                    encryptField.setText(new String(cipherText));


                } catch (Exception ex) {

                    JOptionPane.showMessageDialog(null, ex);
                }

            }
        });

        encryptButton.addActionListener(new ActionListener() {
            /**
             * Invoked when an action occurs.
             */
            @Override
            public void actionPerformed(ActionEvent e) {

                try {

                    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);

                    byte[] plaintext = new byte[cipher.getOutputSize(ctLength)];
                    int ptLength = cipher.update(cipherText, 0, ctLength, plaintext, 0);
                    ptLength += cipher.doFinal(plaintext, ptLength);

                    decryptField.setText(new String(plaintext));

                }
                catch (Exception ex) {

                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, ex);
                }
            }
        });
    }

}

为什么NPE被抛出,我该如何解决这个问题?

0 个答案:

没有答案