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

📄 node_b_1.java

📁 1、利用公钥密码技术实现对称密码技术密钥的分配; 2、利用所分配的对称密钥对通信内容进行加/解密;
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class Node_b_1 
{
	public Node_b_1()
	{}
	
//	从磁盘文件读取公钥
	public PublicKey getPublicKey( String path)
	{
		PublicKey key = null; 
		try 
		{
			System.out.println("读取对方的公钥...");
			ObjectInputStream in = 
				new ObjectInputStream(new FileInputStream(path));
		    key = (PublicKey)in.readObject();
		    in.close();	
		    System.out.println("读取对方的公钥结束!");
		} 
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	    
	    return key;
	}
	//从磁盘文件读取私钥
	public PrivateKey getPrivateKey(String path)
	{
		PrivateKey key = null; 
		try 
		{
			System.out.println("读取自己的私钥...");
			ObjectInputStream in = 
				new ObjectInputStream(new FileInputStream(path));
		    key = (PrivateKey)in.readObject();
		    in.close();
		    System.out.println("读取自己的私钥结束!");
		} 
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	    
	    return key;
	}
	
	public void ReceiveFile( PublicKey pubKey, PrivateKey priKey )
	{ 
		Socket s =null;
		ServerSocket ss =null; 
		String fileName = "d:\\team28\\2.txt";
		
		try
		{
			ss =new ServerSocket(8001);
			
			System.out.println("服务器端已经就绪...");
		    while(true)
		    {
		    	//接受客户端的请求,没有请求则阻塞
		    	s = ss.accept();   
		    	//以重新启动一个线程的方式,取得客户端发送的文件
		    	new Thread(new Servicer1(s,fileName,pubKey,priKey)).start();
		    }
		    
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				s.close();
			}
			catch(IOException ex)
			{
				ex.printStackTrace();
			}
		}  
	}
	
	public static void main(String args[])
	{
		String pubKeyPath;
		String priKeyPath;
		PublicKey pubKey = null;
		PrivateKey priKey = null;
		
		//从命令行传递2个参数--对方公钥的路径和自己私钥的路径
		if(args.length!=2)
		{
			System.out.println("usage: java MyClient Path1 Path2");
			return;
		}
		pubKeyPath = args[0];
		priKeyPath = args[1];
			
		Node_b_1 fileServer = new Node_b_1();
		
		//获取对方的公钥
		pubKey = fileServer.getPublicKey(pubKeyPath);
		//获取本机私钥
		priKey = fileServer.getPrivateKey(priKeyPath);
		
		fileServer.ReceiveFile( pubKey, priKey );
	}
}

class Servicer1 implements Runnable
{
	Socket server =null;
	String fileName =null;
	PublicKey pubKey = null;
	PrivateKey priKey = null;
	
	//传递文件名和Socket
	Servicer1(Socket s,String fileName,PublicKey pubKey, PrivateKey priKey)
	{
	    this.server = s;
	    this.fileName = fileName;
	    this.priKey = priKey;
	    this.pubKey = pubKey;
	}
	
	public void run()
	{
		DataInputStream dis =null;
		FileOutputStream fos =null;
		
		//创建Socket的输入输出流
	    try
	    {
	    	InputStream ips =server.getInputStream();
	        BufferedInputStream br = new BufferedInputStream(ips);
	        
	        //以DataInputStream来包装字节缓冲输入流
	        dis = new DataInputStream(br);
	        //文件输出流
	        fos = new FileOutputStream(fileName);
	       
	        //网络传输都是以字节的方式传递的
	        byte[] buf = new byte[4096];
	        
	        System.out.println("服务器端开始接收...");
	        
	        int count; 
	        count = dis.read(buf);
	        if(count<0)
	        	count = 0;
	        byte[] temp = new byte[count];
		    for(int i = 0; i < temp.length; i++)
		    {
		    	temp[i] = buf[i];
		    }
		    
	        //调用解密过程
			byte priInfo[] = new byte[0];
	        priInfo = decryptFile(temp,pubKey,priKey);
	        
	        fos.write(priInfo);
    
	        System.out.println("服务器端接收完毕");
	        System.out.println("接受到的文件保存在本地:" + fileName);
	    }
	    catch(FileNotFoundException fe)
	    {
	       fe.printStackTrace();
	    }
	    catch(Exception e)
	    {
	    	e.printStackTrace();
	    }
	    finally
	    {
	    	try
	    	{
	    		fos.close();
	    		dis.close();
	    	}
	    	catch(IOException ex)
	    	{}
	     }
	}
	
	public byte[] decryptFile(byte buf[],PublicKey pubKey, PrivateKey priKey) throws Exception
	{
		Cipher cipher = null;
		byte[]decrypted1 = new byte[0];
		byte[]decrypted2 = new byte[0];
		byte[] temp = new byte[128];
		
	    cipher = Cipher.getInstance("RSA");
	    
	    //用自己的私钥进行解密
	    System.out.println("用自己的私钥进行解密...");
	    cipher.init(Cipher.DECRYPT_MODE,priKey);
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    for(int len = buf.length, i = 0; len > 0; len -= 128,i++)
	    {
	    	if(len>128)
	    		temp = cipher.doFinal(buf,128*i,128);
	    	else
	    		temp = cipher.doFinal(buf,128*i,len);
	    	baos.write(temp);
	    }
		decrypted1 = baos.toByteArray();

		//用对方的公钥进行解密
		System.out.println("用对方的公钥进行解密...");
		cipher.init(Cipher.DECRYPT_MODE,pubKey);
		baos.reset();
		for(int len = decrypted1.length, i = 0; len > 0; len -= 128,i++)
	    {
	    	if(len>128)
	    		temp = cipher.doFinal(decrypted1,128*i,128);
	    	else
	    		temp = cipher.doFinal(decrypted1,128*i,len);
	    	baos.write(temp);
	    }
		decrypted2 = baos.toByteArray();
		//关闭baos
		baos.close();
  
		return decrypted2;
	}

}

⌨️ 快捷键说明

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