while (tokenizer.hasMoreTokens()) { token = tokenizer.nextToken(); //若是是運算符,調用isOperator if (this.isOperator(token)) { if(token.charAt(0)==')') while (Nch!='(') { Nch=this.stack.pop(); if(Nch!='(') Nextexpr=Nextexpr+" "+Nch; } Nch=' '; ch=this.stack.pop(); this.stack.push(ch); if(('*'==token.charAt(0)&&(ch=='+'||ch=='-'))||('*'==token.charAt(0)&&ch==' ')||('*'==token.charAt(0)&&ch=='(')|| ('/'==token.charAt(0)&&(ch=='+'||ch=='-'))||('/'==token.charAt(0)&&ch==' ')||('/'==token.charAt(0)&&ch=='(') ||('-'==token.charAt(0)&&(ch=='('||ch==' ')) ||('+'==token.charAt(0)&&(ch=='('||ch==' ')) || '('==token.charAt(0)) this.stack.push(token.charAt(0)); else if(token.charAt(0)!=')'){ Nextexpr=Nextexpr+" "+this.stack.pop(); ch=this.stack.pop(); this.stack.push(ch); if(!(('*'==token.charAt(0)&&(ch=='+'||ch=='-'))||('*'==token.charAt(0)&&ch==' ')||('*'==token.charAt(0)&&ch=='(')|| ('/'==token.charAt(0)&&(ch=='+'||ch=='-'))||('/'==token.charAt(0)&&ch==' ')||('/'==token.charAt(0)&&ch=='(') ||('-'==token.charAt(0)&&(ch=='('||ch==' ')) ||('+'==token.charAt(0)&&(ch=='('||ch==' ')) || '('==token.charAt(0))){ Nextexpr=Nextexpr+" "+this.stack.pop(); } this.stack.push(token.charAt(0)); } } else//若是是操做數 { Nextexpr=Nextexpr+" "+token; } } while(!this.stack.empty()) { Nextexpr=Nextexpr+" "+this.stack.pop(); }
結對編程:1人負責客戶端,一人負責服務器html
我負責的是客戶端java
端口號和IP設置代碼android
Socket socket = new Socket("192.168.43.252",5204); System.out.println("客戶端啓動成功");
加密結對編程:1人負責客戶端,一人負責服務器git
密鑰分發結對編程:1人負責客戶端,一人負責服務器web
創建兩個目錄A和B,模擬須要祕密通訊的A、B雙方,因爲DH算法須要A和B各自生成DH公鑰和私鑰,所以在這兩個目錄下都拷貝編譯後文件Key_DH。算法
最後發佈公鑰,A將Apub.dat拷貝到B目錄,B將Bpub.dat拷貝到A的目錄。
這樣,A、B雙方的DH公鑰和私鑰已經建立並部署完畢。編程
運行截圖數組
完整性校驗結對編程:1人負責客戶端,一人負責服務器安全
import java.security.*; public class DigestPass{ public static void main(String args[ ]) throws Exception{ String x=args[0]; MessageDigest m=MessageDigest.getInstance("MD5"); m.update(x.getBytes("UTF8")); byte s[ ]=m.digest( ); String result=""; for (int i=0; i<s.length; i++){ result+=Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6); } System.out.println(result); } }
package com.example.dell.webapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.Socket; import java.net.UnknownHostException; public class MainActivity extends AppCompatActivity { private TextView text1; private Button but1; private TextView text2; private EditText edit1; private final String DEBUG_TAG="mySocketAct"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text1 = (TextView)findViewById(R.id.text1); text2 = (TextView)findViewById(R.id.text2); but1 = (Button)findViewById(R.id.but1); edit1 = (EditText) findViewById(R.id.edit); but1.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v){ Socket socket = null; String mesg = edit1.getText().toString(); Log.e("dddd","set id"); text1.setText(mesg); MyBC mybc=new MyBC(); MyDC mydc=new MyDC(); String Np=""; String mesg1=mybc.evaluate(mesg); for(int i=2;i<mesg1.length();i++) Np+=mesg1.charAt(i); int a= mydc.evaluate(Np); String str=a+""; text2.setText(str); try{ //text2.setText("結果"); socket = new Socket("172.16.2.107",5216); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); mesg1 = mybc.evaluate(mesg); // for(int i=2;i<mesg1.length();i++) Np+=mesg1.charAt(i); out.println(Np); out.flush(); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String mstr = br.readLine(); if(mstr!=null){ text2.setText(mstr); }else{ text2.setText("數據錯誤"); } out.close(); br.close(); socket.close(); }catch (UnknownHostException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); }catch (Exception e){ Log.e(DEBUG_TAG, e.toString()); } } }); } }
客戶端的顯示:
can not listen to:java.net.SocketException: Connection reset
服務器
服務器端顯示:Error.javax.crypto.BadPaddingException: Given final block not properly padded
問題解決
這每每是從客戶端傳來的密文不符合編碼要求致使的,在查閱了不少相關問題解決的網站後,總結出,在傳送密文時,須要將編碼改成ISO-8859-1
,而不是一慣的UTF8
,改完後就能夠正常運行了;
此次實驗讓我對結對編程有了更深的認識,兩我的一塊兒能夠很好的各自發揮本身的長處,能夠更加有效,更加快速的解決問題,真的感謝20155303同窗的幫助。
步驟 | 耗時(min) | 百分比 |
---|---|---|
需求分析 | 20 | 20 |
設計 | 30 | 30 |
代碼實現 | 30 | 30 |
測試 | 10 | 10 |
分析總結 | 10 | 10 |