Java 文件 I/O 不适用于外部库方法

标签 java encryption file-io jasypt

我正在尝试创建一个使用文本字段的应用程序,该文本字段允许用户保存文件或加载程序中指定的文件的现有文本。如果没有 Jasypt 库调用,程序就可以正常工作。然而,我的最终目标是将输入的文本作为加密文本保存在txt文件中,并且当该文件读入文本字段时,它被解密。我的程序的保存部分工作正常。但是,我的加载事件无法正常工作。

我的程序如下所示:

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

import org.jasypt.util.password.StrongPasswordEncryptor;
import org.jasypt.util.text.*;

public class CheesyWP extends JFrame implements ActionListener {
    StrongTextEncryptor textEncryptor = new StrongTextEncryptor();
    /**
     * @param args
     */
    Panel center;
    Panel south;
    JMenuItem save;
    JMenuItem load;
    JMenuItem clip;
    JMenuItem finish;
    TextArea ta;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CheesyWP cwp = new CheesyWP();
        cwp.doIt();

    }

    public void doIt() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1000, 400);
        center = new Panel();
        south = new Panel();
        clip = new JMenuItem("Open Clipboard");
        save = new JMenuItem("Save");
        load = new JMenuItem("Load");
        finish = new JMenuItem("Finish");

        JMenuBar j = new JMenuBar();


        JMenu option = new JMenu("Options");

        j.add(option);
        option.add(clip);
        option.add(save);
        option.add(load);
        option.add(finish);

        setJMenuBar(j);



        clip.addActionListener(this);
        save.addActionListener(this);
        load.addActionListener(this);
        finish.addActionListener(this);
        ta = new TextArea(20, 80);
        center.add(ta);

        /*south.add(load);
        south.add(save);
        south.add(clip);
        south.add(finish);*/

        this.add(center, BorderLayout.CENTER);
        this.add(south, BorderLayout.SOUTH);
        this.setSize(600, 300);
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == save) {
            try {
                File junk = new File("junk.txt");
                FileWriter fw = new FileWriter(junk);

                String text = ta.getText();
                textEncryptor.setPassword(text);
                String myEncryptedText = textEncryptor.encrypt(text);
                fw.write(myEncryptedText); // write whole TextArea contents
                fw.close();
            } catch (IOException ioe) {
            }
        }// ends if
        if (ae.getSource() == load) {
            StrongTextEncryptor textEncryptor = new StrongTextEncryptor();
            String temp = "";
            try {
                File junk = new File("junk.txt");
                FileReader fr = new FileReader(junk);

                BufferedReader br = new BufferedReader(fr);
                String decrypt;
                while ((temp = br.readLine()) != null) {
                    textEncryptor.setPassword(temp);
                    decrypt = textEncryptor.decrypt(temp);
                    ta.append(decrypt);
                }
                br.close();
            } catch (FileNotFoundException fnfe) {
            } catch (IOException ioe) {
            }
        }
        if (ae.getSource() == finish) {
            System.exit(0);
        }
        if(ae.getSource()==clip){
            new ClipBoard();
        }
    }

    class ClipBoard extends JFrame {
        public ClipBoard() { // a constructor
            this.setTitle("Clipboard");
            this.setLayout(new FlowLayout());
            this.add(new TextArea(10, 50));
            this.setSize(400, 160);
            this.setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    }
}

加载时出现以下错误:

Exception in thread "AWT-EventQueue-0" org.jasypt.exceptions.EncryptionOperationNotPossibleException
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.decrypt(StandardPBEByteEncryptor.java:1055)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:725)
    at org.jasypt.util.text.StrongTextEncryptor.decrypt(StrongTextEncryptor.java:118)
    at CheesyWP.actionPerformed(CheesyWP.java:100)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:389)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809)
    at com.apple.laf.AquaMenuItemUI.doClick(AquaMenuItemUI.java:137)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850)
    at java.awt.Component.processMouseEvent(Component.java:6414)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6179)
    at java.awt.Container.processEvent(Container.java:2084)
    at java.awt.Component.dispatchEventImpl(Component.java:4776)
    at java.awt.Container.dispatchEventImpl(Container.java:2142)
    at java.awt.Component.dispatchEvent(Component.java:4604)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
    at java.awt.Container.dispatchEventImpl(Container.java:2128)
    at java.awt.Window.dispatchEventImpl(Window.java:2492)
    at java.awt.Component.dispatchEvent(Component.java:4604)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:690)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

我应该使用 StrongTextEncryptor 以外的类吗?我应该使用 .encrypt() 和 .decrypt() 以外的方法吗?

最佳答案

Jasypt 使用密码来派生用于加密和解密您的数据的加密 key 。这就是为什么加密和解密操作应该是相同的。

您在代码中所做的事情是这样的:

  1. 从明确的字段中获取密码
  2. 将其设置为 StrongtextEncryptor 中的密码
  3. 加密密码并将其存储到文件中

用于解密:

  1. 您读取了加密密码
  2. 尝试解密数据

这意味着从密码派生的加密 key 将不同,因此解密会失败。

关于Java 文件 I/O 不适用于外部库方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23765514/

相关文章:

java - 如何在两个不同的 CRM 系统中自动化复制故障单?

java - 正则表达式替换java中a标签中href属性后的=

python - "SignatureError: Failed to verify signature"- Okta,pySAML2

java - 我如何使用文本文件

Java:查找用户设置的多个整数列表中的最大数字

java - 关于多线程的困惑

javascript - Android 上基于密码的 AES 加密和使用 CryptoJS 解密

python - HMAC-SHA256 与 CBC 模式下的 AES-256

perl - 如何解决 Perl 中的 "print() on closed filehandle"错误?

c - 如何使用C检查文件是否有内容?