arrB[i]=arrBTmp[i];
}
//生成密钥
Keykey=newjavax.crypto.spec.SecretKeySpec(arrB,"DES");
returnkey;
}
}
服务器程序:
publicclassChatServerJFrame_TCPextendsJFrameimplementsActionListener{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
privateDESPlusdes=null;
ServerSocketserSkt;
SocketscSkt;
DataInputStreamdin;
DataOutputStreamdout;
JTextAreamsg;
JTextFieldtxt_str_send;
JButtonsendBtn,exitBtn;
ChatServerJFrame_TCP(){
setTitle("聊天室-服务器");
msg=newJTextArea(100,250);
msg.setBackground(Color.white);
msg.setEditable(false);
add(msg);//存放双方聊天记录的文本区
JLabelsend_lbl=newJLabel("请输入要发送的消息");
txt_str_send=newJTextField(20);
txt_str_send.addActionListener(this);//发送内容文本框
sendBtn=newJButton("发送");
sendBtn.addActionListener(this);//发送按钮
exitBtn=newJButton("退出");
exitBtn.addActionListener(this);//退出按钮
JPaneljp=newJPanel();
jp.add(send_lbl);
jp.add(txt_str_send);
jp.add(sendBtn);
jp.add(exitBtn);
add(jp,BorderLayout.SOUTH);
setSize(500,400);
//setLocation(400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
serSkt=newServerSocket(6600);//在6600端口监听
scSkt=serSkt.accept();
din=newDataInputStream(scSkt.getInputStream());
dout=newDataOutputStream(scSkt.getOutputStream());
//返回socket的数据输入流和输出流
}catch(IOExceptione){
e.printStackTrace();
}
}
publicvoidsendMessage()throwsException{//发送方法
try{
StringstrToClient=txt_str_send.getText().trim();
//提取文本框输入内容
if(strToClient.equals("")){//发送内容不能为空
JOptionPane.showMessageDialog(null,"发送内容不能为空");
txt_str_send.requestFocus();
return;
}else{//将内容发送给客户机,同时显示在聊天记录文本区中
msg.setForeground(Color.red);
msg.append("服务器说:
"+strToClient+"\n");
des=newDESPlus();
strToClient=des.encrypt(strToClient);
dout.writeUTF(strToClient);
txt_str_send.setText("");
}
}catch(IOExceptionie1){
ie1.printStackTrace();
}
}
publicvoidreceiveMessage(){//接受信息方法
try{
while(true){//可以不断地接收对方发送过来的消息
StringstrFromClient=din.readUTF();
System.out.println("客户端发给服务器的密文:
"+strFromClient);
/*try{
des=newDESPlus();
strFromClient=des.decrypt(strFromClient);
}catch(Exceptionex){
ex.printStackTrace();
}*/
msg.setForeground(Color.red);
msg.append("客户端说:
"+strFromClient+"\n");
}
}catch(IOExceptionie2){
msg.append("您的聊天客户已经离开了!
"+"\n");
}finally{
try{
din.close();
dout.close();
scSkt.close();
}catch(IOExceptionex){
ex.printStackTrace();
}
System.exit(0);
}
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==txt_str_send){
try{
sendMessage();
}catch(Exceptionex){
ex.printStackTrace();
}
}elseif(e.getSource()==sendBtn){
try{
sendMessage();
}catch(Exceptionex){
ex.printStackTrace();
}
}elseif(e.getSource()==exitBtn){
System.exit(0);
}
}
publicstaticvoidmain(String[]args){
ChatServerJFrame_TCPchatServer=newChatServerJFrame_TCP();
chatServer.receiveMessage();//调用接收消息方法
}
}
客户端程序:
publicclassChatClientJFrame_TCPextendsJFrameimplementsActionListener{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
privateDESPlusdes=null;
ServerSocketserSkt;
SocketscSkt;
DataInputStreamdin;
DataOutputStreamdout;
JTextAreamsg;
JTextFieldtxt_str_send;
JButtonsendBtn,exitBtn;
Stringhost="localhost";
intport=6600;
ChatClientJFrame_TCP(){
setTitle("聊天室-客户端");
msg=newJTextArea(100,250);
msg.setBackground(Color.white);
msg.setEditable(false);
add(msg);//存放双方聊天记录的文本区
JLabelsend_lbl=newJLabel("请输入要发送的消息");
txt_str_send=newJTextField(20);
txt_str_send.addActionListener(this);//发送内容文本框
sendBtn=newJButton("发送");
sendBtn.addActionListener(this);//发送按钮
exitBtn=newJButton("退出");
exitBtn.addActionListener(this);//退出按钮
JPaneljp=newJPanel();
jp.add(send_lbl);
jp.add(txt_str_send);
jp.add(sendBtn);
jp.add(exitBtn);
add(jp,BorderLayout.SOUTH);
setSize(500,400);
//setLocation(400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
scSkt=newSocket(host,port);
din=newDataInputStream(scSkt.getInputStream());
dout=newDataOutputStream(scSkt.getOutputStream());
//返回socket的数据输入流和输出流
}catch(IOExceptione){
e.printStackTrace();
}
}
publicvoidsendMessage(){//发送方法
try{
StringstrToClient=txt_str_send.getText().trim();
//提取文本框输入内容
if(strToClient.equals("")){//发送内容不能为空
JOptionPane.showMessageDialog(null,"发送内容不能为空");
txt_str_send.requestFocus();
return;
}else{//将内容发送给客户机,同时显示在聊天记录文本区中
msg.setForeground(Color.red);
msg.append("客户端说:
"+strToClient+"\n");
try{
des=newDESPlus();
strToClient=des.encrypt(strToClient);
}catch(Exceptionex){
ex.printStackTrace();
}
dout.writeUTF(strToClient);
txt_str_send.setText("");
}
}catch(IOExceptionie1){
ie1.printStackTrace();
}
}
publicvoidreceiveMessage(){//接受信息方法
try{
while(true){//可以不断地接收对方发送过来的消息
StringstrFromClient=din.readUTF();
System.out.println("服务器发给客户端的密文:
"+strFromClient);
try{
des=newDESPlus();
strFromClient=des.decrypt(strFromClient);
}catch(Exceptionex){
ex.printStackTrace();
}
msg.setForeground(Color.red);
msg.append("服务器说:
"+strFromClient+"\n");
}
}catch(IOExceptionie2){
msg.append("服务器选择结束和您的本次聊天!
"+"\n");
}finally{
try{
din.close();
dout.close();
scSkt.close();
}catch(IOExceptionex){
ex.printStackTrace();
}
System.exit(0);
}
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==txt_str_send){
sendMessage();
}elseif(e.getSource()==sendBtn){
sendMessage();
}elseif(e.getSource()==exitBtn){
System.exit(0);
}
}
publicstaticvoidmain(String[]args){
ChatClientJFrame_TCPchatServer=newChatClientJFrame_TCP();
chatServer.receiveMessage();