MD5
MD5:
1、压缩性
2、容易计算
3、抗修改性
4、弱抗碰撞
5、强抗碰撞
为什么需要MD5?
存储一些敏感信息的时候,如果不进行加密会出现安全问题。
例如:系统登录的密码,如果数据库中的密码采用明文,一旦数据库泄露,则用户的密码直接泄露。但是使用MD5加密算法之后即使数据泄露,也不会被轻易的破解,相对安全。
public class MD5Utils {
public static String getPWD( String strs ){
/*
* 加密需要使用JDK中提供的类
*/
StringBuffer sb = new StringBuffer();
try{
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] bs = digest.digest(strs.getBytes());
for (byte b : bs) {
int x = b & 255;
String s = Integer.toHexString(x);
if( x > 0 && x < 16 ){
sb.append("0");
sb.append(s);
}else{
sb.append(s);
}
}
}catch( Exception e){
System.out.println("加密失败");
}
return sb.toString();
}
}
原理说明:
MD5码以512位分组来处理输入的信息,且每一分组又被划分为16个32位子分组,经过了一系列的处理后,算法的输出由四个32位分组组成,将这四个32位分组级联后将生成一个128位散列值。
总体流程如下图所示,每次的运算都由前一轮的128位结果值和当前的512bit值进行运算
序列化
序列化其实就是:将一个对象转化为一种格式,能够更好的传输和电脑理解。
反序列化是转换过来,便于人们观看的。
public class Person implements serializable
{
private string name;
private int age;
public Person ( String name,int age) {
this.name = name ;
this.age = age;
);
public class write{
public static void main(String[] args) {
try (
//创建一个objectoutputStream输出流
objectoutputStream oos = new ObjectoutputStream(newFileOutputStream("object.txt")));
/将对象序列化到文件s
Person person = new Person ( "1",23);
oos.write( person ) ;
}catch (Exception e){
e.printStackTrace();
}
正则表达式:
正则表达式是一种字符串匹配一的模式
作业:
数据验证:比如电话号码、邮箱等
替换文本:快速找到特定文本,用于替换
快速提取字符串:基于匹配原则,快速从文档中查找或提取子字符串
比如简单实现一个身份证的正则表达式:
socket简单实现多个线程对一个服务器:
package login;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 20000);
new Thread(new ClientTread(socket)).start();
PrintStream ps = new PrintStream(socket.getOutputStream());
String line = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while ((line = br.readLine()) != null) {
ps.println(line);
}
if (br != null) br.close();
if (socket != null) socket.close();
}
}
package login;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ClientTread implements Runnable {
private final Socket socket;
BufferedReader br = null;
public ClientTread(Socket socket) throws IOException {
this.socket = socket;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void run() {
String content = null;
while (true) {
try {
if ((content = br.readLine()) == null) break;
} catch (IOException e) {
e.printStackTrace();
e.getCause();
}
System.out.println(content);
}
}
}
package socket;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Server {
public static List<Socket> socketList = Collections.synchronizedList(new ArrayList<>());
public static void main(String[] args) throws IOException {
List<Socket> socketList = new ArrayList<>();
ServerSocket serverSocket = new ServerSocket(20000);
try {
while (true) {
Socket socket1 = serverSocket.accept();
socketList.add(socket1);
new Thread(new ServerThread(socket1)).start();
}
} catch (Exception e) {
e.printStackTrace();
socketList.removeAll(socketList);
}
package socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class ServerThread implements Runnable {
Socket socket = null;
BufferedReader br = null;
public ServerThread(Socket socket) throws Exception {
this.socket = socket;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void run() {
String content = null;
while ((content = readFormClient()) != null) {
for (Socket socket : Server.socketList) {
try {
PrintStream ps = new PrintStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
e.getCause();
}
}
}
}
private String readFormClient() {
try {
return br.readLine();
} catch (IOException e) {
Server.socketList.remove(socket);
}
return null;
}
}