📄 dhkeypairgenerator.java
字号:
package au.net.aba.crypto.provider;
/*
* $Id: DHKeyPairGenerator.java,v 1.7 1999/02/18 07:04:09 leachbj Exp $
* $Author: leachbj $
*
* Copyright (C) 1996-1998 Australian Business Access Pty Ltd.
* All rights reserved.
*
* Use, modification, copying and distribution of this software is subject the
* terms and conditions of the ABA Public Licence. See the file
* "PUBLIC_LICENCE" for additional information.
*
* If you have not received a copy of the Public Licence, you must destroy all
* copies of this file immediately.
*
* $Source: /aba/CVSROOT/jdk1.1/src/au.net.aba/crypto/provider/DHKeyPairGenerator.java,v $
* $Revision: 1.7 $
* $Date: 1999/02/18 07:04:09 $
* $State: Exp $
*/
import java.security.*;
import java.math.*;
import javax.crypto.spec.*;
import java.security.*;
import java.security.spec.*;
/**
* a key pair generator for Diffie-Hellman keys (note: incomplete).
*/
public class DHKeyPairGenerator extends KeyPairGenerator
{
public final static String ident = "$Id: DHKeyPairGenerator.java,v 1.7 1999/02/18 07:04:09 leachbj Exp $";
private BigInteger y;
private BigInteger x;
private BigInteger p;
private BigInteger g;
private int primeSize;
private int exponentSize;
private SecureRandom random;
/**
* basic constructor
*/
public DHKeyPairGenerator()
{
super("DH");
primeSize = 512;
exponentSize = 504;
}
/**
* return a new Diffie-Hellman key pair.
*
* @return a Diffie-Hellman key pair.
*/
public KeyPair generateKeyPair()
{
KeyPair keyPair = null;
if (exponentSize == 0)
{
exponentSize = primeSize - 1;
}
if (p == null || g == null)
{
throw new IllegalStateException(
"generator uninitialised");
}
x = new BigInteger(exponentSize, random);
y = g.modPow(x, p);
return new KeyPair(
new DHPubKey(y, p, g, exponentSize),
new DHPrivKey(x, p, g, exponentSize));
}
/**
* initialise the key pair generator with the given prime size
* and random byte source.
*
* @param spec a parameter spec.
* @param random a source of random bytes.
* @exception InvalidAlgorithmParameterException if the params argument
* is invalid.
*/
public void initialise(
AlgorithmParameterSpec spec,
SecureRandom random)
throws InvalidAlgorithmParameterException
{
if (!(spec instanceof DHParameterSpec))
{
throw new InvalidAlgorithmParameterException(
"Bad parameter spec");
}
this.random = random;
p = ((DHParameterSpec)spec).getP();
g = ((DHParameterSpec)spec).getG();
primeSize = p.bitLength();
exponentSize = ((DHParameterSpec)spec).getL();
if (exponentSize != 0 && exponentSize >= primeSize)
{
throw new InvalidAlgorithmParameterException(
"Exponent must be smaller than modulus");
}
}
/**
* initialise the key pair generator with the given prime size
* and random byte source.
*
* @param primeSize prime number size.
* @param random a source of random bytes.
*/
public void initialize(
int primeSize,
SecureRandom random)
{
this.primeSize = primeSize;
exponentSize = 0;
this.random = random;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -