深圳建设局网站深业中城绿化项目,萝卜建站分类信息,网站开发基础课程,纯flash网站一、实验内容1#xff0e;掌握Socket程序的编写#xff1b;2#xff0e;掌握密码技术的使用#xff1b;3#xff0e;设计安全传输系统。二、实验步骤1. 基于Java Socket实现安全传输2. 基于TCP实现客户端和服务器#xff0c;结对编程一人负责客户端#xff0c;一人负责服…一、实验内容1掌握Socket程序的编写2掌握密码技术的使用3设计安全传输系统。二、实验步骤1. 基于Java Socket实现安全传输2. 基于TCP实现客户端和服务器结对编程一人负责客户端一人负责服务器3. 使用Git进行版本控制4. 选择对称算法进行数据加解密.5. 选择非对称算法对对称加密密钥进行密钥分发.6. 选择合适的Hash算法进行完整性验证.7. 选择合适的算法对Hash值进行签名/验证.三、设计思路起初我与队友看到这个实验题目是一筹莫展的一是不太了解IO流的文件读取与文件存储位置二是即使在课上听懂了客户端与服务器之间的加解密消息与传送的流程也不知道如何应用java代码将其实现。在实验课上我们咨询了老师的大致操作并且仔细学习书上有关IO流的知识以及应用哈希函数验证加解密过程的正确性的实现开始动手操作了。1.首先是运行服务器与客户端的代码。客户端public static voidmain(String[] args) throws Exception{InetAddress addrInetAddress.getByName(192.168.252.1);System.out.println(addraddr);Socket socketnew Socket(addr,8080);try{System.out.println(socketsocket);BufferedReaderinnew BufferedReader(newInputStreamReader(socket.getInputStream()));PrintWriteroutnew PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);for(int i0;i10;i){out.println(howdyi);String strin.readLine();System.out.println(str);}2.在客户端Client程序中输入明文“Hello World”然后利用随机秘钥发生器产生DES秘钥并将秘钥应用IO流存入文件keykb1.dat中。String sHello World!;KeyGenerator kgKeyGenerator.getInstance(DESede);kg.init(168);SecretKey kkg.generateKey( );byte[ ] kbk.getEncoded( );FileOutputStream fknew FileOutputStream(keykb1.dat);fk.write(kb);for(int i0;iSystem.out.print(kb[i],);//打印扩展秘钥}3.然后打印明文并转换为UTF8格式并将明文用秘钥加密。Cipher cpCipher.getInstance(DESede);cp.init(Cipher.ENCRYPT_MODE, k);byte ptext[]s.getBytes(UTF8);for(int i0;iSystem.out.print(ptext[i],);}System.out.println();byte ctext[]cp.doFinal(ptext);for(int i0;iSystem.out.print(ctext[i] ,);}4.传递密文给服务器FileOutputStream f2new FileOutputStream(SEnc.dat);f2.write(ctext);5.服务器应用随机秘钥发生器产生服务器的公钥和私钥KeyPairGenerator kpg KeyPairGenerator.getInstance(RSA);kpg.initialize(1024);KeyPair kpkpg.genKeyPair();PublicKey pbkeykp.getPublic();PrivateKey prkeykp.getPrivate();FileOutputStream f1 newFileOutputStream(Skey_RSA_pub.dat);ObjectOutputStream b1 newObjectOutputStream(f1);b1.writeObject(pbkey);FileOutputStream f2 newFileOutputStream(Skey_RSA_priv.dat);ObjectOutputStream b2 newObjectOutputStream(f2);b2.writeObject(prkey);6.客户端创建服务器的公开密钥将密钥加密传递给服务器FileInputStream fnew FileInputStream(Skey_RSA_pub.dat);ObjectInputStream bnewObjectInputStream(f);RSAPublicKey pbk(RSAPublicKey)b.readObject( );BigInteger epbk.getPublicExponent();BigInteger npbk.getModulus();System.out.println(ee);System.out.println(nn);byte ptext1[]s.getBytes(UTF8);BigInteger mnewBigInteger(ptext1);BigInteger cm.modPow(e,n);System.out.println(cc);String csc.toString( );BufferedWriter out1new BufferedWriter(newOutputStreamWriter(new FileOutputStream(Enc_RSA.dat)));out1.write(cs,0,cs.length( ));out1.close( );7.服务器用服务器的私钥将客户端传输的DES的秘钥解密BufferedReader in1new BufferedReader(new InputStreamReader(new FileInputStream(Enc_RSA.dat)));String ctextin1.readLine();BigInteger cnewBigInteger(ctext);FileInputStream fnew FileInputStream(Skey_RSA_priv.dat);ObjectInputStream bnewObjectInputStream(f);RSAPrivateKey prk(RSAPrivateKey)b.readObject( );BigInteger dprk.getPrivateExponent();BigInteger nprk.getModulus();System.out.println(dd);System.out.println(nn);BigInteger mc.modPow(d,n);System.out.println(mm);byte[] mtm.toByteArray();System.out.println(PlainKey is);for(int i0;i{System.out.print((char) mt[i]);}8.服务器用上一步产生的秘钥解密DES产生的密文FileInputStream fsdnew FileInputStream(SEnc.dat);int numfsd.available();byte[ ] ctextdnew byte[num];fsd.read(ctextd);FileInputStream fsd2new FileInputStream(keykb1.dat);int num2fsd2.available();byte[ ] keykbnew byte[num2];fsd2.read(keykb);SecretKeySpec knew SecretKeySpec(keykb,DESede);Cipher cpCipher.getInstance(DESede);cp.init(Cipher.DECRYPT_MODE, k);byte []ptextcp.doFinal(ctextd);String pnew String(ptext,UTF8);System.out.println(服务器收到的信息为p);9.服务器、客户端应用Hash函数验证加解密的正确性System.out.println(result);*/String ha in.readLine();String sahash(p);if(ha.equals(sa))System.out.println(正确);}finally{System.out.println(closing..);socket.close();}}finally{s.close();}String xs;MessageDigest md5MessageDigest.getInstance(MD5);md5.update(x.getBytes( ));byte smd5[ ]md5.digest( );String result;for (int i0; iresultInteger.toHexString((0x000000ff smd5[i]) |0xffffff00).substring(6);}System.out.println(result);四、实验结果客户端服务器两张图片验证的哈希值相同说明加解密成功。五、遇到的问题两个人进行测试时服务器与客户端已成功连接但是服务器不显示结果当自己相连时会出现服务器结果但是会被客户端结果覆盖但可以快速截图截下结果且结果正确。六、解决办法至今为止调试多次并没有找到可行的解决办法明天检查代码时咨询老师。七、实验分析这次实验从全班同学都不会做到一点点探究一点点学习最终做出不甚完美但结果正确的代码还是很有收获的。我从中学会了IO流传输以及文件的存储与读取。而且在这次试验中我并不太了解老师打包发给我们的密码算法的每一条语句但是把他们筛选整合拼凑到一个代码中的技能却掌握了学习java语言最重要的是掌握方法与架构那些具体的函数与语句不是完全明白也可以应用它们。八、统计时间步骤耗时百分比需求分析3h27.3%设计1h9.1%代码实现2h18.2%测试4h36.4%分析总结1h9.1%