密码学_Diffie-Hellmen随机生成大素数(java实现)

d_h随机生成大素数

介绍

1.DH密钥交换过程
(1)通信双方首先交换素数p和本原元(原根)g,及素数p和本原元g是公开的整数。
(2)用户A选择一个随机整数x<p,计算XA=gˣ mod p,并XA将发送给用户B。
(3)用户B选择一个随机整数y<p,计算YB=gʸ mod p,并YB将发送给用户A。
(4)用户A计算KA=YBˣ mod p =g ˣʸ mod p,并将KA作为密钥。
(5)用户B计算KB=XAʸmod p =gˣʸ mod p,并将KB作为密钥。
(6)可见K=KA=KB,用户A与用户B完成密钥交换。
在这里插入图片描述

算法的关键就是利用公式求解XA和YB从而再求解得到KA和KB如果两者相等则得到共享密钥K从而完成密钥的交换

代码讲解

由程序生成随机数:

  • 1)生成随机数p、g、a、b
     public static BigInteger[] generator() {
		Random rnd=new Random();
		BigInteger p,q,g,x,y;
		while(true) {
			q=BigInteger.probablePrime(1020, rnd);   //1024
			p=q.multiply(BigInteger.valueOf(2)).add(BigInteger.valueOf(1));
			if(p.isProbablePrime(20)) break;
		}
		while(true) {
			g=BigInteger.probablePrime(512, rnd);   //512
			x=g.multiply(g).mod(p);
			if(x.equals(BigInteger.valueOf(1))==false) {  //&& y.equals(BigInteger.valueOf(1))==false
				break;
			}
		}
		BigInteger[] p_g= new  BigInteger[2];
		p_g[0]=p;p_g[1]=g;
		return p_g;
	}
  • 2)计算Xa、Yb、Ka、Kb
     // 计算公式G=(g^n)%p
     public static BigInteger Mod(BigInteger g, BigInteger n, BigInteger p) {
     	BigInteger G = new BigInteger("1");
     	int n1 = n.intValue();
         for (int i = 0; i < n1; i++) {
            G = (G.multiply(g).remainder(p));
        }
        return G;
     }
  • 3)算法实现流程图
    在这里插入图片描述

完整代码

  • 随机生成大素数代码
import java.math.BigInteger;
import java.util.*;

import AES1_fenzu.AES;

public class D_Hellman_Demo {
    // 计算公式G=(g^n)%p
    public static BigInteger Mod(BigInteger g, BigInteger n, BigInteger p) {
    	BigInteger G = new BigInteger("1");
    	int n1 = n.intValue();
    	//System.out.println(n1);
        for (int i = 0; i < n1; i++) {
            G = (G.multiply(g).remainder(p));
        }
        return G;
    }

    public static BigInteger[] generator() {
		Random rnd=new Random();
		BigInteger p,q,g,x,y;
		while(true) {
			q=BigInteger.probablePrime(1024, rnd);   //1024
			p=q.multiply(BigInteger.valueOf(2)).add(BigInteger.valueOf(1));
			if(p.isProbablePrime(20)) break;
		}
		
		while(true) {
			g=BigInteger.probablePrime(512, rnd);   //512
			//System.out.println(g);
			x=g.multiply(g).mod(p);
			//y = g.pow(q.intValue()).mod(p);
			if(x.equals(BigInteger.valueOf(1))==false) {  //&& y.equals(BigInteger.valueOf(1))==false
				break;
			}
		}
		BigInteger[] p_g= new  BigInteger[2];
		p_g[0]=p;p_g[1]=g;
		return p_g;
	}


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        BigInteger[] p_g = generator();
        BigInteger p = p_g[0];
        BigInteger g = p_g[1];
        System.out.println("p="+p_g[0]+"\ng="+p_g[1]);
        
        Random rnd=new Random();
        BigInteger a = BigInteger.probablePrime(10, rnd);
        BigInteger b = BigInteger.probablePrime(10, rnd);
        
        System.out.println("生成的随机数(a,b):"+a+" "+b);

        BigInteger Xa = Mod(g, a, p);
        System.out.print("得A的公钥Xa= ");
        System.out.print(Xa);
        System.out.println();

        BigInteger Yb = Mod(g, b, p);
        System.out.print("得B的公钥Yb= ");
        System.out.print(Yb);
        System.out.println();

        System.out.print("A得到密钥K1= ");
        BigInteger K1 = Mod(Yb, a, p);
        System.out.print(K1);
        System.out.println();
        System.out.print("B得到密钥K2= ");
        BigInteger K2 = Mod(Xa, b, p);
        System.out.print(K2);
        System.out.println();
        if(K1.compareTo(K2)==0){
            System.out.println("K1=K2,分配密钥成功\n共享密钥K="+K1);
        }
        else {
            System.out.println("K1!=K2,分配密钥失败!!!");
        }
    }
}

部分运行结果图

  • 随机生成大素数代码
    在这里插入图片描述

总结

此篇简单介绍了Diffie-Hellmen算法,并用java对随机生成p/q算法进行了实现,希望可以帮助到大家!
其中生成大素数的方法参考了https://blog.csdn.net/qq_37685156/article/details/88190088
如有问题,欢迎评论区留言或者私信!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值