团队冲刺DAY6

今天的内容是无图形界面的客户端和服务器的加密解密系统。

通信时用的socket方法,内置的密钥,端口,ip地址.

客户端:

import java.io.*;
import java.net.*;
import java.nio.charset.Charset;
import java.util.*;
public class Client {
private static final String SKEY = "abcdefgh";
private static final Charset CHARSET = Charset.forName("gb2312");
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
Socket mysocket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread readData ;
Read read=null;
try{ mysocket=new Socket();
read = new Read();
readData = new Thread(read);
String IP = "127.0.0.1";
int port = 2010;
if(mysocket.isConnected()){}
else{
InetAddress address=InetAddress.getByName(IP);
InetSocketAddress socketAddress=new InetSocketAddress(address,port);
mysocket.connect(socketAddress);
in =new DataInputStream(mysocket.getInputStream());
out = new DataOutputStream(mysocket.getOutputStream());
read.setDataInputStream(in);
readData.start();
}
}
catch(Exception e) {
System.out.println("服务器已断开"+e);
}
System.out.println("已经成功连接服务器,开始通讯!");
System.out.print("输入:");
while(scanner.hasNext()) {
Macq S = new Macq();
String s = "";
String st = "";
try {
s = scanner.nextLine();
}
catch(InputMismatchException exp){
System.exit(0);
}
if(s.equals("end!")){
System.exit(0);
}
s = DesUtil.encrypt(s, CHARSET, SKEY);
st = S.mac(s);
try {
out.writeUTF(s);
out.writeUTF(st);
}
catch(Exception e) {}
}
}
}

服务器:

import java.io.*;
import java.nio.charset.Charset; public class Read implements Runnable {
private static final String SKEY = "abcdefgh";
private static final Charset CHARSET = Charset.forName("gb2312");
DataInputStream in;
public void setDataInputStream(DataInputStream in) {
this.in = in;
}
@Override
public void run() {
Macq S = new Macq();
String s = "";
String mactext = "";
String mactest = "";
while(true) {
try{
s = in.readUTF();
mactext = in.readUTF();
System.out.println("服务器回复:"+s);
System.out.println("接收到的mac:"+mactext);
try{
mactest =S.mac(s);
System.out.println("本地检验的mac:"+mactext);
if(mactest.equals(mactext)==true){
throw new java.io.IOException("没有通过mac检验");
}
}catch (Exception e2){
e2.printStackTrace();
}
try {
s = DesUtil.decrypt(s, CHARSET, SKEY);
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("解密为:"+s);
System.out.print("输入:");
}
catch(IOException e) {
System.out.println("与服务器已断开!"+e);
break;
}
}
}
}

因为不是最终稿,所以并没有用更难组合的用两个线程的客户端和服务器的DAY1代码,而是用的书上的例子,来增加部分代码的方法先练练手。

最新文章

  1. 解决 PowerDesigner 错误 The generation has been cancelled…
  2. poj 1276 Cash Machine(多重背包)
  3. HTTP 传输内容的压缩
  4. 初学Struts2-自定义拦截器及其配置
  5. C++中的一些定义
  6. linux64位系统中g++4.4.7下使用wcsncpy函数有造成段错误的BUG(其它g++版本号未知)
  7. Java设计模式:代理模式(一)
  8. mac 删除文件夹里所有的.svn文件
  9. 傻瓜学编程之block_1
  10. Unity3d项目入门之虚拟摇杆
  11. composer 安装thinkphp
  12. git 入门教程之实战 git
  13. Windows 配置nginx服务器 运行php项目
  14. maya2016卸载/安装失败/如何彻底卸载清除干净maya2016注册表和文件的方法
  15. Kerberos 互信免登陆
  16. boost-智能指针
  17. [转帖]第二个显示屏上禁用Windows任务栏
  18. 使用sqoop从Oracle或mysql抽取数据到HDFS遇到的报错及解决
  19. 【AppScan】入门工作原理详解
  20. MFC文档应用程序CToolBar:设置两个工具条并列停靠到同一条边上 转

热门文章

  1. Linux后台执行脚本 &与nohup
  2. nRF51822 之 Interrupt
  3. 编译-构建Shell语法的语法树(parse tree)
  4. 图解HTTP 读书笔记
  5. HDU 5183 Negative and Positive (NP) (手写哈希)
  6. 科普:std::sort干了什么
  7. Gentoo 搭遗
  8. vue中修改了数据但视图无法更新的情况(转)
  9. ---搭建springMvc框架,希望对初学者有所参考
  10. 通过yum在CentOS7部署LNMP环境(Centos7.4+Nginx1.12+mariadb5.5.56+PHP7.0)