button.addActionListener(this); button_1.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub m=textField.getText(); k=textField_1.getText(); s=textField_2.getText(); c=\;
Caesar cae=new Caesar();
WordTable wtable=new WordTable(); if (e.getSource()==button) { //加密
c+=cae.Caeplus(m,
Integer.parseInt(k),Integer.parseInt(s),wtable); JOptionPane.showConfirmDialog(this,c,\
\,JOptionPane.CLOSED_OPTION); Client client=new Client(); client.run(c,k,s);
} else if (e.getSource() ==button_1) { //解密
c+=cae.Caesub(m,
Integer.parseInt(k),Integer.parseInt(s),wtable); JOptionPane.showConfirmDialog(this,c,\\,JOptionPane.CLOSED_OPTION);
}
}
}
(3)字符表类WordTable
class KeyTable{ char key; int code;
public KeyTable(char key,int code){ this.key=key;
this.code=code;
}
}
public class WordTable { int length=0;
KeyTable table[]=new KeyTable[93]; public WordTable(){
table[0]=new KeyTable('!',1); table[1]=new KeyTable('#',2);
table[2]=new KeyTable('$',3); 密
文
明
文
table[3]=new KeyTable('%',4); table[4]=new KeyTable('&',5); table[5]=new KeyTable('\\'',6); table[6]=new KeyTable('(',7); table[7]=new KeyTable(')',8); table[8]=new KeyTable('*',9); table[9]=new KeyTable('+',10); table[10]=new KeyTable(',',11); table[11]=new KeyTable('-',12); table[12]=new KeyTable('.',13); table[13]=new KeyTable('/',14); table[14]=new KeyTable('0',15); table[15]=new KeyTable('1',16); table[16]=new KeyTable('2',17); table[17]=new KeyTable('3',18); table[18]=new KeyTable('4',19); table[19]=new KeyTable('5',20); table[20]=new KeyTable('6',21); table[21]=new KeyTable('7',22); table[22]=new KeyTable('8',23); table[23]=new KeyTable('9',24); table[24]=new KeyTable(':',25); table[25]=new KeyTable(';',26); table[26]=new KeyTable('<',27); table[27]=new KeyTable('=',28); table[28]=new KeyTable('>',29); table[29]=new KeyTable('?',30); table[30]=new KeyTable('@',31); table[31]=new KeyTable('A',32); table[32]=new KeyTable('B',33); table[33]=new KeyTable('C',34); table[34]=new KeyTable('D',35); table[35]=new KeyTable('E',36); table[36]=new KeyTable('F',37); table[37]=new KeyTable('G',38); table[38]=new KeyTable('H',39); table[39]=new KeyTable('I',40); table[40]=new KeyTable('J',41); table[41]=new KeyTable('K',42); table[42]=new KeyTable('L',43); table[43]=new KeyTable('M',44); table[44]=new KeyTable('N',45); table[45]=new KeyTable('O',46); table[46]=new KeyTable('P',47);
table[47]=new KeyTable('Q',48); table[48]=new KeyTable('R',49); table[49]=new KeyTable('S',50); table[50]=new KeyTable('U',51); table[51]=new KeyTable('V',52); table[52]=new KeyTable('W',53); table[53]=new KeyTable('X',54); table[54]=new KeyTable('Y',55); table[55]=new KeyTable('Z',56); table[56]=new KeyTable('[',57); table[57]=new KeyTable('\\\\',58); table[58]=new KeyTable(']',59); table[59]=new KeyTable('^',60); table[60]=new KeyTable('_',61); table[61]=new KeyTable('`',62); table[62]=new KeyTable('a',63); table[63]=new KeyTable('b',64); table[64]=new KeyTable('c',65); table[65]=new KeyTable('d',66); table[66]=new KeyTable('e',67); table[67]=new KeyTable('f',68); table[68]=new KeyTable('g',69); table[69]=new KeyTable('h',70); table[70]=new KeyTable('i',71); table[71]=new KeyTable('j',72); table[72]=new KeyTable('k',73); table[73]=new KeyTable('l',74); table[74]=new KeyTable('m',75); table[75]=new KeyTable('n',76); table[76]=new KeyTable('o',77); table[77]=new KeyTable('p',78); table[78]=new KeyTable('q',79); table[79]=new KeyTable('r',80); table[80]=new KeyTable('s',81); table[81]=new KeyTable('t',82); table[82]=new KeyTable('u',83); table[83]=new KeyTable('v',84); table[84]=new KeyTable('w',85); table[85]=new KeyTable('x',86); table[86]=new KeyTable('y',87); table[87]=new KeyTable('z',88); table[88]=new KeyTable('{',89); table[89]=new KeyTable('|',90); table[90]=new KeyTable('}',91);
}
table[91]=new KeyTable('~',92); table[92]=new KeyTable(' ',93); length=table.length;
}
(4)客户类Client
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket;
public class Client {
Socket mysocket1= null,mysocket2= null; InputStreamReader in1= null;
OutputStreamWriter out1= null,out2 =null; try{
mysocket1 = new Socket(\,1895); mysocket2 = new Socket(\,1896); in1 out1
=
new =new
public void run(String mc,String mk,String ms) {
// TODO Auto-generated method stub
InputStreamReader(mysocket1.getInputStream()); OutputStreamWriter(mysocket1.getOutputStream());
BufferedWriter bout1 = new BufferedWriter(out1); BufferedReader bin1 = new BufferedReader(in1); out2
=new
OutputStreamWriter(mysocket2.getOutputStream());
BufferedWriter bout2 = new BufferedWriter(out2); while(true){
bout1.write(mc);
System.out.println(\密文发送成功\); bout1.newLine(); bout1.flush(); bout2.write(mk);
System.out.println(\密钥发送成功\); bout2.newLine(); bout2.flush(); bout2.write(ms);
System.out.println(\起始编码发送成功\);
}
}
}
}
bout2.newLine(); bout2.flush(); String temp = null; temp = bin1.readLine();
System.out.println(\消息状态:\+temp); bout1.write(\);
System.out.println(\); bout1.newLine(); bout1.flush();
temp = bin1.readLine(); if(temp.equals(\!\)){ System.out.println(temp); }
break;
mysocket1.close(); mysocket2.close();
catch(Exception e){ }
System.out.println(e);
(5)服务器类Server
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class Server {
public void decode(String mc,String mk,String ms){ }
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerSocket server1= null,server2 = null; Socket you1= null,you2 = null;
Caesarframe frame = new Caesarframe(); frame.setVisible(true); frame.textField.setText(mc); frame.textField_1.setText(mk); frame.textField_2.setText(ms);