📄 code.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.math.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.swing.JOptionPane;
class Type
{
char data;
int x,y,z;
Type(int m,int n,int t)
{x=m;y=n;z=t;}
Type(char m)
{this.data=m;}
Type(){ };
}
class Node
{
char info;
String code;
Node(char a)
{
this.info=a;
this.code=new String();
}
Node(){this.code=new String(); }
public void addcode(char i)
{
this.code=code+i;
}
}
class TTree
{
char flag;
char info;
int count;
TTree lchild,rchild,parent;
TTree(char data)
{
this.info=data;
this.flag='2';
this.count=1;
this.lchild=null;
this.rchild=null;
this.parent=null;
}
TTree(char data,TTree left,TTree right)
{
this.flag='2';
this.info=data;
this.count=left.count+right.count;
this.lchild=left;
this.lchild.flag='1';
this.rchild=right;
this.rchild.flag='0';
}
TTree(){ }
public void addcount()
{
this.count++;
}
}
class Tstack
{
LinkedList mylist=new LinkedList();
public void push(TTree c)
{
mylist.add(c);
}
public TTree pop()
{
TTree temp=new TTree();
if(mylist.size()!=0) {temp=(TTree)mylist.getLast(); mylist.removeLast();return (TTree)temp;}
else return null;
}
public boolean empty()
{
if(mylist.size()==0) return true;
else return false;
}
public TTree gettop()
{
if(mylist.size()!=0) return (TTree)mylist.getLast();
else return null;
}
}
class Sstack
{
LinkedList mylist=new LinkedList();
public void push(Type c)
{
mylist.add(c);
}
public Type pop()
{
Type temp=new Type();
if(mylist.size()!=0) {temp=(Type)mylist.getLast(); mylist.removeLast();return (Type)temp;}
else return null;
}
public boolean empty()
{
if(mylist.size()==0) return true;
else return false;
}
public Type gettop()
{if(mylist.size()!=0) return (Type)mylist.getLast();
else return null;
}
}
class rsa
{
int Ddata[];
rsa(String str)
{
Ddata=new int[10000];
int i;
char s[]=str.toCharArray();
for(i=1;i<=str.length();i++)
Ddata[i]=s[str.length()-i]-'0';
for(;i<10000;i++)
Ddata[i]=0;
Ddata[0]=str.length();
}
rsa(int st)
{
int t=st;
int i;
for(i=1;i<10000;i++)
{
Ddata[i]=t%10;
t=t/10;
if(t==0)
break;
}
Ddata[0]=i;
++i;
for(;i<10000;i++)
Ddata[i]=0;
}
rsa(char c)
{
int t=(int)c;
int i;
for(i=1;i<10000;i++)
{
Ddata[i]=t%10;
t=t/10;
if(t==0)
break;
}
Ddata[0]=i;
++i;
for(;i<10000;i++)
Ddata[i]=0;
}
}
class Mycanvas extends Canvas
{
TTree T ;
String str;
Mycanvas(TTree Current)
{
setSize(1024,768);
setBackground(Color.cyan);
T=Current;
}
public int TreeDepth(TTree Current1 )
{
if(Current1==null) return 0;
else
{ int h1=TreeDepth(Current1.lchild);
int h2=TreeDepth(Current1.rchild);
return (h1>h2?h1:h2)+1;
}
}
public void paint(Graphics g)
{
g.setColor(Color.red);
int i=0;
int depth=TreeDepth(T);
int wideth=0;
int len,wide;
int bi,wi ;
Type temp1;
Tstack s=new Tstack();
Sstack t=new Sstack();
len=1024;
wide=720;
wi=len/2;
bi=wide/(depth+1);
int y=bi,x=len/2,m ,n;
n=len;
for(i=0;i<depth;i++)
{ n=n/2;}
m=n;
TTree temp=T;
str=new String();
while(s.gettop()!=null||temp!=null)
{
if(temp!=null)
{
if(temp.lchild==null&&temp.rchild==null)
{
g.drawOval(x-m/2,y-n/2,m,n);
str=""+temp.info;
g.drawString(str,x,y);
}
else
{
g.drawRect(x-m/2 ,y-n/2 ,m,n );
str=""+temp.count;
g.drawString(str,x ,y );
if(temp.lchild!=null)
{
g.drawLine(x ,y ,x-wi/2 ,y+bi-10 );
str=""+1;
g.drawString(str,(2*x-wi/2)/2 ,(2*y+bi-10)/2+10);
}
if(temp.rchild!=null)
{
g.drawLine(x ,y ,x +wi/2 ,y+bi-10);
str=""+0;
g.drawString(str,(2*x+wi/2)/2 ,(2*y+bi-10)/2+10 );
}
}
s.push(temp);
temp=temp.lchild;
temp1=new Type(x,y,wi);
t.push(temp1);
wi=wi/2;
x=x-wi;
y=y+bi;
}
else
{
temp1=t.pop();
temp=s.pop();
temp=temp.rchild;
wi=temp1.z/2;
x=temp1.x+wi;
y=temp1.y+bi;
}
}
}}
class windowcan extends Frame
{
Mycanvas can;
windowcan(TTree T)
{
this.setVisible(true);
this.setBackground(Color.red);
can=new Mycanvas(T);
add(can,BorderLayout.CENTER);
setBounds(0,0,1024,720);
this.setResizable(false);
setVisible(true);
validate();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ setVisible(false);} } );}
}
class Code extends JFrame implements ActionListener
{
JTextPane textPane ;
JFileChooser filechooser = new JFileChooser();
LinkedList list3=new LinkedList();
JDialog subwin;
int x;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JTextField text1,text2;
TTree TT;
int coden,codee,coded;
int Listlength;
boolean listflag,listopenflag;
String replacecode;
boolean replacesetflag,replaceopenflag;
boolean dessetflag,descodeflag,desopenflag;
int s[];
char list[];
char replace[];
boolean rsasetflag,rsacodeflag,rsaopenflag;
public Code()
{
super("文件加密系统");
Action[] actions =
{
new NewAction(),
new OpenAction(),
new SaveAction(),
new CutAction(),
new CopyAction(),
new PasteAction(),
new ExitAction(),
new ComplierAction(),
new ConvertAction(),
new PrintAction(),
new RsasetcodeAction(),
new RsasetAction(),
new RsaopenAction(),
new DessetcodeAction(),
new DessetAction(),
new DesopenAction(),
new ListsetlengthAction(),
new ListsetAction(),
new ListopenAction(),
new ReplacesetwordAction(),
new ReplacesetAction(),
new ReplaceopenAction()
};
button1=new JButton("确定");
button2=new JButton("取消");
button3=new JButton("确定");
button4=new JButton("确定");
button5=new JButton("确定");
button6=new JButton("确定");
button7=new JButton("确定");
button8=new JButton("确定");
button9=new JButton("确定");
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
String th=new String("abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ");
th=th+(char)13;
th=th+(char)10;
list=th.toCharArray();
s=new int[10000];
rsasetflag=false;
rsacodeflag=false;
rsaopenflag=false;
listflag=false;
listopenflag=false;
replacesetflag=false;
replaceopenflag=false;
TT=new TTree();
setJMenuBar(createJMenuBar(actions));
Container container = getContentPane();
textPane = new JTextPane();
textPane.setAutoscrolls(true);
container.add(textPane, BorderLayout.CENTER);
setSize(1024, 720);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JMenuBar createJMenuBar(Action[] actions)
{
JMenuBar menubar = new JMenuBar();
JMenu menuFile = new JMenu("文件");
JMenu menuEdit = new JMenu("编辑");
JMenu menuHuff = new JMenu("哈夫曼加密");
JMenu menuRsa = new JMenu("非对称加密");
JMenu menuDes = new JMenu("对称加密");
JMenu menuList = new JMenu("查表加密");
JMenu menuReplace=new JMenu("替换加密");
menuFile.add(new JMenuItem(actions[0]));
menuFile.add(new JMenuItem(actions[1]));
menuFile.add(new JMenuItem(actions[2]));
menuFile.addSeparator();
menuFile.add(new JMenuItem(actions[6]));
menuEdit.add(new JMenuItem(actions[3]));
menuEdit.add(new JMenuItem(actions[4]));
menuEdit.add(new JMenuItem(actions[5]));
menuHuff.add(new JMenuItem(actions[7]));
menuHuff.add(new JMenuItem(actions[8]));
menuHuff.add(new JMenuItem(actions[9]));
menuRsa.add(new JMenuItem(actions[10]));
menuRsa.add(new JMenuItem(actions[11]));
menuRsa.add(new JMenuItem(actions[12]));
menuDes.add(new JMenuItem(actions[13]));
menuDes.add(new JMenuItem(actions[14]));
menuDes.add(new JMenuItem(actions[15]));
menuList.add(new JMenuItem(actions[16]));
menuList.add(new JMenuItem(actions[17]));
menuList.add(new JMenuItem(actions[18]));
menuReplace.add(new JMenuItem(actions[19]));
menuReplace.add(new JMenuItem(actions[20]));
menuReplace.add(new JMenuItem(actions[21]));
menubar.add(menuFile);
menubar.add(menuEdit);
menubar.add(menuHuff);
menubar.add(menuRsa);
menubar.add(menuDes);
menubar.add(menuList);
menubar.add(menuReplace);
return menubar;
}
boolean checkformat(String word) //判断输入字符是否为数字
{
char cword[]=word.toCharArray();
for(int i=0;i<cword.length;i++)
if(!(Character.isDigit(cword[i])) )
return false;
return true;
}
class NewAction extends AbstractAction
{
public NewAction()
{
super("新建");
}
public void actionPerformed(ActionEvent e)
{
textPane.setDocument(new DefaultStyledDocument());
}
}
class OpenAction extends AbstractAction
{
public OpenAction()
{
super("打开");
}
public void actionPerformed(ActionEvent e)
{
int i = filechooser.showOpenDialog(Code.this);
if (i == JFileChooser.APPROVE_OPTION)
{
File f = filechooser.getSelectedFile();
try
{
InputStream is = new FileInputStream(f);
textPane.read(is, "d");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}
class SaveAction extends AbstractAction
{
public SaveAction()
{
super("保存");
}
public void actionPerformed(ActionEvent e)
{
int i = filechooser.showSaveDialog(Code.this);
if (i == JFileChooser.APPROVE_OPTION)
{
File f = filechooser.getSelectedFile();
try
{
FileOutputStream out = new FileOutputStream(f);
out.write(textPane.getText().getBytes());
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}
class ExitAction extends AbstractAction
{
public ExitAction()
{
super("退出");
}
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
class CutAction extends AbstractAction
{
public CutAction()
{
super("剪切");
}
public void actionPerformed(ActionEvent e)
{
textPane.cut();
}
}
class CopyAction extends AbstractAction
{
public CopyAction()
{
super("拷贝");
}
public void actionPerformed(ActionEvent e)
{
textPane.copy();
}
}
class PasteAction extends AbstractAction
{
public PasteAction()
{
super("粘贴");
}
public void actionPerformed(ActionEvent e)
{
textPane.paste();
}
}
class Warning extends javax.swing.JOptionPane
{
Warning(String s,String t)
{
javax.swing.JOptionPane.showMessageDialog(this,t,s,JOptionPane.WARNING_MESSAGE);
}
}
class Saveing
{
public Saveing()
{
int i = filechooser.showSaveDialog(Code.this);
if (i == JFileChooser.APPROVE_OPTION)
{
File f = filechooser.getSelectedFile();
try
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -