public class CodeStandard { public static void main(String [] args){ StringBuffer buffer = new StringBuffer(); buffer.append('S'); buffer.append("tringBuffer"); System.out.println(buffer.charAt(1)); System.out.println(buffer.capacity()); System.out.println(buffer.indexOf("tring")); System.out.println("buffer = " + buffer.toString()); if(buffer.capacity()<20) buffer.append("1234567"); for(int i=0; i<buffer.length();i++) System.out.println(buffer.charAt(i)); } }
(1).實驗一:安裝alibaba插件:IDEA中的插件,咱們經過Jetbrains官方倉庫安裝:打開 Settings ->Plugins -> Browse repositories...在搜索框輸入alibaba便可看到Alibaba Java Code Guidelines插件,點擊Install進行安裝,而後重啓IDE生效,並使用Reformate Code對代碼進行優化。
php
(2)實驗二:下載搭檔實驗二的Complex代碼,加入很多於三個JUnit單元測試用例,提交搭檔項目git log的截圖,包含上面git commit的信息,並加上本身的學號水印信息。
碼雲連接: https://gitee.com/CS-IMIS-23/Java-pro/blob/master/src/test/exp3/ComplexTest.javahtml
(3)實驗三:下載搭檔的代碼,至少進行三項重構
碼雲連接: https://gitee.com/CS-IMIS-23/Homework/tree/master/test/experiment3java
(4)實驗四:參考 http://www.cnblogs.com/rocedu/p/6683948.html,以結對的方式完成Java密碼學相關內容的學習,結合重構,git,代碼標準
截圖偏多,這裏再也不一一展現,可查看藍墨雲班課提交圖片: https://www.mosoteach.cn/web/index.php?c=interaction_homework&m=s_write&clazz_course_id=DAAF9347-1064-11E8-AA22-7CD30AD36C02&id=64B877DE-A497-CE84-E07C-9D662600EF7F&order_item=group
碼雲連接: https://gitee.com/CS-IMIS-23/Java-pro/tree/master/src/exp3git
問題一:關於密碼學學習實驗項目,參考的連接Java 密碼學算法也看不太明白這個實驗要作什麼,怎麼作,整個實驗一頭霧水。web
問題一解決方案:參考張昊然同窗在藍墨雲上的提問與解答記錄瞭解到實驗的進行過程,
算法
問題二:關於密碼學中使用密鑰協定建立共享密鑰這一項目實驗過程當中出現了問題,截圖沒有保存下來,問題是在進行這個代碼的編譯運行時找不到Bpub.dat和Bpri.dat文件。數據結構
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"); } }
創建兩個目錄A和B,模擬須要祕密通訊的A、B雙方,因爲DH算法須要A和B各自生成DH公鑰和私鑰,所以在這兩個目錄下都拷貝編譯後文件Key_DH。
首先由A建立本身的公鑰和私鑰,即在A目錄下輸入「java Key_DH Apub.dat Apri.dat」運行程序,這時在目錄A下將產生文件Apub.dat和Apri.dat,前者保存着A的公鑰,後者保存着A的私鑰。
而後由B建立本身的公鑰和私鑰,即在B目錄下輸入「java Key_DH Bpub.dat Bpri.dat」運行程序,這時在目錄B下將產生文件Bpub.dat和Bpri.dat,前者保存着B的公鑰,後者保存着B的私鑰。
最後發佈公鑰,A將Apub.dat拷貝到B目錄,B將Bpub.dat拷貝到A的目錄。這樣,A、B雙方的DH公鑰和私鑰已經建立並部署完畢。app
本身進行實驗時值進行了一次命令行輸入,就致使了上面的問題。再進行二次輸入下面的參數,問題就解決了。下面是Key_DH的代碼ide
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); } }