📄 aes.java
字号:
}
public void outCode(int code_num[])
{
for(int i = 0; i < 16; i ++)
code_num[i] = Code[i];
}
public void getKey(int key_num[])
{
for(int i = 0; i < 16; i++)
{
key[i] = 0;
Code[i] = 0;
}
for(int i = 0; i < 16; i ++)
{
key[i] = key_num[i]^key[i];
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == encode)
{
/*
* get the keys(the keys is initialized with all 0s.)
* if the keys are shorter or longer than 16 bits,
* it add '0' to the tail
* until it mod(16) equals to 0 .
*/
int i,j;
int []code_num = new int[16];
int []key_num = new int[16];
String temp_key = keytext.getText();
String temp_code = original.getText();
int len_key = temp_key.length();
int len_code = temp_code.length();
if(len_key%16!=0)
{
for(i = len_key%16; i < 16; i ++)
{
temp_key += " ";
}
}
for(i = 0, j = 0; j < temp_key.length(); j ++, i ++)
{
key_num[i] = temp_key.charAt(i);
if(i == 15)
{
getKey(key_num);
i = -1;
}
}
KeyExpansion();
/*
* get the code
* if the keys are shorter or longer than 16 bits,
* it add '0' to the tail
* until it mod(16) equals to 0
*/
//////////////////////////////////////////
String output = "";
if(len_code%16!=0)
{
for(i = len_code%16; i < 16; i++)
temp_code += " ";
}
for(i = 0,j = 0; j < temp_code.length(); j ++, i ++)
{
code_num[i] = (temp_code.charAt(j));
if(i == 15)
{
getCode(code_num);
Encode();
outCode(code_num);
for(i = 0; i < 16; i ++)
{
/*
* change the ASCII code into character
*/
//String aa = String.valueOf(char(code_num[i]));
output += Character.toChars(code_num[i])[0];
}
to_code.setText(output);
i = -1;
}
}
}
/*
* Button event of decode
*/
if(e.getSource() == decode)
{
/*
* get the keys(the keys is initialized with all 0s.)
* if the keys are shorter or longer than 16 bits,
* it add '0' to the tail
* until it mod(16) equals to 0 .
*/
int i,j;
int []code_num = new int[16];
int []key_num = new int[16];
String temp_key = keytext.getText();
String temp_code = to_code.getText();
int len_key = temp_key.length();
int len_code = temp_code.length();
//char shit;
//System.out.print(len_code);
if(len_key%16!=0)
{
for(i = len_key%16; i < 16; i ++)
{
temp_key += " ";
}
}
for(i = 0, j = 0; j < temp_key.length(); j ++, i ++)
{
key_num[i] = temp_key.charAt(i);
if(i == 15)
{
getKey(key_num);
i = -1;
}
}
KeyExpansion();
/*
* get the code
* if the keys are shorter or longer than 16 bits,
* it add '0' to the tail
* until it mod(16) equals to 0
*/
//////////////////////////////////////////
String output = "";
if(len_code%16!=0)
{
for(i = len_code%16; i < 16; i++)
temp_code += " ";
}
for(i = 0,j = 0; j < temp_code.length(); j ++, i ++)
{
code_num[i] = (temp_code.charAt(j));
if(i == 15)
{
getCode(code_num);
Decode();
outCode(code_num);
for(i = 0; i < 16; i ++)
{
output += Character.toChars(code_num[i])[0];
//output += String.valueOf(code_num[i]);
}
to_original.setText(output);
i = -1;
}
}
}
}
/*
* construct function
*/
public AES()
{
Container container = getContentPane();
container.setLayout(new GridLayout(1,1,5,5));
//container.addMouseListener(this);
//container.addMouseMotionListener(this);
JPanel p1 = new JPanel();
//p1.addMouseListener(this);
p1.setLayout(new GridLayout(4,1,5,5));
//p1.add(label = new JLabel());
JPanel p1_1 = new JPanel();
p1_1.setLayout(new BorderLayout());
JPanel p1_2 = new JPanel();
p1_2.setLayout(new GridLayout(2,1,5,5));
JPanel p1_3 = new JPanel();
p1_3.setLayout(new BorderLayout());
JPanel p1_4 = new JPanel();
p1_4.setLayout(new BorderLayout());
//p1_1.add(new JLabel(" 明文"),BorderLayout.NORTH);
p1_1.setBorder(BorderFactory.createTitledBorder("明文 (暂时无法加密汉字)"));
p1_1.add(original = new JTextArea(),BorderLayout.CENTER);
JPanel p1_2_2 = new JPanel();
p1_2_2.setLayout(new BorderLayout());
p1_2_2.setBorder(BorderFactory.createTitledBorder("密钥 (暂时无法使用汉字做密钥)"));
//p1_2_2.add(new JLabel("密钥"),BorderLayout.NORTH);
p1_2_2.add(keytext = new JTextArea(),BorderLayout.CENTER);
p1_2.add(p1_2_2);
JPanel p1_2_1 = new JPanel();
p1_2_1.setLayout(new FlowLayout());
p1_2_1.add(encode = new JButton("加密"));
p1_2_1.add(decode = new JButton("解密"));
p1_2.add(p1_2_1);
//p1_3.add(new JLabel(" 密文"),BorderLayout.NORTH);
p1_3.setBorder(BorderFactory.createTitledBorder("密文 (暂时无法解密汉字)"));
p1_3.add(to_code = new JTextArea(),BorderLayout.CENTER);
p1_4.setBorder(BorderFactory.createTitledBorder("原文 (比较)"));
//p1_4.add(new JLabel(" 原文(比较)"),BorderLayout.NORTH);
p1_4.add(to_original = new JTextArea(),BorderLayout.CENTER);
p1.add(p1_1);
p1.add(p1_2);
p1.add(p1_3);
p1.add(p1_4);
container.add(p1);
original.setLineWrap(true);
p1_1.add(new JScrollPane(original));
to_code.setLineWrap(true);
p1_3.add(new JScrollPane(to_code));
to_original.setLineWrap(true);
p1_4.add(new JScrollPane(to_original));
keytext.setLineWrap(true);
p1_2_2.add(new JScrollPane(keytext));
encode.addActionListener(this);
decode.addActionListener(this);
JMenuBar bar = new JMenuBar();
//JPanel menu = new JPanel();
//menu.setLayout(new GridLayout(1,10));
JMenu theFile = new JMenu("文件");
JMenu editMenu = new JMenu("编辑");
JMenu helpMenu = new JMenu("帮助");
JMenuItem firstMenu = new JMenuItem("退出");
JMenuItem cutMenu = new JMenuItem("剪切");
JMenuItem copyMenu = new JMenuItem("复制");
JMenuItem pasteMenu = new JMenuItem("粘贴");
JMenuItem secondMenu = new JMenuItem("关于");
editMenu.add(cutMenu);
editMenu.add(copyMenu);
editMenu.add(pasteMenu);
theFile.add(firstMenu);
helpMenu.add(secondMenu);
bar.add(theFile);
bar.add(editMenu);
bar.add(helpMenu);
setJMenuBar(bar);
setSize(300,300);
cutMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
original.cut();
keytext.cut();
to_code.cut();
to_original.cut();
}
});
copyMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
original.copy();
keytext.copy();
to_code.copy();
to_original.copy();
}
});
pasteMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//if(original.getCursor() == DEFAULT_CURSOR)
original.paste();
System.out.print(original.getCursor());
keytext.paste();
to_code.paste();
to_original.paste();
}
});
firstMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
secondMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame newF = new JFrame("关于");
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(5,1,10,5));
p1.add(new JLabel("版本AES 1.0"));
p1.add(new JLabel("创建于 2006/6/3"));
p1.add(new JLabel("创建者:计算机B班 李德苑 033511063"));
p1.add(new JLabel("E-mail:ldyhot@163.com"));
p1.add(new JLabel("说明:暂时无法加密与解密汉字"));
newF.add(p1);
newF.setSize(400,200);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
Dimension frameSize = newF.getSize();
int x = (screenWidth - frameSize.width)/2;
int y = (screenHeight - frameSize.height)/2;
newF.setLocation(x,y);
//newF.setVisible(true);
newF.show();
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AES test = new AES();
test.setTitle("AES");
test.setSize(600,600);
test.setVisible(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
Dimension frameSize = test.getSize();
int x = (screenWidth - frameSize.width)/2;
int y = (screenHeight - frameSize.height)/2;
test.setLocation(x,y);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//System.out.println("Welcome to java");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -