表達式Exp = S1 + OP + S2
(S1 ,S2是兩個操做數,OP爲運算符)有三種標識方法:html
由中綴轉後綴,可使用棧(stack)。棧 (Stack)是一種只容許在表尾插入和刪除的線性表,有先進後出(FILO),後進先出(LIFO)的特色。容許插入和刪除的一端稱爲棧頂(top),另外一端稱爲棧底(bottom)。java
使用API能夠查詢到Stack的主要方法git
由中綴式求得後綴式的僞代碼以下:算法
MyBC.javaexpress
/** * @5312 AND 5309lenovo */ public class MyBC { public static String toPostfix(String expr){ MyStack<String> stack = new MyStack<>(expr.length()); String postfix = ""; int i = 0; while(i<expr.length()){ char ch = expr.charAt(i); switch (ch){ case '+': case '-':while(!stack.isEmpty() && !stack.get().equals("(")) postfix += stack.pop(); //postfix += " "; stack.push(ch + ""); i++; break; case '*': case '/':while (!stack.isEmpty() && (stack.get().equals("*")||stack.get().equals("/"))) postfix += stack.pop(); //postfix += " "; stack.push(ch + ""); i++; break; case '(':stack.push(ch + ""); i++; break; case ')':String out = stack.pop(); while(out!=null && !out.equals("(")){ postfix += out; out = stack.pop(); //postfix += " "; } i++; break; default:while(i < expr.length() && ch>='0' && ch<='9'){ postfix += ch; i++; if(i<expr.length()) ch = expr.charAt(i); } postfix += " "; } } while (!stack.isEmpty()) postfix += stack.pop(); return postfix; } }
MyDC.java編程
import java.util.Stack; /** * @5312 AND 5309 lenovo */ public class MyDC { /** constant for addition symbol */ private final char ADD = '+'; /** constant for subtraction symbol */ private final char SUBTRACT = '-'; /** constant for multiplication symbol */ private final char MULTIPLY = '*'; /** constant for division symbol */ private final char DIVIDE = '/'; public int value(String postfix){ Stack<Integer> stack = new Stack(); int i = 0, result = 0; while(i < postfix.length()){ char ch = postfix.charAt(i); if(ch>='0' && ch<='9'){ result = 0; while(ch!=' '){ result = result*10 + Integer.parseInt(ch+""); i++; ch = postfix.charAt(i); } i++; stack.push(new Integer(result)); } else{ int y = stack.pop().intValue(); int x = stack.pop().intValue(); switch (ch){ case ADD: result = x + y; break; case SUBTRACT: result = x - y; break; case MULTIPLY: result = x * y; break; case DIVIDE: result = x / y; } stack.push(new Integer(result)); i++; } } return stack.pop().intValue(); } }
MyDCTest.java數組
import junit.framework.TestCase; import java.util.Scanner; public class MyDCTest extends TestCase { public static void main(String[] args) { String expression; int result; try { Scanner in = new Scanner(System.in); MyDC evaluator = new MyDC(); System.out.println ("請輸入中綴表達式 "); expression = in.nextLine(); String postfix = MyBC.toPostfix(expression); System.out.println ("後綴表示式是 :" + postfix); result = evaluator.value (postfix); System.out.println ("計算結果是 :" + result); } catch (Exception IOException) { System.out.println("Input exception reported"); } } }
我和20165309吳思佳結對編程,我負責客戶端安全
Client.java服務器
/** * Created by 5309 AND 5312 */ import java.net.*; import java.io.*; public class Client { public static void main(String srgs[]) throws Exception { try { // 一、建立客戶端Socket,指定服務器地址和端口 Socket socket=new Socket("127.0.0.1",10000); System.out.println("客戶端成功啓動,等待服務器應答"); // 二、獲取輸出流,向服務器端發送信息 // 向本機的10000端口發出客戶請求 System.out.println("請輸入中綴表達式:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 由系統標準輸入設備構造BufferedReader對象 PrintWriter write = new PrintWriter(socket.getOutputStream()); // 由Socket對象獲得輸出流,並構造PrintWriter對象 //三、獲取輸入流,並讀取服務器端的響應信息 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); // 由Socket對象獲得輸入流,並構造相應的BufferedReader對象 String readline, infix, expression; readline = br.readLine(); // 從系統標準輸入讀入一字符串 MyBC theTrans = new MyBC(readline); infix = theTrans.doTrans(); StringBuilder newInfix = new StringBuilder(infix.replace(" ","")); for (int i = 1; i < infix.length()+(i+1)/2 ; i=i+2) { newInfix.insert(i," "); } System.out.println("後綴表達式:" + newInfix); expression=newInfix.toString(); while (!readline.equals("end")) { // 若從標準輸入讀入的字符串爲 "end"則中止循環 write.println(expression); // 將從系統標準輸入讀入的字符串輸出到Server write.flush(); // 刷新輸出流,使Server立刻收到該字符串 System.out.println("收到服務器的消息:" + in.readLine()); // 從Server讀入一字符串,並打印到標準輸出上 readline = br.readLine(); // 從系統標準輸入讀入一字符串 } // 繼續循環 //四、關閉資源 write.close(); // 關閉Socket輸出流 in.close(); // 關閉Socket輸入流 socket.close(); // 關閉Socket } catch (Exception e) { System.out.println(e);//輸出異常 } finally { } } }
我負責的依舊是客戶端,增長了使用DES算法加密的部分,DES算法參考婁老師的博客Java 密碼學算法。Client.java在剛纔的基礎上增長如下代碼,實現DES的加密網絡
FileInputStream f=new FileInputStream("key1.dat"); ObjectInputStream b=new ObjectInputStream(f); Key k=(Key)b.readObject(); Cipher cp=Cipher.getInstance("DESede"); cp.init(Cipher.ENCRYPT_MODE, k); byte ptext[]=expression.getBytes("UTF-8"); byte ctext[]=cp.doFinal(ptext); String Str=new String(ctext,"ISO-8859-1");
DH算法參考婁老師的博客客Java 密碼學算法。
Key_DH.java
import java.io.*; import java.math.*; import java.security.*; import java.security.spec.*; import javax.crypto.*; import javax.crypto.spec.*; import javax.crypto.interfaces.*; public class Key_DH{ //三個靜態變量的定義從 // C:\j2sdk-1_4_0-doc\docs\guide\security\jce\JCERefGuide.html // 拷貝而來 // The 1024 bit Diffie-Hellman modulus values used by SKIP private static final byte skip1024ModulusBytes[] = { (byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58, (byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD, (byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4, (byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B, (byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D, (byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C, (byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C, (byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6, (byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0, (byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B, (byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB, (byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D, (byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD, (byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43, (byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C, (byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C, (byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C, (byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40, (byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C, (byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72, (byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03, (byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29, (byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C, (byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB, (byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B, (byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08, (byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D, (byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C, (byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22, (byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB, (byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55, (byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7 }; // The SKIP 1024 bit modulus private static final BigInteger skip1024Modulus = new BigInteger(1, skip1024ModulusBytes); // The base used with the SKIP 1024 bit modulus private static final BigInteger skip1024Base = BigInteger.valueOf(2); public static void main(String args[ ]) throws Exception{ DHParameterSpec DHP= new DHParameterSpec(skip1024Modulus,skip1024Base); KeyPairGenerator kpg= KeyPairGenerator.getInstance("DH"); kpg.initialize(DHP); KeyPair kp=kpg.genKeyPair(); PublicKey pbk=kp.getPublic(); PrivateKey prk=kp.getPrivate(); // 保存公鑰 FileOutputStream f1=new FileOutputStream(args[0]); ObjectOutputStream b1=new ObjectOutputStream(f1); b1.writeObject(pbk); // 保存私鑰 FileOutputStream f2=new FileOutputStream(args[1]); ObjectOutputStream b2=new ObjectOutputStream(f2); b2.writeObject(prk); } }
KeyAgree.java
import java.io.*; import java.math.*; import java.security.*; import java.security.spec.*; import javax.crypto.*; import javax.crypto.spec.*; import javax.crypto.interfaces.*; public class KeyAgree{ public static void main(String args[ ]) throws Exception{ // 讀取對方的DH公鑰 FileInputStream f1=new FileInputStream(args[0]); ObjectInputStream b1=new ObjectInputStream(f1); PublicKey pbk=(PublicKey)b1.readObject( ); //讀取本身的DH私鑰 FileInputStream f2=new FileInputStream(args[1]); ObjectInputStream b2=new ObjectInputStream(f2); PrivateKey prk=(PrivateKey)b2.readObject( ); // 執行密鑰協定 KeyAgreement ka=KeyAgreement.getInstance("DH"); ka.init(prk); ka.doPhase(pbk,true); //生成共享信息 byte[ ] sb=ka.generateSecret(); for(int i=0;i<sb.length;i++){ System.out.print(sb[i]+","); } SecretKeySpec k=new SecretKeySpec(sb,"DESede"); } }
MD5算法參考婁老師的博客客Java 密碼學算法。
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); } }
1.在作第一個實驗的時候,運行MyDCTest.java的時候,出現了junit.framework.AssertionFailedError: No tests found in MyDCTest
的錯誤。而後我百度了這個問題,找到了Junit 報錯:No tests Found in xx這篇博客,解決了問題。
這一次的結對編程是有關於客戶端和服務器,我主要負責客戶端,個人搭檔吳思佳主要負責服務器,有關於密碼算法是咱們倆共同研究、查找資料的。服務器和客戶端是相互關聯的關係,這就要求咱們倆要配合默契,不能有一我的沒有完成指定的任務,結對編程減輕了工做量,並增添了編程中的樂趣。