📄 girl_gui.java
字号:
else if (event.getSource() == sign_button)
{
if (judge_authen_input())
{
File sign_src_file = get_authen_input_file();
File sign_dst_file = select_dst_file();
if (sign_dst_file == null)
{
return;
}
do_sign(sign_src_file, sign_dst_file);
}
}
else if (event.getSource() == verify_button)
{
if (judge_authen_input())
{
File verify_src_file = get_authen_input_file();
do_verify(verify_src_file);
}
}
}
}
//do the message decryption
public void do_decryp(File src_file, File dst_file)
{
FileInputStream fin;
try
{
fin = new FileInputStream(src_file);
BufferedInputStream buf_input = new BufferedInputStream(fin);
//read the src file content into a byte array instead of into a string buffer
byte byte_array[] = new byte[buf_input.available()];
buf_input.read(byte_array);
buf_input.close();
fin.close();
//get the corresponding key and cipher and generates the cipher
Key decryp_key = null;
if (this.selected_key_algorithm_name == "DES")
{
decryp_key = this.key_center.get_symmetric_DES_key(this.myCipher);
this.myCipher.generate_cipher(this.selected_algorithm_name);
}
else if (this.selected_key_algorithm_name == "AES")
{
decryp_key = this.key_center.get_symmetric_AES_key(this.myCipher);
this.myCipher.generate_cipher(this.selected_algorithm_name);
}
else if (this.selected_key_algorithm_name == "RSA")
{
decryp_key = this.key_center.get_girl_private_key(this.myCipher);
this.myCipher.generate_cipher(this.selected_algorithm_name);
}
//begins to decrypt the message using the decryp_key
byte[] decryp_text = null;
if (this.selected_key_algorithm_name == "DES" || this.selected_key_algorithm_name == "AES")
{
this.myCipher.decryp_message(byte_array, decryp_key);
decryp_text = this.myCipher.get_decrypText();
}
else if (this.selected_key_algorithm_name == "RSA")
{
this.myCipher.decryp_RSA(byte_array, decryp_key);
decryp_text = this.myCipher.get_RSA_decrypText();
}
//write the plain text in bytes into the target file
FileOutputStream fout = new FileOutputStream(dst_file);
BufferedOutputStream buf_output = new BufferedOutputStream(fout);
buf_output.write(decryp_text, 0, decryp_text.length);
buf_output.close();
fout.close();
//print the plaintext after decryption in the text area
if (new String(decryp_text, "UTF-8").length() < 100*1024)
{
String output_to_console = " 解密后的明文如下:\n\n";
this.output_to_textarea(output_to_console + new String(decryp_text));
}
else
{
String output_to_console = " 解密后的部分明文如下:\n\n";
this.output_to_textarea(output_to_console + new String(decryp_text, 0, 100*1024));
}
JOptionPane.showMessageDialog(null," HI, 美女~\n 解密已经完成了,快去看看吧 !","提示",JOptionPane.INFORMATION_MESSAGE);
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
//do the message encryption
public void do_encryp(File src_file, File dst_file)
{
FileInputStream fin;
try
{
fin = new FileInputStream(src_file);
BufferedInputStream buf_input = new BufferedInputStream(fin);
//read the src file content into a byte array instead of into a string buffer
byte byte_array[] = new byte[buf_input.available()];
buf_input.read(byte_array);
buf_input.close();
fin.close();
//gets the corresponding key and generates the cipher
Key encryp_key = null;
if (this.selected_key_algorithm_name == "DES")
{
encryp_key = this.key_center.get_symmetric_DES_key(this.myCipher);
this.myCipher.generate_cipher(this.selected_algorithm_name);
}
else if (this.selected_key_algorithm_name == "AES")
{
encryp_key = this.key_center.get_symmetric_AES_key(this.myCipher);
this.myCipher.generate_cipher(this.selected_algorithm_name);
}
else if (this.selected_key_algorithm_name == "RSA")
{
encryp_key = this.key_center.get_boy_public_key(this.myCipher);
this.myCipher.generate_cipher(this.selected_algorithm_name);
}
//begins to encrypt the message using the encryp_key
byte[] encryp_text = null;
if (this.selected_key_algorithm_name == "DES" || this.selected_key_algorithm_name == "AES")
{
this.myCipher.encryp_message(byte_array, encryp_key);
encryp_text = this.myCipher.get_encrypText();
}
else if (this.selected_key_algorithm_name == "RSA")
{
this.myCipher.encryp_RSA(byte_array, encryp_key);
encryp_text = this.myCipher.get_RSA_encrypText();
}
//write the cipher text in byte into the target file
FileOutputStream fout = new FileOutputStream(dst_file);
BufferedOutputStream buf_output = new BufferedOutputStream(fout);
buf_output.write(encryp_text, 0, encryp_text.length);
buf_output.close();
fout.close();
//print the cipher text to the text area
if (new String(encryp_text, "UTF-8").length() <= 100*1024)
{
String output_to_console = " 加密后的密文如下:\n\n";
this.output_to_textarea(output_to_console + new String(encryp_text));
}
else
{
String output_to_console = " 加密后的部分密文如下:\n\n";
this.output_to_textarea(output_to_console + new String(encryp_text, 0, 100*1024));
}
JOptionPane.showMessageDialog(null," HI, 美女~\n 加密已经完成了,快去看看吧 !","提示",JOptionPane.INFORMATION_MESSAGE);
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
//do the MAC signing
public void do_sign(File src_file, File dst_file)
{
FileInputStream fin;
try
{
fin = new FileInputStream(src_file);
BufferedInputStream buf_input = new BufferedInputStream(fin);
//read the src file content into a byte array instead of into a string buffer
byte byte_array[] = new byte[buf_input.available()];
buf_input.read(byte_array);
buf_input.close();
fin.close();
//generate the Mac object and use it to make the authentication codes
byte[] mac_bytes = this.mac_center.generate_MAC(byte_array, this.myCipher);
//append the MAC in byte into the target file
FileOutputStream fout = new FileOutputStream(dst_file);
BufferedOutputStream buf_output = new BufferedOutputStream(fout);
buf_output.write(byte_array);
buf_output.write(mac_bytes, 0, mac_bytes.length);
buf_output.close();
fout.close();
JOptionPane.showMessageDialog(null," HI, 美女~\n MAC认证码已经生成并添加至文件了 !","提示",JOptionPane.INFORMATION_MESSAGE);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
//do the MAC verifying
public void do_verify(File src_file)
{
FileInputStream fin;
try
{
fin = new FileInputStream(src_file);
BufferedInputStream buf_input = new BufferedInputStream(fin);
//read the src file content into a byte array instead of into a string buffer
byte byte_array[] = new byte[buf_input.available()];
buf_input.read(byte_array);
buf_input.close();
fin.close();
if (this.mac_center.verify_MAC(byte_array, this.myCipher) == true)
{
JOptionPane.showMessageDialog(null," HI, 美女~\n 该文件已经成功通过MAC认证了 !放心使用吧。","提示",JOptionPane.INFORMATION_MESSAGE);
//append the MAC in byte into the target file
FileOutputStream fout = new FileOutputStream(src_file);
BufferedOutputStream buf_output = new BufferedOutputStream(fout);
buf_output.write(byte_array, 0, byte_array.length-16);
buf_output.close();
fout.close();
}
else
{
JOptionPane.showMessageDialog(null," HI, 美女~\n 该文件没有通过MAC认证 !可能被某些人恶意修改过。","警告",JOptionPane.WARNING_MESSAGE);
src_file.delete();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -