📄 framede.java
字号:
//package FrameDe;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.security.Key;
public class framede{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
f_frame f = new f_frame();
}
}
class f_frame extends JFrame implements ActionListener{
String coverText,ciperText;
byte[] plainText,cipherText,newPlainText;
Key key;
KeyGenerator KeyGen;
Cipher cipher;
TextField tfid0,tfid1,tfid2;
Label id0,id1,id2;
Button buttonO,buttonD;
f_frame(){
super("加密解密");
setLayout(null);
setLocation(200, 200);
setVisible(true);
setSize(450,400);
getContentPane().setBackground(Color.green);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
id0 = new Label("明文:");
id1 = new Label("密文:");
id2 = new Label("解析:");
tfid0 = new TextField();
tfid1 = new TextField();
tfid2 = new TextField();
buttonO = new Button("输出密文");
buttonD = new Button("解析密文");
id0.setBounds(30, 100, 40, 20);
id1.setBounds(30, 140, 40, 20);
id2.setBounds(30, 180, 40, 20);
tfid0.setBounds(100, 100, 300, 20);
tfid1.setBounds(100, 140, 300, 20);
tfid2.setBounds(100, 180, 300, 20);
buttonO.setBounds(130,250,80,30);
buttonD.setBounds(250,250,80,30);
add(id0);
add(id1);
add(id2);
add(tfid0);
add(tfid1);
add(tfid2);
add(buttonO);
add(buttonD);
buttonD.setEnabled(false);
buttonO.addActionListener(this);
buttonD.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == buttonO){
String text = tfid0.getText().trim();
if(text.length() == 0){
JOptionPane.showMessageDialog(null, "明文不能为空");
}else{
try {
encryption_ActionPerformed();
buttonD.setEnabled(true);
JOptionPane.showMessageDialog(null, "加密成功");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if(e.getSource() == buttonD){
try {
decryption_ActionPerformed();
buttonD.setEnabled(false);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "解密成功");
}
}
public void encryption_ActionPerformed() throws Exception{
coverText = tfid0.getText().trim();
plainText=coverText.getBytes();
KeyGen=KeyGenerator.getInstance("DES");
KeyGen.init(56);
key=(Key) KeyGen.generateKey();
cipher=Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, (java.security.Key) key);
cipherText=cipher.doFinal(plainText);
tfid1.setText(new String(cipherText,"UTF8"));
}
public void decryption_ActionPerformed() throws Exception{
cipher.init(Cipher.DECRYPT_MODE, (java.security.Key) key);
newPlainText=cipher.doFinal(cipherText);
tfid2.setText((new String(newPlainText, "UTF8")));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -