# 20172329 2017-2018-2 《程序设计与数据结构》实验三报告

20172329 2017-2018-2 《程序设计与数据结构》实验一报告

课程:《程序设计与数据结构》
班级: 1723
姓名: 王文彬
学号:20172329
实验教师:王志强
实验日期:2018年5月10日
必修/选修: 必修

一、实验内容

(1)代码规范

  • 安装alibaba 插件,解决代码中的规范问题。
  • 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化
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));
}
}

(2)协同测试

  • 在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后,下载搭档实验二的Complex代码,加入不少于三个JUnit单元测试用例,测试成功后git add .; git commit -m "自己学号 添加内容";git push;

(3)重构

  • 学习并完成重构内容的练习,下载搭档的代码,至少进行三项重构。

(4)密码学应用

  • 以结对的方式完成Java密码学相关内容的学习,结合重构。

二、 实验过程及结果

一、第一个实验:代码规范
1、首先,我们在设置里面下载并安装了alibaba 插件,开始了我们规范代码的道路。在刚刚装好这个插件的时候,满屏的规范异常,我们就只得跟着步骤一步一步来;

/**
 *  CodeStandard class
 *
 * @author wwb
 * @date 2018/5/16
 */
public class CodeStandard  {
    public static void main(String [] args){
        StringBuffer buffer = new StringBuffer();
        final int maxCapacity=20;
        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()<maxCapacity)
        {
            buffer.append("1234567");
        }
        for(int i=0; i<buffer.length();i++)
        {
            System.out.println(buffer.charAt(i));
        }
    }*/
}}

这个就是我们修改后的模样,虽然这个按照阿里巴巴他们的格式来定的,谁叫他们有钱呢,哎,虽然规范代码是一件比较繁琐的事情,但是他还是有好处的,闲的时候可以锻炼锻炼 Alt+Enter键位的练习,可以帮助我们规范一下自己的代码格式,让自己的代码样子更加好看一点。

二、第二个实验:协同测试
1、首先,我们在之前的结对编程中,就有过这样一个项目,三个人一起管理,所以对于接触到这个实验,觉得并没有什么难度,就在码云里面,对我的两位结对队友发起了加入我的项目的邀请。

加入以后:

就进行我们的测试:

三、第三个实验:重构
1、首先,我们就开始下载结对队友的代码,然后加上我们的重构,

比如就是这个样子,重命名.......
我会在参考文献里加入一个重构的介绍以及一些常用功能的介绍。

四、第三个实验:密码学应用
1、这个是我们的重头戏了,也是大家最感兴趣的时候来了,让我们一一来进行介绍;

  • (1)凯撒密码
public class javaCaesar {
    public static void main(String args[]) throws Exception{
        String s=args[0];
        int key=Integer.parseInt(args[1]);
        String es="";
        for(int i=0;i<s.length( );i++)
        {  char c=s.charAt(i);
            if(c>='a' && c<='z') // 是小写字母
            { c+=key%26;  //移动key%26位
                if(c<'a') {
                    c+=26;  //向左超界
                }
                if(c>'z') {
                    c-=26;  //向右超界
                }
            }
            else if(c>='A' && c<='Z') // 是大写字母
            {  c+=key%26;
                if(c<'A') {
                    c+=26;
                }
                if(c>'Z') {
                    c-=26;
                }
            }
            es+=c;
        }
        System.out.println(es);
    }
}

这个就是著名的凯撒密码,凯撒密码是罗马扩张时期朱利斯•凯撒(Julius Caesar)创造的,用于加密通过信使传递的作战命令。它将字母表中的字母移动一定位置而实现加密。

  • (2)Java对称加密-DES算法
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class SDec{
    public static void main(String args[]) throws Exception{
        // 获取密文
        FileInputStream f=new FileInputStream("SEnc.dat");
        int num=f.available();
        byte[ ] ctext=new byte[num];
        f.read(ctext);
        // 获取密钥
        FileInputStream  f2=new FileInputStream("keykb1.dat");
        int num2=f2.available();
        byte[ ] keykb=new byte[num2];
        f2.read(keykb);
        SecretKeySpec k=new  SecretKeySpec(keykb,"DESede");
        // 解密
        Cipher cp=Cipher.getInstance("DESede");
        cp.init(Cipher.DECRYPT_MODE, k);
        byte []ptext=cp.doFinal(ctext);
        // 显示明文
        String p=new String(ptext,"UTF8");
        System.out.println(p);
    }
}
import java.io.*;
import java.security.*;
import javax.crypto.*;
public class SEnc{
    public static void main(String args[]) throws Exception{
        String s="Hello World!";
        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[]=s.getBytes("UTF8");
        for(int i=0;i<ptext.length;i++){
            System.out.print(ptext[i]+",");
        }
        System.out.println("");
        byte ctext[]=cp.doFinal(ptext);
        for(int i=0;i<ctext.length;i++){
            System.out.print(ctext[i] +",");
        }
        FileOutputStream f2=new FileOutputStream("SEnc.dat");
        f2.write(ctext);
    }
}
import java.io.*;
import javax.crypto.*;
public class Skey_DES{
    public static void main(String args[])
            throws Exception{
        KeyGenerator kg=KeyGenerator.getInstance("DESede");
        kg.init(168);
        SecretKey k=kg.generateKey( );
        FileOutputStream  f=new FileOutputStream("key1.dat");
        ObjectOutputStream b=new  ObjectOutputStream(f);
        b.writeObject(k);
    }
}
import java.io.*;
import java.security.*;
public class Skey_kb{
    public static void main(String args[]) throws Exception{
        FileInputStream f=new FileInputStream("key1.dat");
        ObjectInputStream b=new ObjectInputStream(f);
        Key k=(Key)b.readObject( );
        byte[ ] kb=k.getEncoded( );
        FileOutputStream  f2=new FileOutputStream("keykb1.dat");
        f2.write(kb);
        // 打印密钥编码中的内容
        for(int i=0;i<kb.length;i++){
            System.out.print(kb[i]+",");
        }
    }
}
  • (3)Java非对称加密-RSA算法

    import java.security.*;
    import java.security.spec.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import javax.crypto.interfaces.*;
    import java.security.interfaces.*;
    import java.math.*;
    import java.io.*;
    public class Dec_RSA{
    public static void main(String args[]) throws Exception{
        //读取密文
        BufferedReader in=
                new BufferedReader(new InputStreamReader(
                        new FileInputStream("Enc_RSA.dat")));
        String ctext=in.readLine();
        BigInteger c=new BigInteger(ctext);
        //读取私钥
        FileInputStream f=new FileInputStream("Skey_RSA_priv.dat");
        ObjectInputStream b=new ObjectInputStream(f);
        RSAPrivateKey prk=(RSAPrivateKey)b.readObject( );
        BigInteger d=prk.getPrivateExponent();
        //获取私钥参数及解密
        BigInteger n=prk.getModulus();
        System.out.println("d= "+d);
        System.out.println("n= "+n);
        BigInteger m=c.modPow(d,n);
        //显示解密结果
        System.out.println("m= "+m);
        byte[] mt=m.toByteArray();
        System.out.println("PlainText is ");
        for(int i=0;i<mt.length;i++){
            System.out.print((char) mt[i]);
        }
    }
    }
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
import java.security.interfaces.*;
import java.math.*;
import java.io.*;
public class Enc_RSA{
    public static void main(String args[]) throws Exception{
        String s="Hello World!";
        // 获取公钥及参数e,n
        FileInputStream f=new FileInputStream("Skey_RSA_pub.dat");
        ObjectInputStream b=new ObjectInputStream(f);
        RSAPublicKey  pbk=(RSAPublicKey)b.readObject( );
        BigInteger e=pbk.getPublicExponent();
        BigInteger n=pbk.getModulus();
        System.out.println("e= "+e);
        System.out.println("n= "+n);
        // 明文 m
        byte ptext[]=s.getBytes("UTF8");
        BigInteger m=new BigInteger(ptext);
        // 计算密文c,打印
        BigInteger c=m.modPow(e,n);
        System.out.println("c= "+c);
        // 保存密文
        String cs=c.toString( );
        BufferedWriter out=
                new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream("Enc_RSA.dat")));
        out.write(cs,0,cs.length( ));
        out.close( );

    }
}
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class Skey_RSA{
    public static void main(String args[]) throws Exception{
        KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
        kpg.initialize(1024);
        KeyPair kp=kpg.genKeyPair();
        PublicKey pbkey=kp.getPublic();
        PrivateKey prkey=kp.getPrivate();
        //  保存公钥
        FileOutputStream  f1=new FileOutputStream("Skey_RSA_pub.dat");
        ObjectOutputStream b1=new  ObjectOutputStream(f1);
        b1.writeObject(pbkey);
        //  保存私钥
        FileOutputStream  f2=new FileOutputStream("Skey_RSA_priv.dat");
        ObjectOutputStream b2=new  ObjectOutputStream(f2);
        b2.writeObject(prkey);
    }
}
  • (4)使用密钥协定创建共享密钥
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);
   }      
}  

可能会有人问,为什么你要把所有的代码都粘这上面呢,是不是想要觉得博客长呢,在这里我给出两点原因:

  • 首先,我觉得,说不定以后我会用这个东西,但是又因为娄老师的博客太复杂,或许当时我只是需要代码,所以我觉得留下这一份可以给我以后复习或者以后再用留下资源;

  • 其次,是觉得博客长,但长又能说明什么呢,有些人宁愿少一点,也不愿自己再清楚一点,这些东西就像手里过一遍一样,就像那句老话“眼过千遍,不如手过一遍”,这样记录一遍,脑子里总会是留下影响的,会充实自己的。

三、实验过程中遇到的问题和解决过程

  • 问题1:本次实验的目的是什么呢,因为我发现,本次实验很多部分都是在复制粘贴代码,而且很多时候还有很多人并没有很仔细的去看娄老师博客里的一些很是细节的东西,那本次博客对于我们本身有什么好处?

  • 问题1解答:首先,我觉得本次实验一是为了让我们更加了解一些idea的功能,并且通过结对编程我们可以让我们集体的力量变得更加强大,通过码云的的共享,我们可以及时发现对方代码的问题并且及时为他指出或者点明,所以我觉得从了解层面是这样的;二是,因为我们这个专业不接触密码学,所以需要补充一些密码学相关的知识,驱使我们去了解密码学,让自己身为一名电科院学子竟然连密码学的概念都不了解一点,所以为了提高我们的基础学科能力,也是需要这样的一次实验去提升我们的自我知识和自我的学习能力。

其他(感悟、思考等)

  • 最近感觉任务的确是轻松了一点,但是仍旧不容小觑,因为马上要迎来复习阶段,所以如何让自己更快的投身自我复习和及时答辩的状态至关重要,希望大家要及时发现问题,及时纠正,不要把很多东西拖到事到临头的地步才感觉自己浑身上下难受,学会让自己更加充实地过好每一天,加油!

参考资料

Java 密码学算法
Java程序设计
蓝墨云
实验三 敏捷开发与XP实践

猜你喜欢

转载自www.cnblogs.com/qh45wangwenbin/p/9096524.html
今日推荐