⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kserver.java

📁 客户端与服务器的加密验证,用vc开发,只是一个例子,只作参考.
💻 JAVA
字号:

import org.ietf.jgss.*;
import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;


public class KServer  {
    
    public static void main(String[] args) 
	throws IOException, GSSException {

	// Obtain the command-line arguments and parse the port number

	if (args.length != 1) {
	    System.err.println("Usage: java <options> Login KServer <localPort>");
	    System.exit(-1);
	}

	int localPort = Integer.parseInt(args[0]);

	ServerSocket ss = new ServerSocket(localPort);

	GSSManager manager = GSSManager.getInstance();

	while (true) {

	    System.out.println("Waiting for incoming connection...");

	    Socket socket = ss.accept();
	    DataInputStream inStream =
		new DataInputStream(socket.getInputStream());
	    DataOutputStream outStream = 
		new DataOutputStream(socket.getOutputStream());

	    System.out.println("Got connection from client "
			       + socket.getInetAddress());
	
	    GSSContext context = manager.createContext((GSSCredential)null);

	    	    
	    byte[] token = null;
	    
	    while (!context.isEstablished()) {
		
		token = new byte[inStream.readInt()];
		System.out.println("Will read input token of size "
				   + token.length
				   + " for processing by acceptSecContext");
		inStream.readFully(token);
		
		token = context.acceptSecContext(token, 0, token.length);
		
		
		if (token != null) {
		    System.out.println("Will send token of size "
				       + token.length
				       + " from acceptSecContext.");
		    outStream.writeInt(token.length);
		    outStream.write(token);
		    outStream.flush();
		}
	    }
	    
	    System.out.print("Context Established! ");
	    System.out.println("Client is " + context.getSrcName());
	    System.out.println("Server is " + context.getTargName());
	    
	    if (context.getMutualAuthState())
		System.out.println("Mutual authentication took place!");
	    
	    
	    MessageProp prop = new MessageProp(0, false);

	     for(;;)
	     {
	    	 token = new byte[inStream.readInt()];
	    	 System.out.println("Will read token of size " 
			       + token.length);
	    	 inStream.readFully(token);
	    
	    	 byte[] bytes = context.unwrap(token, 0, token.length, prop);
	    	 String str = new String(bytes);
	    	 System.out.println("Received data \"" 
				       + str + "\" of length " + str.length());
	    
	   	 System.out.println("Confidentiality applied: "
				       + prop.getPrivacy());
	
		    prop.setQOP(0);
	    
		    token = context.getMIC(bytes, 0, bytes.length, prop);
	    
		    System.out.println("Will send MIC token of size " 
			       + token.length);
		    outStream.writeInt(token.length);
		    outStream.write(token);
		    outStream.flush();
		    
		    if(str.equals("quit"))
		    {
		    	break;
		     }
		}

	    System.out.println("Closing connection with client " 
			       + socket.getInetAddress());
	    context.dispose();
	    socket.close();
	}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -