www.pudn.com > upload.rar > client.java


import java.io.*; 
import java.security.*; 
import javax.crypto.*; 
import javax.crypto.spec.*; 
import java.net.*; 
public class client{ 
  public static void main(String args[])throws Exception{ 
   KeyGenerator kg=KeyGenerator.getInstance("DESede");      //获取密匙生成器 
    kg.init(168); 
    SecretKey k=kg.generateKey();                            //生成密匙 
    FileOutputStream f_des=new FileOutputStream("c:/deskey.dat"); 
    ObjectOutputStream o=new ObjectOutputStream(f_des); 
    o.writeObject(k); 
    Cipher cp=Cipher.getInstance("DESede");  
	  cp.init(Cipher.ENCRYPT_MODE,k); 
	  FileInputStream fin=new FileInputStream("c:/temp.txt"); 
    FileOutputStream fout=new FileOutputStream("c:/desencryp.txt"); 
	  CipherOutputStream cout=new CipherOutputStream(fout,cp); 
	  int b=0; 
	  while((b=fin.read())!=-1){ 
	    cout.write(b); 
	  } 
	  cout.close(); 
	  fout.close(); 
	  fin.close();//生成密钥文件和对TEMP文件的加密文件(是好的) 
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
	File f1,f2=null;   //传输 
	FileReader fin1,fin2=null; 
	BufferedReader read1,read2 =null; 
	int port =2345; 
	Socket socket1=null; 
	DataInputStream in1=null; 
  DataOutputStream dout=null; 
  String s1,s2=null; 
  StringBuffer temp1=new StringBuffer(); 
  StringBuffer temp2=new StringBuffer(); 
	String content1,content2=null; 
	try{ 
		socket1=new Socket("10.100.101.4",port); 
	  in1=new DataInputStream(socket1.getInputStream()); 
		dout=new DataOutputStream(socket1.getOutputStream()); 
		} 
	catch(IOException e){} 
	if(socket1!=null){ 
	  f1=new File("c:/deskey.dat"); 
	  f2=new File("c:/desencryp.txt"); 
		fin1=new FileReader(f1); 
		fin2=new FileReader(f2); 
		read1=new BufferedReader(fin1); 
		read2=new BufferedReader(fin2); 
		while((s1=read1.readLine())!=null){ 
			temp1.append(s1); 
			} 
		  content1=new String(temp1); 
		while((s2=read2.readLine())!=null){ 
			temp2.append(s2); 
			} 
		content2=new String(temp2); 
	  try{ 
			dout.writeUTF("miyao:"+content1); 
			dout.writeUTF("jiami:"+content2); 
			} 
		catch(IOException e){} 
		}	 
	try{socket1.close();} 
	catch(IOException e){}	 
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  } 
}