PKCS#3 v1.4 java 语言实现参考

原创 2012年03月28日 10:44:46

再接再厉,用java实现了PKCS#3 的Diffie-Hellman,这个类比较简单,就不提供下载了,直接粘在下面:


package com.broadthinking.pkcs.pkcs_3;

import java.math.BigInteger;
import java.util.Random;

/**
 * This standard describes a method for implementing Diffie-Hellman key
 * agreement, whereby two parties, without any prior arrangements, can agree
 * upon a secret key that is known only to them (and, in particular, is not
 * known to an eavesdropper listening to the dialogue by which the parties agree
 * on the key). This secret key can then be used, for example, to encrypt
 * further communications between the parties.
 * 
 * @author CaesarZou
 * 
 */
public class Diffie_Hellman {
	
	private Diffie_Hellman(BigInteger p, BigInteger g, BigInteger x, BigInteger y) {
		this.p = p;
		this.g = g;
		this.x = x;
		this.y = y;
	}
	
	/**
	 * Generate a Diffie_Hellman party by input the common args
	 * 
	 * @param p
	 * @param g
	 * @param l
	 * @return instance of Diffe_Hellman
	 * @throws PKCS3Exception 
	 */
	public static Diffie_Hellman parameterGeneration(BigInteger p, BigInteger g, int l) throws PKCS3Exception {
		BigInteger TWO = BigInteger.valueOf(2);
		//A central authority shall select an odd prime p. 
		if(!p.isProbablePrime(100)) {
			throw new PKCS3Exception(PKCS3Exception.ERROR_PARAMTER);
		}
		
		//The central authority shall also select an integer g, the base, that satisfies 0 < g < p. 
		if(!(g.compareTo(BigInteger.ZERO)>0 && g.compareTo(p)<0)) {
			throw new PKCS3Exception(PKCS3Exception.ERROR_PARAMTER);
		}
		
		//The central authority may optionally select an integer l, the private-value length in bits, 
		//that satisfies 2^(l-1) <= p.
		BigInteger bl = null;
		
		if(l>0) {
			bl = TWO.pow(l-1);
			if(bl.compareTo(p)>0) {
				throw new PKCS3Exception(PKCS3Exception.ERROR_PARAMTER);
			}
		}
		
		//The length of the prime p in octets is the integer k satisfying2^8(k−1) <= p < 2^8k .

		//7.1 Private-value generation
		//An integer x, the private value, shall be generated privately and randomly. 
		//This integer shall satisfy 0 < x < p−1, 
		BigInteger leftExtent = BigInteger.ZERO;
		BigInteger rightExtent = p.subtract(BigInteger.ONE);		
		
		if(l>0) {
			//unless the central authority specifies a private- value length l, 
			//in which case the integer shall satisfy 2l-1 ≤ x < 2l.
			leftExtent = bl;
			BigInteger rightExtent2 = TWO.pow(l);
			if(rightExtent2.compareTo(rightExtent)<0) {
				rightExtent = rightExtent2;
			}
		}
		
		//to make a random x between leftExtent and rightExtent
		//let range = (rightExtend-leftExtend)
		//let x = leftExtend + random(range.length)%range
		BigInteger range = rightExtent.subtract(leftExtent);
		BigInteger x = new BigInteger(range.bitLength(),new Random());
		x = x.mod(range);
		x = x.add(leftExtent);
		
		//7.2 Exponentiation
		//The base g shall be raised to the private value x modulo p to give an integer y,
		//the integer public value.
		//y = g^x mod p, 0 < y < p .
		//This is the classic discrete-exponentiation computation
		BigInteger y = g.modPow(x, p);
		
		return new Diffie_Hellman(p,g,x,y);
	}
	
	/**
	 * get public value
	 * @return
	 */
	public BigInteger getY() {
		return this.y;
	}
	
	/**
	 * calc secret key by other party's public value
	 * @param y
	 * @return z
	 */
	public BigInteger getZ(BigInteger y) {
		/*
		 * z = (y')x mod p, 0 < z < p .
		 */
		return y.modPow(this.x, this.p);
	}
	
	//
	BigInteger x;
	BigInteger y;
	BigInteger p;
	BigInteger g;

}


package com.broadthinking.pkcs.pkcs_3.test;

import java.math.BigInteger;

import com.broadthinking.pkcs.pkcs_3.Diffie_Hellman;
import com.broadthinking.pkcs.pkcs_3.PKCS3Exception;

public class TestVect {
	
