任務一java
ab+cde+**
,改成中綴表達式實際上是(a+b)*((d+e)*c)
/* creat by xyw,cyy */ import java.util.Stack; public class MyBC { MyBC(){} public static String infixToSuffix(String exp){ Stack<String> s = new Stack<String>(); // 建立操做符堆棧 String suffix = ""; // 要輸出的後綴表達式字符串 String suffix1 = ""; //上一次的後綴表達式 String suffix2 = ""; String str[] = exp.split(" "); int length = str.length; // 輸入的中綴表達式的長度 String temp=""; for (int i = 0; i < length; i++) { // 對該中綴表達式的每個字符並進行判斷 switch (str[i]) { case " ":break; // 忽略空格 case "(": s.push(str[i]); // 若是是左括號直接壓入堆棧 break; case "+": case "-": if(s.size() != 0){ // 碰到'+' '-',將棧中的全部運算符所有彈出去,直至碰到左括號爲止,輸出到隊列中去 temp = s.pop(); if (temp.equals("(")) { // 將左括號放回堆棧,終止循環 s.push(temp); s.push(str[i]); break; } else{ s.push(str[i]); suffix2 = suffix2 + temp + " "; break; } } else{ s.push(str[i]); // 說明是當前爲第一次進入或者其餘前面運算都有括號等狀況致使棧已經爲空,此時須要將符號進棧 break; } // 若是是乘號或者除號,則彈出全部序列,直到碰到加好、減號、左括號爲止,最後將該操做符壓入堆棧 case "*": case "÷": if(s.size()!=0){ temp = s.pop(); if(temp.equals("+")||temp.equals("-")||temp.equals("(")){ s.push(temp); s.push(str[i]); break; } else{ s.push(str[i]); suffix2 = suffix2+temp+" "; break; } } else { s.push(str[i]); //當前爲第一次進入或者其餘前面運算都有括號等狀況致使棧已經爲空,此時須要將符號進棧 break; } // 若是碰到的是右括號,則距離棧頂的第一個左括號上面的全部運算符彈出棧並拋棄左括號 case ")": while (!s.isEmpty()) { temp = s.pop(); if (temp.equals("(")) { break; } else { suffix2 = suffix2+temp+" "; } } break; // 默認狀況,若是讀取到的是數字,則直接送至輸出序列 default: suffix2 = suffix2+str[i]+" "; break; } } // 若是堆棧不爲空,則把剩餘運算符一次彈出,送至輸出序列 while (s.size() != 0) { suffix2 = suffix2+s.pop()+" "; } if(suffix1.equals("")){ //第一個題目 suffix1 = suffix2; suffix = suffix2; } else{ if(suffix2.equals(suffix1)) suffix = ""; else suffix = suffix2; } suffix1 = suffix2; return suffix; } }
實驗截圖
算法
import java.io.*; import java.net.*; public class Server { public static void main(String args[]) { int answer; ServerSocket serverForClient=null; Socket socketOnServer=null; DataOutputStream out=null; DataInputStream in=null; try { serverForClient = new ServerSocket(2010); } catch(IOException e1) { System.out.println(e1); } try{ System.out.println("等待客戶呼叫"); socketOnServer = serverForClient.accept(); //堵塞狀態,除非有客戶呼叫 out=new DataOutputStream(socketOnServer.getOutputStream()); in=new DataInputStream(socketOnServer.getInputStream()); String s=in.readUTF(); // in讀取信息,堵塞狀態 System.out.println("服務器收到客戶的提問:"+s); MyDC d=new MyDC(); answer=d.evaluate(s); out.writeUTF(answer+""); Thread.sleep(500); } catch(Exception e) { System.out.println("客戶已斷開"+e); } } }
**客戶端**
import java.io.*; import java.net.*; import java.lang.*; import java.util.Scanner; public class Client { public static void main(String args[]) { Socket mysocket; DataInputStream in=null; DataOutputStream out=null; try{ mysocket=new Socket("127.1.0.0",2010); in=new DataInputStream(mysocket.getInputStream()); out=new DataOutputStream(mysocket.getOutputStream()); System.out.println("請輸入算式:"); Scanner scanner=new Scanner(System.in); String str=scanner.nextLine(); MyBC b=new MyBC(); str=b.result(str); out.writeUTF(str); String s=in.readUTF(); //in讀取信息,堵塞狀態 System.out.println("客戶收到服務器的回答:"+s); Thread.sleep(500); } catch(Exception e) { System.out.println("服務器已斷開"+e); } } }
public Socket(java.lang.String host, int port)
。其中,host是遠程機器名或IP地址,port是端口號,IP地址查詢方法以下:KeyGenerator kg=KeyGenerator.getInstance("DESede")
kg.init(168)
;SecretKey k=kg.generateKey( )
;byte[ ] kb=k.getEncoded( )
;Cipher cp=Cipher.getInstance("DESede")
;cp.init(Cipher.ENCRYPT_MODE, k)
;byte ptext[]=s.getBytes("UTF8")
;byte []ptext=cp.doFinal(ctext)
Cipher cp=Cipher.getInstance("DESede")
;cp.init(Cipher.DECRYPT_MODE, k)
;byte []ptext=cp.doFinal(ctext)
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.io.*; import java.net.*; import java.util.Scanner; public class Client1 { public static void main(String[] args) { Scanner inn = new Scanner(System.in); Socket mysocket; DataInputStream in = null; DataOutputStream out = null; try{ mysocket = new Socket("127.0.0.1",2010); in = new DataInputStream(mysocket.getInputStream()); out = new DataOutputStream(mysocket.getOutputStream()); KeyGenerator kg=KeyGenerator.getInstance("DESede"); kg.init(168); SecretKey k=kg.generateKey( ); byte[ ] kb=k.getEncoded( ); out.writeUTF(kb.length+""); for(int i=0; i<kb.length; i++){ out.writeUTF(kb[i]+""); } System.out.println("請輸入中綴表達式:"); String infix = inn.nextLine(); MyBC myBC = new MyBC(); String suffix = myBC.infixToSuffix(infix); Cipher cp=Cipher.getInstance("DESede"); cp.init(Cipher.ENCRYPT_MODE, k); byte ptext[]=suffix.getBytes("UTF8"); byte ctext[]=cp.doFinal(ptext); out.writeUTF(ctext.length+""); for(int i=0; i<ctext.length; i++){ out.writeUTF(ctext[i]+""); } String result = in.readUTF(); System.out.println("收到請回復"+result); Thread.sleep(500); }catch (Exception e){ System.out.println("斷開鏈接"+e); } } }
服務器
import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.io.*; import java.net.*; public class Sever1 { public static void main(String[] args) { ServerSocket serverForClient = null; Socket socketOnServer = null; DataOutputStream out = null; DataInputStream in = null; MyDC myDC = new MyDC(); try{ serverForClient = new ServerSocket(2010); }catch (IOException e1){ System.out.println("請客戶端回覆"+e1); } try{ System.out.println("客戶端收到"); socketOnServer = serverForClient.accept(); out = new DataOutputStream(socketOnServer.getOutputStream()); in = new DataInputStream(socketOnServer.getInputStream()); String keylength = in.readUTF(); byte []kb = new byte[Integer.parseInt(keylength)]; for(int i=0; i<Integer.parseInt(keylength); i++){ String t = in.readUTF(); kb[i] = Byte.parseByte(t); } String clength = in.readUTF(); byte []ctext = new byte[Integer.parseInt(clength)]; for(int i=0; i<Integer.parseInt(clength); i++){ String temp = in.readUTF(); ctext[i] = Byte.parseByte(temp); } SecretKeySpec k=new SecretKeySpec(kb,"DESede"); Cipher cp=Cipher.getInstance("DESede"); cp.init(Cipher.DECRYPT_MODE, k); byte []ptext=cp.doFinal(ctext); String suffix = new String(ptext,"UTF8"); System.out.println("請提問"+suffix); out.writeUTF(myDC.evaluate(suffix)+""); Thread.sleep(500); }catch (Exception e){ System.out.println("已斷開鏈接"); } } }
實驗截圖
編程
4.任務四數組
Key_DH
和KeyAgree
主類的參數改成由客戶端和服務器傳遞,並修改方法名public static void DH(String str1,String str2) throws Exception
public static void Agree(String str1,String str2)
KeyAgreement ka=KeyAgreement.getInstance("DH")
;ka.init(prk)
;ka.doPhase(pbk,true)
;byte[ ] sb=ka.generateSecret()
;import javax.crypto.spec.*; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.Socket; import java.util.Scanner; public class Client4 { public static void main(String[] args) { Scanner inn = new Scanner(System.in); Socket mysocket; DataInputStream in = null; DataOutputStream out = null; try { mysocket = new Socket("127.0.0.1", 2010); in = new DataInputStream(mysocket.getInputStream()); out = new DataOutputStream(mysocket.getOutputStream()); KeyGenerator kg = KeyGenerator.getInstance("DESede"); kg.init(168); SecretKey k = kg.generateKey(); byte[] kb = k.getEncoded(); System.out.println("請輸入中綴表達式:"); String infix = inn.nextLine(); MyBC myBC = new MyBC(); String suffix = myBC.infixToSuffix(infix); System.out.println(suffix); //中綴表達式加密 Cipher cp = Cipher.getInstance("DESede"); cp.init(Cipher.ENCRYPT_MODE, k); byte ptext[] = suffix.getBytes("UTF8"); byte ctext[] = cp.doFinal(ptext); out.writeUTF(ctext.length + ""); for (int i = 0; i < ctext.length; i++) { out.writeUTF(ctext[i] + ""); } //對密鑰進行加密 KeyAgree keyAgree = new KeyAgree(); SecretKeySpec k1 = keyAgree.KeyAgree("Serverpub.dat","Clientpri.dat"); cp.init(Cipher.ENCRYPT_MODE, k1); byte ckey[] = cp.doFinal(kb); out.writeUTF(ckey.length + ""); for (int i = 0; i < ckey.length; i++) { out.writeUTF(ckey[i] + ""); } String result = in.readUTF(); System.out.println("服務器收到請回去" + result); Thread.sleep(500); } catch (Exception e) { System.out.println("服務器斷開鏈接" + e); } } }
import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.io.*; import java.net.*; public class Server4 { public static void main(String[] args) { ServerSocket serverForClient = null; Socket socketOnServer = null; DataOutputStream out = null; DataInputStream in = null; MyDC myDC = new MyDC(); try{ serverForClient = new ServerSocket(2010); }catch (IOException e1){ System.out.println("請提出問題"+e1); } try{ System.out.println("等待中"); socketOnServer = serverForClient.accept(); out = new DataOutputStream(socketOnServer.getOutputStream()); in = new DataInputStream(socketOnServer.getInputStream()); //獲取密文 String clength = in.readUTF(); byte []ctext = new byte[Integer.parseInt(clength)]; for(int i=0; i<Integer.parseInt(clength); i++){ String temp = in.readUTF(); ctext[i] = Byte.parseByte(temp); } //獲取密鑰 String keylength = in.readUTF(); byte []ckey = new byte[Integer.parseInt(keylength)]; for(int i=0; i<Integer.parseInt(keylength); i++){ String temp = in.readUTF(); ckey[i] = Byte.parseByte(temp); } //密鑰解密 SecretKeySpec k1 = KeyAgree.KeyAgree("Clientpub.dat","Serverpri.dat"); Cipher cp=Cipher.getInstance("DESede"); cp.init(Cipher.DECRYPT_MODE, k1); byte []pkey=cp.doFinal(ckey); //密文解密 SecretKeySpec k=new SecretKeySpec(pkey,"DESede"); cp.init(Cipher.DECRYPT_MODE, k); byte []ptext=cp.doFinal(ctext); String suffix = new String(ptext,"UTF8"); System.out.println("受到提問"+suffix); out.writeUTF(myDC.evaluate(suffix)+""); Thread.sleep(500); }catch (Exception e){ System.out.println("已斷開鏈接"); } } }
實驗截圖
安全
MyBC.java
的功能轉化爲後綴表達式,把後綴表達式用3DES或AES算法加密經過網絡把密文和明文的MD5値發送給服務器MyDC.java
的功能計算後綴表達式的值,把結果發送給客戶端MD5
MessageDigest
對象,MessageDigest m=MessageDigest.getInstance("MD5")
m.update(x.getBytes("UTF8" ))
byte s[ ]=m.digest( )