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

📄 node_a_1.java

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

public class Node_a_1 
{
	public Node_a_1()
	{}

	//创建一个对称密钥,保存到本地磁盘
	public void generateDESedeKey()
	{
		//产生对称密钥
		KeyGenerator keygen = null;
		SecretKey secretKey = null;
		try 
		{
			System.out.println("产生对称密钥...");
			keygen = KeyGenerator.getInstance("DESede");
			keygen.init(168);	  
			
			secretKey = keygen.generateKey();
			System.out.println("产生对称密钥结束!");
		}
		catch (NoSuchAlgorithmException e)
		{
			e.printStackTrace();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}

		//把密钥写到磁盘
		try 
		{
			ObjectOutputStream out =
				new ObjectOutputStream(new FileOutputStream("d:\\team28\\1.txt"));
			out.writeObject(secretKey);
			out.close();
			System.out.println("密钥保存到: " + "d:\team28\1.txt"); 
		} 
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	//从磁盘文件读取公钥
	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 SendSemKey( 
			String ip, PublicKey pubkey, PrivateKey prikey )
	{
		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("d:\\team28\\1.txt");
		    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); 
		    if(count<0)
		    	count = 0;
		    
		    byte[] temp = new byte[count];
		    for(int i = 0; i < temp.length; i++)
		    {
		    	temp[i] = buf[i];
		    }
		    
		    //调用加密过程
		    byte[] encryptedBuf = new byte[4096];
		    encryptedBuf = encryptFile(temp,prikey,pubkey);
		    //传输过程
		    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[],Key prikey, Key pubkey) throws Exception
	{
		Cipher cipher = null;
		byte[] temp = new byte[117];
	    byte[] encrypted1 = new byte[0];
	    byte[] encrypted2 = new byte[0];
	    cipher = Cipher.getInstance("RSA");
	    
	    //利用自己的私钥进行加密
	    System.out.println("利用自己的私钥进行加密...");
	    cipher.init(Cipher.ENCRYPT_MODE,prikey);
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    for(int len = buf.length, i = 0; len > 0; len -= 117,i++)
	    {
	    	if(len>117)
	    		temp = cipher.doFinal(buf,117*i,117);
	    	else
	    		temp = cipher.doFinal(buf,117*i,len);
	    	baos.write(temp);
	    }
		encrypted1 = baos.toByteArray();
		
		//利用对方的公钥进行加密
		System.out.println("利用对方的公钥进行加密...");
	    cipher.init(Cipher.ENCRYPT_MODE,pubkey);
	    baos.reset();
	    for(int len = encrypted1.length, i = 0; len > 0; len -= 117,i++)
	    {
	    	if(len>117)
	    		temp = cipher.doFinal(encrypted1,117*i,117);
	    	else
	    		temp = cipher.doFinal(encrypted1,117*i,len);
	    	baos.write(temp);
	    }
		encrypted2 = baos.toByteArray();
		
		//关闭baos
		baos.close();   
		System.out.println("加密结束!");
		return encrypted2;	
	}
	
	public static void main(String[] args)
	{
		String ip;
		String pubKeyPath;
		String priKeyPath;
		PublicKey pubKey = null;
		PrivateKey priKey = null;
		
		//从命令行传递3个参数--服务器IP地址、公钥的路径和私钥的路径
		if(args.length!=3)
		{
			System.out.println("usage: java MyClient ServerIP Path1 Path2");
			return;
		}
		ip = args[0];
		pubKeyPath = args[1];
		priKeyPath = args[2];
			
		Node_a_1 fileClient = new Node_a_1();
		
		//产生对称密钥
		fileClient.generateDESedeKey();
		//获取对方的公钥
		pubKey = fileClient.getPublicKey(pubKeyPath);
		//获取本机私钥
		priKey = fileClient.getPrivateKey(priKeyPath);
		
		fileClient.SendSemKey(ip,pubKey,priKey);		
	}
}

⌨️ 快捷键说明

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