	public static void main(String [] args) throws PKCS3Exception {
		BigInteger p = new BigInteger(new byte [] {
				(byte)0x00,
				(byte)0xee, (byte)0xcf, (byte)0xae, (byte)0x81, (byte)0xb1, (byte)0xb9, (byte)0xb3, (byte)0xc9, (byte)0x08, (byte)0x81, (byte)0x0b, (byte)0x10, (byte)0xa1, (byte)0xb5, (byte)0x60, (byte)0x01,
				(byte)0x99, (byte)0xeb, (byte)0x9f, (byte)0x44, (byte)0xae, (byte)0xf4, (byte)0xfd, (byte)0xa4, (byte)0x93, (byte)0xb8, (byte)0x1a, (byte)0x9e, (byte)0x3d, (byte)0x84, (byte)0xf6, (byte)0x32,
				(byte)0x12, (byte)0x4e, (byte)0xf0, (byte)0x23, (byte)0x6e, (byte)0x5d, (byte)0x1e, (byte)0x3b, (byte)0x7e, (byte)0x28, (byte)0xfa, (byte)0xe7, (byte)0xaa, (byte)0x04, (byte)0x0a, (byte)0x2d,
				(byte)0x5b, (byte)0x25, (byte)0x21, (byte)0x76, (byte)0x45, (byte)0x9d, (byte)0x1f, (byte)0x39, (byte)0x75, (byte)0x41, (byte)0xba, (byte)0x2a, (byte)0x58, (byte)0xfb, (byte)0x65, (byte)0x99
		});
		BigInteger g = new BigInteger(new byte [] {
				(byte)0x00,
				(byte)0xb0, (byte)0x6c, (byte)0x4f, (byte)0xda, (byte)0xbb, (byte)0x63, (byte)0x01, (byte)0x19, (byte)0x8d, (byte)0x26, (byte)0x5b, (byte)0xdb, (byte)0xae, (byte)0x94, (byte)0x23, (byte)0xb3,
				(byte)0x80, (byte)0xf2, (byte)0x71, (byte)0xf7, (byte)0x34, (byte)0x53, (byte)0x88, (byte)0x50, (byte)0x93, (byte)0x07, (byte)0x7f, (byte)0xcd, (byte)0x39, (byte)0xe2, (byte)0x11, (byte)0x9f,
				(byte)0xc9, (byte)0x86, (byte)0x32, (byte)0x15, (byte)0x4f, (byte)0x58, (byte)0x83, (byte)0xb1, (byte)0x67, (byte)0xa9, (byte)0x67, (byte)0xbf, (byte)0x40, (byte)0x2b, (byte)0x4e, (byte)0x9e,
				(byte)0x2e, (byte)0x0f, (byte)0x96, (byte)0x56, (byte)0xe6, (byte)0x98, (byte)0xea, (byte)0x36, (byte)0x66, (byte)0xed, (byte)0xfb, (byte)0x25, (byte)0x79, (byte)0x80, (byte)0x39, (byte)0xf7	
		});
		//test two part use same p,g without l limit
		{
			Diffie_Hellman part1 = Diffie_Hellman.parameterGeneration(p, g, -1);
			Diffie_Hellman part2 = Diffie_Hellman.parameterGeneration(p, g, -1);
			
			BigInteger z1 = part1.getZ(part2.getY());
			BigInteger z2 = part2.getZ(part1.getY());
			
			if(z1.compareTo(z2)==0) {
				System.out.println("success!");
			}else {
				System.out.println("error!");
			}
		}
		//test two part use same p,g with l limit
		{
			Diffie_Hellman part1 = Diffie_Hellman.parameterGeneration(p, g, 500);
			Diffie_Hellman part2 = Diffie_Hellman.parameterGeneration(p, g, 500);
			
			BigInteger z1 = part1.getZ(part2.getY());
			BigInteger z2 = part2.getZ(part1.getY());
			
			if(z1.compareTo(z2)==0) {
				System.out.println("success!");
			}else {
				System.out.println("error!");
			}
		}
	}
	
}




相关文章推荐

PKCS#1 v2.1 java 语言实现参考

PKCS#1 v2.1 逐渐已经替代 PKCS#1 v1.5成为了应用选择的主要加密/签名方案。近期看了一下这规范。发现新增的OAEP加密、PSS签名网上的资源不是很多。所以弄了这么个工程,一个目的是...

Java 语言导学(3版) PDG

  • 2010年07月31日 11:29
  • 8.09MB
  • 下载

使用 ASM 实现 Java 语言的“多重继承”

问题的提出 在大部分情况下,需要多重继承往往意味着糟糕的设计。但在处理一些遗留项目的时候,多重继承可能是我们能做出的选择中代价最小的。由于 Java 语言本身不支持多重继承,这常常会给我们带来麻烦,...

Windows 平台上的使用 Java 语言实现 appium 自动化程序 for Android(完整版)

首先说下楼主也是个新手,摸索了好几天天 终于完成了第一个appium程序,帖子写的不合理的地方,请大家轻喷,指导,大家一起学习。写这个帖子就是为了方便新手能快速入门,也算是自己的一种巩固吧。好了,开始...
  • wanglha
  • wanglha
  • 2014年12月31日 13:20
  • 659

Java 语言使用 Observer/Observable 实现简单的观察者模式

为了更好的理解什么是观察者模式,下面列举壹些可能用到该模式的情形或例子: (1)周期性任务。比如linux中的周期性任务命令crontab命令,win7 下的定时关机命令shutdown -s -t ...

泛型归并排序(java 语言实现)

以下实例实现了 1. 实现了Comparable 接口的基本类型的泛型排序,2. 复杂或者说自定义对象的泛型排序,使用 Comparator 接口定制的比较器泛型归并排序主要代码package com...
  • stuShan
  • stuShan
  • 2016年04月22日 10:57
  • 362

堆排序(java 语言实现)

可以用 数组或者线性表实现 Heap ,关键是理清楚 当前节点的坐标和父节点的坐标以及左右孩子的坐标的关系,比如 当前坐标是 i 其他节点的坐标如何表示。然后就是添加删除的原则。package co...
  • stuShan
  • stuShan
  • 2016年04月22日 11:16
  • 173

(转)Java 语言实现的八大排序算法

1 直接插入排序  基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数也是排好顺序的。如此反复循环,直到全部...

反转单向链表 (java 语言实现)

带泛型的实现:package dan;/** * @author shan 定义一个单链表 */class Node { // 变量 private E record; /...
  • stuShan
  • stuShan
  • 2016年04月22日 11:08
  • 267

归并算法(java 语言实现)

package com.shan.mergeSort.improve;import org.junit.Test;public class Merge { private static Comp...
  • stuShan
  • stuShan
  • 2016年04月22日 10:45
  • 347
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:PKCS#3 v1.4 java 语言实现参考
举报原因:
原因补充:

(最多只允许输入30个字)