📄 desdemo.java
字号:
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class DESDemo extends Frame
{
private Label l;
private Button b2,b3;
private TextField tf1,tf2,tf3;
byte[] plainText,cipherText,newPlainText;
KeyGenerator KeyGen;
Cipher cipher;
Key key;
String mingwen1,miwen,mingwen2;
public static void main(String[] args)
{
new DESDemo().lanchFrame();
}
private void lanchFrame()
{
Frame f = new Frame();
f.setTitle("DESDemo");
f.setLocation(200, 300);
l = new Label("输入明文");
b2 = new Button("输出密文");
b3 = new Button("解密");
tf1 = new TextField(20);
tf2 = new TextField(20);
tf3 = new TextField(20);
f.setLayout(new FlowLayout());
f.add(l);f.add(tf1);
f.add(b2);f.add(tf2);
f.add(b3);f.add(tf3);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
encryption_ActionPerformed();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
decryption_ActionPerformed();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
public void encryption_ActionPerformed() throws Exception
{
mingwen1 = tf1.getText();
plainText=mingwen1.getBytes();
KeyGen=KeyGenerator.getInstance("DES");
KeyGen.init(56);
key=KeyGen.generateKey();
cipher=Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText=cipher.doFinal(plainText);
tf2.setText(new String(cipherText,"UTF8"));
}
public void decryption_ActionPerformed() throws Exception
{
cipher.init(Cipher.DECRYPT_MODE, key);
newPlainText=cipher.doFinal(cipherText);
tf3.setText((new String(newPlainText, "UTF8")));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -