Java 集合练习数据加密

需求:

某个公司采用公用电话传递数据,数据是8位的整数,在传递过程中是加密的,加密规则如下:每 位数字都加上5,
然后用和除以10的余数代替该数字,再将第1位和第8位交换,第2位和第7位交换以此类推。

代码实现:

public class Test3 {
    @Test
    public void test1(){
        //定义集合用来存放原数据
        ArrayList<Integer> list = new ArrayList<>();
        ArrayList<Integer> l = getList(list);//获取原数据集合
        System.out.println("原数据为: "+l);
        ArrayList<Integer> encipherList = encipherNum(l);//加密后的数据集合
        System.out.println("加密后的数据为: "+encipherList);

    }
    //定义方法获取数据
    public ArrayList<Integer> getList(ArrayList<Integer> list) {
        //创建随机数模拟传递的8位数数据
        Random r = new Random();
        for (int i = 0; i < 8; i++) {
            int num = r.nextInt(10);//每个位置的数都是0-9的随机的数字
            list.add(num);
        }
        return  list;
    }

    //定义方法加密数据
    public ArrayList<Integer> encipherNum(ArrayList<Integer> list) {
        ArrayList<Integer> encipherList = new ArrayList<>();//定义集合保存加密后的数据
        for (Integer i : list) {
              encipherList.add((i+5)%10);
        }
        Collections.reverse(encipherList);//将集合中的元素位置反转
        return  encipherList;

    }

运行结果:

猜你喜欢

转载自blog.csdn.net/xilin6664/article/details/89403986