📄 client.java
字号:
if(e.getSource()==b1)
{if(!(t1.getText().equals(Client.user_word))){wmessage updatemiss1=new wmessage("错误提示"," 密码错误","wrong.jpg");updatemiss1.setVisible(true);}
else if(!(t2.getText().equals(t3.getText()))){wmessage updatemiss2=new wmessage("错误提示"," 两次输入密码不同","wrong.jpg");updatemiss2.setVisible(true);}
else {try{Client.output.writeUTF(Client.hong.RSA("updata"+Client.user_name+"\1"+t2.getText()));}
catch(Exception e1){}
}
}
else if(e.getSource()==b2) {this.setVisible(false);}
}
}
/****************************************提示窗口****************************************************************/
class wmessage extends Frame implements ActionListener
{static Label label;Panel p=new Panel();
static Button button=new Button("确定");
Imagecanvas canvas;
wmessage(String s,String s2,String s3)
{super(s);
setBounds(400,270,317,180);
setVisible(true);
setResizable(false);
setLayout(null);
canvas=new Imagecanvas(s3);
label=new Label(s2);
add(label);add(button);add(canvas);
label.setBounds(100,80,150,20);canvas.setBounds(-20,10,350,200);
button.setBounds(150,130,50,25);
button.addActionListener(this);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{dispose();
}
});
}
public void actionPerformed(ActionEvent e)
{this.dispose();}
}
/****************************************验证密码窗口****************************************************************/
class compare extends Frame implements ActionListener
{Label label2=new Label("请输入密码");
Button b1=new Button("确定");Button b2=new Button("取消");Panel pcen=new Panel();Panel psou=new Panel();
TextField t2=new TextField(10);
compare()
{super("密码确认");
setBounds(300,200,350,200);
setVisible(false);
setResizable(false);
t2.setEchoChar('*');
pcen.setLayout(new GridLayout(5,4));
pcen.add(new Label());pcen.add(new Label());pcen.add(new Label());pcen.add(new Label());
pcen.add(new Label());pcen.add(new Label());pcen.add(new Label());pcen.add(new Label());
pcen.add(new Label());pcen.add(label2);pcen.add(t2);pcen.add(new Label());
pcen.add(new Label());pcen.add(new Label());pcen.add(new Label());pcen.add(new Label());
pcen.add(new Label());pcen.add(new Label());pcen.add(new Label());pcen.add(new Label());
psou.setLayout(new GridLayout(1,6));psou.add(new Label());psou.add(new Label());psou.add(b1);psou.add(b2);psou.add(new Label());psou.add(new Label());
add("Center",pcen);add("South",psou);
b1.addActionListener(this);b2.addActionListener(this);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{dispose();
}
});
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==b1)
{if(t2.getText().equals(Client.user_word)){t2.setText("");this.setVisible(false);Remember l_message=new Remember();}
else {wmessage wordcompare=new wmessage("密码确认"," 密码错误","wrong.jpg");wordcompare.setVisible(true);}
}
else if(e.getSource()==b2) {this.dispose();}
}
}
/****************************************聊天记录窗口****************************************************************/
class Remember extends JFrame implements TreeSelectionListener,ActionListener
{JTree tree=null;JTextArea text=new JTextArea("",20,20);int i=0;
DefaultMutableTreeNode root;JButton b_del=new JButton("删除聊天记录"),b_back=new JButton("返回");
DefaultMutableTreeNode month[]=new DefaultMutableTreeNode[13];
Remember()
{super("查看聊天记录");
Container con=getContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("日历记事本");
for(i=1;i<=12;i++)
{month[i]=new DefaultMutableTreeNode(""+i+"月");
root.add(month[i]);
}
for(i=1;i<=12;i++)
{ if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
{for(int j=1;j<=31;j++)
month[i].add(new DefaultMutableTreeNode(j+"日"));
}
else if(i==2||i==4||i==6||i==9||i==11)
{for(int j=1;j<=30;j++)
month[i].add(new DefaultMutableTreeNode(j+"日"));
}
}
tree=new JTree(root);
JPanel p=new JPanel();p.setLayout(new BorderLayout());
JScrollPane scrollpane_1=new JScrollPane(text);
p.add(scrollpane_1,BorderLayout.CENTER);
JPanel p_1=new JPanel();p_1.add(b_del);p_1.add(b_back);
p.add(p_1,BorderLayout.NORTH);
JScrollPane scrollpane_2=new JScrollPane(tree);
JSplitPane splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollpane_2,p);
tree.addTreeSelectionListener(this);
b_del.addActionListener(this);
b_back.addActionListener(this);
con.add(splitpane);
setVisible(true);setBounds(70,80,200,300);
pack();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==b_del)
{DefaultMutableTreeNode node =(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
String str=node.toString();
if(node.isLeaf())
{try
{File f=new File(Client.user_name+"/"+node.getParent().toString()+str+".txt");
boolean del=f.delete();
if(del)
{ wmessage wm_del=new wmessage("删除成功"," 您已经成功删除记录","提示.jpg");}
else {wmessage wm_del2=new wmessage("删除成功"," 正在删除记录","");}
}
catch(Exception e1){}
}
}
else if(e.getSource()==b_back) {this.dispose();}
}
public void valueChanged(TreeSelectionEvent e)
{text.setText("");int is_right=0;
if(e.getSource()==tree)
{DefaultMutableTreeNode node =(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(node.isLeaf())
{String str=node.toString();
for(int i=0;i<=12;i++)
{if(node.getParent()==month[i])
{try
{ try{
is_right=Client.hong.unMessagebrief(Client.user_name+"/"+node.getParent().toString()+str+".txt");
}
catch(Exception e1){};
if(is_right!=1)
{wmessage treemess=new wmessage(" 警告"," 您的记录被人修改过","wrong.jpg");treemess.setVisible(true);}
String temp=null;
File f=new File(Client.user_name+"/"+node.getParent().toString()+str+".txt");
FileReader file=new FileReader(f);
BufferedReader in=new BufferedReader(file);
while((temp=in.readLine())!=null)
text.append(temp+'\n');
file.close();in.close();
}
catch(FileNotFoundException e1){}
catch(IOException e1){}
}
}
}
}
}
}
/****************************************发信息窗口****************************************************************/
class sen
{
sen()
{}
public static void smess(String s,int i)
{byte buffer[]=s.trim().getBytes();
try{InetAddress address=InetAddress.getByName("localhost");
DatagramSocket mail_data=new DatagramSocket();
DatagramPacket data_pack=new DatagramPacket(buffer,buffer.length,address,i);
mail_data.send(data_pack);
}
catch(Exception e){}
}
}
class seesource extends Frame implements ActionListener
{TextField t1,t2,t3;TextArea ta1;
Label la1,la2,la3,la4;
Button b1,b2;URL ur;
int i=0;Imagecanvas seecan=new Imagecanvas("see.jpg");
seesource(int j)
{super("加密实现窗口");
setVisible(true);
setBounds(200,200,300,300);
i=j;
b1=new Button("查看源代码");b2=new Button("返回");
ta1=new TextArea();
t1=new TextField(20);t2=new TextField(20);t3=new TextField(20);
la1=new Label("请输入要加密的字符串,按回车键结束");la2=new Label("加密后的字符串如下,要解密请按回车键");la3=new Label("解密后字符串如下");la4=new Label("");
add(ta1);add(b1); add(b2); add(t1); add(t2); add(t3); add(la1);add(la2);add(la3);add(seecan);add(la4);
la1.setBounds(20,40,250,20); t1.setBounds(20,70,250,20);
la2.setBounds(20,100,250,20); t2.setBounds(20,130,250,20);
la3.setBounds(20,160,250,20); t3.setBounds(20,190,250,20);
b1.setBounds(40,240,80,25);b2.setBounds(150,240,50,25);
ta1.setBounds(10,270,380,200);seecan.setBounds(-40,-40,this.getSize().width+40,this.getSize().height+40);
ta1.setVisible(false);
b1.addActionListener(this); b2.addActionListener(this);
t1.addActionListener(this); t2.addActionListener(this);t3.addActionListener(this);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{dispose();
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==t1)
{if(this.i==1)
{try
{t2.setText(Client.hong.simpleness(6,t1.getText()));
}
catch(Exception e1){}
}
else if(this.i==2)
{try
{t2.setText(Client.hong.symmetry(t1.getText()));
}
catch(Exception e1){}
}
else if(this.i==3)
{try
{t2.setText(Client.hong.CBC(t1.getText()));
}
catch(Exception e1){}
}
else if(this.i==4)
{try
{t2.setText(Client.hong.RSA(t1.getText()));
}
catch(Exception e1){}
}
}
else if(e.getSource()==t2)
{if(this.i==1)
{try
{t3.setText(Client.dehong.de_simpleness(6,t2.getText()));}
catch(Exception e1){}
}
else if(this.i==2)
{try
{t3.setText(Client.dehong.de_symmetry(t2.getText()));}
catch(Exception e1){}
}
else if(this.i==3)
{try
{t3.setText(Client.dehong.de_CBC(t2.getText()));}
catch(Exception e1){}
}
else if(this.i==4)
{try
{t3.setText(Client.dehong.de_RSA(t2.getText()));}
catch(Exception e1){}
}}
else if(e.getSource()==b1)
{this.setBounds(200,50,400,500);ta1.setVisible(true);seecan.setBounds(-40,-40,this.getSize().width+40,this.getSize().height+40);
if(this.i==1)
{ta1.setText("/********** 凯撒加密**************/"+"\n"+"public String de_simpleness(int a,String s)throws Exception "+"\n"+"{String returnstr=new String('');"+"\n"+" for(int i=0;i<s.length();i++)"+"\n"+" {char c=s.charAt(i);"+"\n"+" c-=a;"+"\n"+" returnstr+=c;"+"\n"+" }"+"\n"+" return returnstr;"+"\n"+"}"+"\n"+"/************ 以下为解密***********/"+"\n"+"public String de_simpleness(int a,String s)throws Exception"+"\n"+" {String returnstr=new String('');"+"\n"+" for(int i=0;i<s.length();i++)"+"\n"+" {char c=s.charAt(i);"+"\n"+" c-=a;"+"\n"+" returnstr+=c;"+"\n"+" }"+"\n"+" return returnstr;"+"\n"+"}");
}
else if(this.i==2)
{ta1.setText(" /********** DES加密**************/"+"\n"+" public String symmetry(String s)throws Exception//DESede对称密码,密钥为key1.dat,针对数组的加密。输入字符串,密文输出为字符串,可用中文"+"\n"+" { "+"\n"+" FileInputStream f=new FileInputStream('key1.dat');"+"\n"+" ObjectInputStream b=new ObjectInputStream(f);"+"\n"+" Key k=(Key)b.readObject();"+"\n"+" Cipher cp=Cipher.getInstance('DESede');"+"\n"+" cp.init(Cipher.ENCRYPT_MODE,k);"+"\n"+" byte ptext[]=s.getBytes('UTF8');"+"\n"+" byte ctext[]=cp.doFinal(ptext);"+"\n"+" FileOutputStream f2=new FileOutputStream('DES.dat');"+"\n"+" f2.write(ctext);"+"\n"+" String returnstr=new String(ctext,'UTF8');"+"\n"+" return returnstr;"+"\n"+" }"+"\n"+"/************ 以下为解密***********/"+"\n"+"public static String de_symmetry(String s)throws Exception//输入字符串,密文输出为字符串"+"\n"+" { "+"\n"+" FileInputStream f=new FileInputStream('key1.dat');"+"\n"+" ObjectInputStream b=new ObjectInputStream(f);"+"\n"+" Key k=(Key)b.readObject();"+"\n"+" Cipher cp=Cipher.getInstance('DESede');"+"\n"+" cp.init(Cipher.DECRYPT_MODE,k);"+"\n"+" FileInputStream f2=new FileInputStream('DES.dat');"+"\n"+" int num=f2.available();"+"\n"+" byte[] ptext=new byte[num];"+"\n"+" f2.read(ptext);"+"\n"+" byte ctext[]=cp.doFinal(ptext);"+"\n"+" String returnstr=new String(ctext,'UTF8');"+"\n"+" return returnstr;"+"\n"+" }");}
else if(this.i==3)
{ta1.setText(" /********** CBC加密**************/"+"\n"+" public String CBC(String s)throws Exception//CBC方式加密,密钥为key1.dat,"+"\n"+" {"+"\n"+" FileInputStream f1=new FileInputStream('key1.dat');"+"\n"+" ObjectInputStream b1=new ObjectInputStream(f1);"+"\n"+" Key k=(Key)b1.readObject();"+"\n"+" byte[] rand=new byte[8];"+"\n"+" Random r=new Random();"+"\n"+" r.nextBytes(rand);"+"\n"+" IvParameterSpec iv=new IvParameterSpec(rand);"+"\n"+" Cipher cp=Cipher.getInstance('DESede/CBC/PKCS5Padding');"+"\n"+" cp.init(Cipher.ENCRYPT_MODE,k,iv);"+"\n"+" byte ptext[]=s.getBytes('UTF8');"+"\n"+" byte ctext[]=cp.doFinal(ptext);"+"\n"+" FileOutputStream f2=new FileOutputStream('CBC.dat');"+"\n"+" f2.write(rand);"+"\n"+" f2.write(ctext);"+"\n"+" byte[]tt=new byte[100];"+"\n"+" for(int i=0;i<rand.length;i++)"+"\n"+" tt[i]=rand[i];"+"\n"+" String returnstr=new String(ctext,'UTF8');"+"\n"+" System.out.println(returnstr);"+"\n"+" return returnstr;"+"\n"+"}"+"\n"+"/************ 以下为解密***********/"+"\n"+"public String de_CBC(String s) throws Exception"+"\n"+" {"+"\n"+" FileInputStream f1=new FileInputStream('key1.dat');"+"\n"+" ObjectInputStream b1=new ObjectInputStream(f1);"+"\n"+" Key k=(Key)b1.readObject();"+"\n"+" byte[] rand=new byte[8];"+"\n"+" FileInputStream f2=new FileInputStream('CBC.dat');"+"\n"+" f2.read(rand);"+"\n"+" IvParameterSpec iv=new IvParameterSpec(rand);"+"\n"+" int num=f2.available();"+"\n"+" byte[] ptext=new byte[num];"+"\n"+" f2.read(ptext);"+"\n"+" Cipher cp=Cipher.getInstance('DESede/CBC/PKCS5Padding');"+"\n"+" cp.init(Cipher.DECRYPT_MODE,k,iv);"+"\n"+" byte ctext[]=cp.doFinal(ptext);"+"\n"+" String returnstr=new String(ctext,'UTF8');"+"\n"+" System.out.println(returnstr);"+"\n"+" return returnstr;"+"\n"+"}");}
else if(this.i==4)
{ta1.setText(" /********** RSA加密**************/"+"\n"+"public String RSA(String s)throws Exception"+"\n"+" { String returnstr=new String('');"+"\n"+" FileInputStream Lfile=new FileInputStream('Skey_RSA_pub.dat');"+"\n"+" ObjectInputStream ob=new ObjectInputStream(Lfile);"+"\n"+" RSAPublicKey pbk=(RSAPublicKey)ob.readObject();"+"\n"+" BigInteger ee=pbk.getPublicExponent();"+"\n"+" BigInteger n=pbk.getModulus();"+"\n"+" byte ptext[]=s.getBytes('UTF8');"+"\n"+" BigInteger m=new BigInteger(ptext);"+"\n"+" BigInteger c=m.modPow(ee,n);"+"\n"+" returnstr=c.toString();"+"\n"+" return returnstr;"+"\n"+" }"+"\n"+"/************ 以下为解密***********/"+"\n"+"public String de_RSA(String s)throws Exception"+"\n"+" {"+"\n"+" BigInteger c=new BigInteger(s);"+"\n"+" FileInputStream Lfile=new FileInputStream('Skey_RSA_pri.dat');"+"\n"+" ObjectInputStream ob=new ObjectInputStream(Lfile);"+"\n"+" RSAPrivateKey pbk=(RSAPrivateKey)ob.readObject();"+"\n"+" BigInteger d=pbk.getPrivateExponent();"+"\n"+" BigInteger n=pbk.getModulus();"+"\n"+" BigInteger m=c.modPow(d,n);"+"\n"+" byte[]mt=m.toByteArray();"+"\n"+" String returnstr=new String(mt,'UTF8');"+"\n"+" return returnstr;"+"\n"+"}");}
}
else if(e.getSource()==b2)
{this.dispose();}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -