package test.example;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class encrypt extends Frame implements ActionListener {
private MenuBar menubar;
private Menu menu1, menu2, menu3, menu4;
private MenuItem menuitem1, menuitem1_1, menuitem2, menuitem2_2, menuitem3,
menuitem22, menuitem22_2, menuitem4, menuitem_t, menuitem_t_t;
private static JTextArea txtarea;
private MenuShortcut ms1, ms2, ms3, ms4;
private File f;
private static char dp = 'a';
encrypt() {
super("加密解密程序");
txtarea = new JTextArea();
menubar = new MenuBar();
menu1 = new Menu("文件");
menuitem1 = new MenuItem("打开");
menuitem1_1 = new MenuItem("保存");
ms1 = new MenuShortcut(KeyEvent.VK_N);
menuitem1.addActionListener(this);
menuitem1_1.addActionListener(this);
menu1.add(menuitem1);
menu1.add(menuitem1_1);
menu2 = new Menu("保护");
menuitem2 = new MenuItem("二进制加密");
menuitem2_2 = new MenuItem("二进制解密");
menuitem22 = new MenuItem("十六进制加密");
menuitem22_2 = new MenuItem("十六进制解密");
menuitem_t = new MenuItem("特殊方式加密");
menuitem_t_t = new MenuItem("特殊方式解密");
menuitem2.addActionListener(this);
ms2 = new MenuShortcut(KeyEvent.VK_A);
menuitem2_2.addActionListener(this);
menu2.add(menuitem2);
menu2.add(menuitem2_2);
menu2.add(menuitem22);
menu2.add(menuitem22_2);
menu2.add(menuitem_t);
menu2.add(menuitem_t_t);
menu3 = new Menu("编辑");
menuitem3 = new MenuItem("清空");
menu3.add(menuitem3);
menuitem3.addActionListener(this);
menuitem22.addActionListener(this);
menuitem22_2.addActionListener(this);
menuitem_t.addActionListener(this);
menuitem_t_t.addActionListener(this);
menu4 = new Menu("帮助");
menuitem4 = new MenuItem("关于");
ms4 = new MenuShortcut(KeyEvent.VK_H);
menuitem4.addActionListener(this);
menu4.add(menuitem4);
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
menubar.add(menu4);
setMenuBar(menubar);
setLayout(new BorderLayout());
add(new JScrollPane(txtarea), BorderLayout.CENTER);
txtarea.setLineWrap(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(900, 600);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == menuitem1) {
try {
file();
qu();
} catch (Exception ee) {
}
;
} else if (e.getSource() == menuitem1_1) {
try {
file();
shuchu();
} catch (Exception ee) {
}
;
} else if (e.getSource() == menuitem2) {
txtarea.setText(fen(encode(txtarea.getText())));
} else if (e.getSource() == menuitem2_2) {
System.out.println(1);
String string = he(txtarea.getText());
if (!string.equals("")) {
txtarea.setText(decode(string));
}
} else if (e.getSource() == menuitem22) {
txtarea.setText(encode(txtarea.getText()));
} else if (e.getSource() == menuitem22_2) {
if (!decode(txtarea.getText()).equals("")) {
txtarea.setText(decode(txtarea.getText()));
}
} else if (e.getSource() == menuitem_t) {
String url = JOptionPane.showInputDialog("请输入一个字符");
if (url.length() > 1)
return;
dp = url.charAt(0);
txtarea.setText(Encryption(txtarea.getText()));
} else if (e.getSource() == menuitem_t_t) {
String url = JOptionPane.showInputDialog("请输入一个字符");
if (url.length() > 1)
return;
dp = url.charAt(0);
txtarea.setText(Decrypt(txtarea.getText()));
} else if (e.getSource() == menuitem3) {
txtarea.setText("");
} else if (e.getSource() == menuitem4) {
txtarea.setText("/n加密解密多少次随自己愿意..只要你记得解密步骤..");
}
}
public static void main(String[] args) {
new encrypt();
}
public void file() {
JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可
if (fileChooser.showOpenDialog(encrypt.this) == JFileChooser.APPROVE_OPTION) { // 弹出文件选择器,并判断是否点击了打开按钮
String fileName = fileChooser.getSelectedFile().getAbsolutePath(); // 得到选择文件或目录的绝对路径
f = new File(fileName);
}
}
private static String decode(String theHex) {
String theRst = "";
byte[] theByte = new byte[theHex.length() / 2];
try {
for (int i = 0; i < theHex.length(); i += 2) {
theByte[i / 2] = Integer.decode(
"0X" + theHex.substring(i, i + 2)).byteValue();
}
theRst = new String(theByte, 0, theByte.length);
} catch (Exception Ue) {
JOptionPane.showMessageDialog(null, "无法继续解密");
}
return theRst;
}
private static String encode(String theStr) {
byte[] bytes;
String result = "";
int tmp;
String tmpStr;
bytes = theStr.getBytes();
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] < 0) {
tmp = 256 + bytes[i];
tmpStr = Integer.toHexString(tmp).toUpperCase();
result += tmpStr;
} else {
tmpStr = Integer.toHexString(bytes[i]).toUpperCase();
result += tmpStr.length() == 1 ? "0" + tmpStr : tmpStr;
}
}
return result;
}
public static String Decrypt(String oldStr) {
char[] oldStrC = oldStr.toCharArray();
char[] newStrC = new char[oldStrC.length];
for (int i = 0; i < oldStrC.length; i++) {
newStrC[i] = (char) (oldStrC[i] ^ dp);
}
String newStr = String.valueOf(newStrC);// 解密后的
return newStr;
}
public static String Encryption(String message) {
char[] old = message.toCharArray();
char[] newC = new char[old.length];
for (int i = 0; i < old.length; i++) { // 加密
newC[i] = (char) (old[i] ^ dp);
}
return String.valueOf(newC);
}
public static String fen(String str) {
byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder();
String ss = "";
String quanbu = "";
for (int i = 0; i < bytes.length; i++) {
char count = (char) bytes[i];
ss = Integer.toBinaryString(Integer.parseInt(Integer.valueOf(
count + "", 16).toString()));
while (ss.length() != 4) {
ss = 0 + ss;
}
quanbu += ss;
}
return quanbu;
}
public static String he(String str) {
String ur = "";
try {
if (str.length() >= 4) {
for (int i = 0; i < str.length() / 4; i++) {
System.out.println(4);
String s = Integer.valueOf(
str.substring((4 * (i + 1)) - 4, 4 * (i + 1)), 2)
.toString();
System.out.println(5);
ur += Integer.toHexString(Integer.parseInt(s));
}
} else {
throw new Exception();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "无法继续解密");
}
return ur.toUpperCase();
}
public void shuchu() {
try {
FileOutputStream out = new FileOutputStream(f);
byte buf[] = txtarea.getText().getBytes();
try {
out.write(buf);
out.flush();
out.close();
} catch (IOException e) {
// e.printStackTrace();
}
} catch (FileNotFoundException e) {
// e.printStackTrace();
}
}
public void qu() {
try {
FileInputStream in = new FileInputStream(f);
int a = (int) f.length();
byte buf[] = new byte[a];
try {
int len = in.read(buf);
if (len == -1)
System.out.println("文件为空");
else
txtarea.setText(new String(buf, 0, len));
} catch (IOException e) {
// e.printStackTrace();
}
} catch (FileNotFoundException e) {
// e.printStackTrace();
}
}
}