这次一共五条题目感觉都还蛮有意思的
一.求最大公约数(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代码实现
/**
* @author Dionysus_xu
* @create 2022-03-17-20:23
*/
public class s5 {
public static void main(String[] args) {
s5 s = new s5();
s.getPublicKey(97,5,36,58);
}
public void getPublicKey(int p,int i, int x,int y){
//获取A,B各自的明文
int A =(int)(Math.pow(i,x)%p);//A = i^x modp
int B =(int)(Math.pow(i,y)%p);//B = i^y modp
//加密公钥
int k1 = (int) (Math.pow(A,y)%p);//k1 = A^y modp
int k2 = (int) (Math.pow(B,x)%p);//k2 = B^x modp
//按道理这里是相等的
if(k1 == k2){
System.out.println("您的秘钥为:"+k1);
}
}
}