我正在尝试创建一个利用文本字段的应用程序,该文本字段允许用户保存文件或加载程序中指定的文件的现有文本。没有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()以外的方法吗?
答案 0 :(得分:2)
Jasypt使用密码派生用于加密和解密数据的加密密钥。这就是加密和解密操作应该相同的原因。
您在代码中执行的操作是:
解密:
这意味着从密码派生的加密密钥将不同,因此解密将失败。