0
点赞
收藏
分享

微信扫一扫

密码学概论课后题手写+JAVA代码复现

林肯公园_97cc 2022-03-17 阅读 86

这次一共五条题目感觉都还蛮有意思的

一.求最大公约数(1790,1066)

1.1手写答案

1.2代码实现

/**
 * @author Dionysus_xu
 * @create 2022-03-17-18:02
 */
public class s {
    public static void main(String[] args) {
        s s1 = new s();
        int max = s1.getMax(1790, 1066);
        System.out.println(max);
    }

    private int getMax(int a, int b) {
        int length = (a>b)?a:b;
        int max = 0;
        for (int i = 1; i < length; i++) {
            if(a % i ==0 && b % i == 0){
                max =(max > i )?max:i;
            }
        }
        return max;
    }
}

二.求15(mod 26)的乘法逆元

2.1手写答案

2.2代码实现

/**
 * @author Dionysus_xu
 * @create 2022-03-17-18:13
 */
public class s2 {
    public static void main(String[] args) {
        s2 s = new s2();
        s.findOne(26,15);
    }

    private void findOne(int i, int i1) {
        int MAX_VALUE =(i>i1)?i:i1;
        for (int j = 0; j < MAX_VALUE; j++) {
            for (int k = 0; k < MAX_VALUE; k++) {
                if(i * j - i1 * k==1){
                    System.out.println(i+"*"+j+"-"+ i1 + "*" + k + "==1");
                }
                if(i * j - i1 * k== -1){
                    System.out.println(i+"*"+j+"-"+ i1 + "*" + k + "== -1");
                }
            }
        }
    }
}

 三.求模5的平方剩余和平方非剩余

3.1手写过程

3.2代码实现

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Dionysus_xu
 * @create 2022-03-17-19:10
 */
public class s3 {
    public static void main(String[] args) {
        s3 s = new s3();
        s.getSqrtAndNonSqrt(11);
    }

    private void getSqrtAndNonSqrt(int i) {
        int temp =(int )(0.5 * (i-1));
        Collection c1 = new ArrayList();
        Collection c2 = new ArrayList();
        for (int j = 1; j < i; j++) {
            int random = j;
            random =(int)(Math.pow(j,temp));
            if(random%i == 1){
                c1.add(j);
            }else {
                c2.add(j);
            }
        }
        System.out.println("模"+i+"的平方剩余有:");
        for(Object o : c1){
            System.out.print(o+"\t");
        }
        System.out.println();
        System.out.println("模"+i+"的平方非剩余有:");
        for(Object o : c2){
            System.out.print(o+"\t");
        }
    }
}

 四.分组密码的主要工作模式

4.1手写过程

 五.Diff-Hellman算法问题

Diffe-Hellman算法是一种经典的密钥交换算法,请回答如下问题:

(1)请画出用户A和用户B之间交换共享密钥的交互图。

(2)设素数p=97,其本原元g=5,A和B选取的随机数分别为x=36,y=58,求共享密钥。

5.1手写过程

 5.2代码实现

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Dionysus_xu
 * @create 2022-03-17-19:10
 */
public class s3 {
    public static void main(String[] args) {
        s3 s = new s3();
        s.getSqrtAndNonSqrt(11);
    }

    private void getSqrtAndNonSqrt(int i) {
        int temp =(int )(0.5 * (i-1));
        Collection c1 = new ArrayList();
        Collection c2 = new ArrayList();
        for (int j = 1; j < i; j++) {
            int random = j;
            random =(int)(Math.pow(j,temp));
            if(random%i == 1){
                c1.add(j);
            }else {
                c2.add(j);
            }
        }
        System.out.println("模"+i+"的平方剩余有:");
        for(Object o : c1){
            System.out.print(o+"\t");
        }
        System.out.println();
        System.out.println("模"+i+"的平方非剩余有:");
        for(Object o : c2){
            System.out.print(o+"\t");
        }
    }
}
举报

相关推荐

0 条评论