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

📄 node_a_2.java

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

public class Node_a_2
{
	public Node_a_2()
	{}

	//从磁盘文件读取对称密钥
	public SecretKey getSemKey()
	{
		SecretKey key = null; 
		try 
		{
			ObjectInputStream in = 
				new ObjectInputStream(new FileInputStream("d:\\team28\\1.txt"));
		    key = (SecretKey)in.readObject();
		    in.close();
		} 
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	    
	    return key;
	}
	
	public void SendEncryptedFile( String ip, String fileName, SecretKey key )
	{
		FileInputStream fis = null;
		DataOutputStream dos = null;
		Socket socket = null;
		String port = "8001";
		
		try
		{
			socket = new Socket(InetAddress.getByName(ip),Integer.parseInt(port));
			System.out.println("客户端开始传输...");
			
		    //创建Socket的输入输出流
		    OutputStream ops = socket.getOutputStream();
		    
		    fis = new FileInputStream(fileName);
		    
		    if(fis.available()>4000)
		    {
		    	System.out.println("文件太大!");
		    	return;
		    }
		    
		    dos = new DataOutputStream(new BufferedOutputStream(ops));
		    
		    //读取文件的字节流
		    byte[] buf = new byte[4000];
		    
		    int count;
		    count = fis.read(buf); 
		    
		    byte[] temp = new byte[count];
		    for(int i = 0; i < temp.length; i++)
		    {
		    	temp[i] = buf[i];
		    }
		    
		    //调用加密过程
		    byte[] encryptedBuf = new byte[0];
		    encryptedBuf = encryptFile(temp,key);
		    //传输过程
		    dos.write(encryptedBuf);
		
		    System.out.println("客户端传输完毕");
	    }
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				fis.close();
				dos.close();
				socket.close();
		    }
			catch(IOException ex){}
		}
	}
	
	public byte[] encryptFile(byte buf[],SecretKey key)
	{
		Cipher cipher = null;
	    byte[] encrypted = new byte[0];
	    try 
	    {
	    	cipher = Cipher.getInstance("DESede");
	        cipher.init(Cipher.ENCRYPT_MODE,key);
	        encrypted = cipher.doFinal(buf);
	    } 
	    catch (NoSuchAlgorithmException e)
	    {
	    	e.printStackTrace();
	    } 
	    catch (NoSuchPaddingException e)
	    {
	    	e.printStackTrace();
	    } 
	    catch (InvalidKeyException e) 
	    {
	    	e.printStackTrace();
	    } 
	    catch (IllegalBlockSizeException e)
	    {
	        e.printStackTrace();
	    }
	    catch (BadPaddingException e) 
	    {
	    	e.printStackTrace();
	    }
	   // System.out.println("end:" + encrypted.length);    
		return encrypted;
		

		
	}
	
	public static void main(String[] args)
	{	
		Node_a_2 fileClient = new Node_a_2();
		
		SecretKey secretkey = null;
		
		//从命令行传递2个参数--服务器IP地址和传送的文件名
		if(args.length<2)
		{
			System.out.println("usage: java MyClient ServerIP FileName");
			return;
		}
		else if(args.length == 2)
		{		
			secretkey = fileClient.getSemKey();
			//调用发送文件方法,参数为目标机器的IP
			fileClient.SendEncryptedFile(args[0], args[1], secretkey);
		}
	}

}

⌨️ 快捷键说明

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