Skip to content

Instantly share code, notes, and snippets.

@ZhiCYue
Created April 10, 2012 05:49
Show Gist options
  • Select an option

  • Save ZhiCYue/2348534 to your computer and use it in GitHub Desktop.

Select an option

Save ZhiCYue/2348534 to your computer and use it in GitHub Desktop.
Classical_encryption
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;
public class Encryption extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JLabel reminder;
private JTextField textField_2;
private JTextField textField_1;
private JTextField textField_3;
private String message;
private String key;
private JTextField txtkey;
private boolean hasMessage=false;
private boolean hasKey=false;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Encryption frame = new Encryption();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Encryption() {
setTitle("Caesar algorithm");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 361, 226);
contentPane = new JPanel();
contentPane.setBackground(SystemColor.inactiveCaption);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
{
textField_1 = new JTextField();
textField_1.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
textField_1.setText("");
message = "";
hasMessage = false;
}
@Override
public void focusLost(FocusEvent e) {
if(textField_1.getText().equals("")){
textField_1.setFont(new Font("宋体", Font.ITALIC, 12));
textField_1.setText("\u8F93\u5165\u660E\u6587\u6216\u5BC6\u6587...");
hasMessage = false;
}else{
message = textField_1.getText();
hasMessage = true;
}
}
});
textField_1.setToolTipText("");
textField_1.setBounds(51, 22, 281, 31);
contentPane.add(textField_1);
textField_1.setColumns(10);
}
{
txtkey = new JTextField();
txtkey.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
key = "";
hasKey = false;
txtkey.setText("");
}
@Override
public void focusLost(FocusEvent e) {
if(txtkey.getText().equals("")){
txtkey.setFont(new Font("宋体", Font.ITALIC, 12));
txtkey.setText("\u8F93\u5165key...");
hasKey = false;
}else{
key = txtkey.getText();
hasKey = true;
reminder.setText("");
}
}
});
txtkey.setFont(new Font("宋体", Font.ITALIC, 12));
txtkey.setText("\u8F93\u5165key...");
txtkey.setBounds(51, 63, 76, 32);
contentPane.add(txtkey);
txtkey.setColumns(10);
}
{
textField_2 = new JTextField();
textField_2.setEnabled(false);
textField_2.setBounds(51, 105, 210, 31);
contentPane.add(textField_2);
textField_2.setColumns(10);
}
{
textField_3 = new JTextField();
textField_3.setEnabled(false);
textField_3.setBounds(51, 146, 210, 31);
contentPane.add(textField_3);
textField_3.setColumns(10);
}
{
JLabel label = new JLabel("\u62A5 \u6587");
label.setForeground(new Color(0, 0, 0));
label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBounds(0, 26, 54, 23);
contentPane.add(label);
}
{
JLabel label = new JLabel("\u5BC6 \u94A5");
label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBounds(0, 67, 54, 23);
contentPane.add(label);
}
{
JLabel label = new JLabel("\u52A0 \u5BC6");
label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBounds(0, 109, 54, 23);
contentPane.add(label);
}
{
JLabel label = new JLabel("\u89E3 \u5BC6");
label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBounds(0, 150, 54, 23);
contentPane.add(label);
}
{
JButton button = new JButton("\u52A0\u5BC6");
button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(hasMessage==true && hasKey==true){
textField_2.setText(encryption());
}else{
reminder.setText("不能为空");
}
}
});
button.setBounds(271, 105, 61, 31);
contentPane.add(button);
}
{
JButton button = new JButton("\u89E3\u5BC6");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(hasMessage==true && hasKey==true){
textField_3.setText(decryption());
}else{
reminder.setText("不能为空");
}
}
});
button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
button.setBounds(271, 146, 61, 31);
contentPane.add(button);
}
{
JButton button = new JButton("\u5173\u95ED");
button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
});
button.setBounds(271, 63, 61, 31);
contentPane.add(button);
}
{
JButton button = new JButton("\u91CD\u7F6E");
button.setFont(new Font("微软雅黑", Font.PLAIN, 12));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField_1.setText("");
textField_2.setText("");
txtkey.setText("");
textField_3.setText("");
hasMessage = false;
hasKey = false;
message = "";
key = "";
reminder.setText("");
}
});
button.setForeground(new Color(25, 25, 112));
button.setBounds(192, 64, 61, 31);
contentPane.add(button);
}
{
reminder = new JLabel("");
reminder.setFont(new Font("微软雅黑", Font.PLAIN, 12));
reminder.setBounds(128, 66, 54, 24);
contentPane.add(reminder);
}
}
/*
* encryption()
*/
public String encryption(){
char[] array=message.toCharArray();
try{
int mode=Integer.parseInt(key);
mode=mode%26;
for(int i=0; i<array.length; i++)
{
array[i] = (char)(array[i] + mode);
}
}catch(Exception e){
reminder.setText("ERROR!");
}
return new String(array);
}
/*
* decryption()
*/
public String decryption(){
char[] array=message.toCharArray();
try{
int mode=Integer.parseInt(key);
mode=mode%26;
for(int i=0; i<array.length; i++)
{
array[i] = (char)(array[i] - mode);
}
}catch(Exception e){
reminder.setText("ERROR!");
}
return new String(array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment