下面列出了怎么用javax.crypto.spec.DHParameterSpec的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Return DH parameters for the given keylength. Uses cache if possible,
* generates new parameters and adds them to the cache otherwise.
*/
public static DHParameterSpec getDHParameterSpec(int keyLength,
SecureRandom random)
throws NoSuchAlgorithmException, InvalidParameterSpecException {
DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
if (spec != null) {
return spec;
}
AlgorithmParameterGenerator gen =
AlgorithmParameterGenerator.getInstance("DH");
gen.init(keyLength, random);
AlgorithmParameters params = gen.generateParameters();
spec = params.getParameterSpec(DHParameterSpec.class);
dhCache.put(Integer.valueOf(keyLength), spec);
return spec;
}
static DHParameterSpec getDHParameterSpec(NamedGroup namedGroup) {
if (namedGroup.type != NamedGroupType.NAMED_GROUP_FFDHE) {
throw new RuntimeException(
"Not a named DH group: " + namedGroup);
}
AlgorithmParameters params = namedGroupParams.get(namedGroup);
if (params == null) {
throw new RuntimeException(
"Not a supported DH named group: " + namedGroup);
}
try {
return params.getParameterSpec(DHParameterSpec.class);
} catch (InvalidParameterSpecException ipse) {
// should be unlikely
return getPredefinedDHParameterSpec(namedGroup);
}
}
/**
* Initializes this key pair generator for the specified parameter
* set and source of randomness.
*
* <p>The given parameter set contains the prime modulus, the base
* generator, and optionally the requested size in bits of the random
* exponent (private value).
*
* @param params the parameter set used to generate the key pair
* @param random the source of randomness
*
* @exception InvalidAlgorithmParameterException if the given parameters
* are inappropriate for this key pair generator
*/
public void initialize(AlgorithmParameterSpec algParams,
SecureRandom random) throws InvalidAlgorithmParameterException {
if (!(algParams instanceof DHParameterSpec)){
throw new InvalidAlgorithmParameterException
("Inappropriate parameter type");
}
params = (DHParameterSpec)algParams;
pSize = params.getP().bitLength();
try {
checkKeySize(pSize);
} catch (InvalidParameterException ipe) {
throw new InvalidAlgorithmParameterException(ipe.getMessage());
}
// exponent size is optional, could be 0
lSize = params.getL();
// Require exponentSize < primeSize
if ((lSize != 0) && (lSize > pSize)) {
throw new InvalidAlgorithmParameterException
("Exponent size must not be larger than modulus size");
}
this.random = random;
}
/**
* Return DH parameters for the given keylength. Uses cache if possible,
* generates new parameters and adds them to the cache otherwise.
*/
public static DHParameterSpec getDHParameterSpec(int keyLength,
SecureRandom random)
throws NoSuchAlgorithmException, InvalidParameterSpecException {
DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
if (spec != null) {
return spec;
}
AlgorithmParameterGenerator gen =
AlgorithmParameterGenerator.getInstance("DH");
gen.init(keyLength, random);
AlgorithmParameters params = gen.generateParameters();
spec = params.getParameterSpec(DHParameterSpec.class);
dhCache.put(Integer.valueOf(keyLength), spec);
return spec;
}
/**
* Creates a Diffie-Hellman key pair.
*
* @return dh keypair
*/
protected KeyPair generateKeyPair() {
KeyPair keyPair = null;
DHParameterSpec keySpec = new DHParameterSpec(DH_MODULUS, DH_BASE);
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
keyGen.initialize(keySpec);
keyPair = keyGen.generateKeyPair();
keyAgreement = KeyAgreement.getInstance("DH");
// key agreement is initialized with "this" ends private key
keyAgreement.init(keyPair.getPrivate());
} catch (Exception e) {
log.error("Error generating keypair", e);
}
return keyPair;
}
public void initialize(
AlgorithmParameterSpec params,
SecureRandom random)
throws InvalidAlgorithmParameterException
{
if (!(params instanceof DHParameterSpec))
{
throw new InvalidAlgorithmParameterException("parameter object not a DHParameterSpec");
}
DHParameterSpec dhParams = (DHParameterSpec)params;
param = new DHKeyGenerationParameters(random, new DHParameters(dhParams.getP(), dhParams.getG(), null, dhParams.getL()));
engine.init(param);
initialised = true;
}
/**
* Initializes this key pair generator for the specified parameter
* set and source of randomness.
*
* <p>The given parameter set contains the prime modulus, the base
* generator, and optionally the requested size in bits of the random
* exponent (private value).
*
* @param params the parameter set used to generate the key pair
* @param random the source of randomness
*
* @exception InvalidAlgorithmParameterException if the given parameters
* are inappropriate for this key pair generator
*/
public void initialize(AlgorithmParameterSpec algParams,
SecureRandom random) throws InvalidAlgorithmParameterException {
if (!(algParams instanceof DHParameterSpec)){
throw new InvalidAlgorithmParameterException
("Inappropriate parameter type");
}
params = (DHParameterSpec)algParams;
pSize = params.getP().bitLength();
try {
checkKeySize(pSize);
} catch (InvalidParameterException ipe) {
throw new InvalidAlgorithmParameterException(ipe.getMessage());
}
// exponent size is optional, could be 0
lSize = params.getL();
// Require exponentSize < primeSize
if ((lSize != 0) && (lSize > pSize)) {
throw new InvalidAlgorithmParameterException
("Exponent size must not be larger than modulus size");
}
this.random = random;
}
/**
* Initializes this key pair generator for the specified parameter
* set and source of randomness.
*
* <p>The given parameter set contains the prime modulus, the base
* generator, and optionally the requested size in bits of the random
* exponent (private value).
*
* @param algParams the parameter set used to generate the key pair
* @param random the source of randomness
*
* @exception InvalidAlgorithmParameterException if the given parameters
* are inappropriate for this key pair generator
*/
public void initialize(AlgorithmParameterSpec algParams,
SecureRandom random) throws InvalidAlgorithmParameterException {
if (!(algParams instanceof DHParameterSpec)){
throw new InvalidAlgorithmParameterException
("Inappropriate parameter type");
}
params = (DHParameterSpec)algParams;
pSize = params.getP().bitLength();
try {
checkKeySize(pSize);
} catch (InvalidParameterException ipe) {
throw new InvalidAlgorithmParameterException(ipe.getMessage());
}
// exponent size is optional, could be 0
lSize = params.getL();
// Require exponentSize < primeSize
if ((lSize != 0) && (lSize > pSize)) {
throw new InvalidAlgorithmParameterException
("Exponent size must not be larger than modulus size");
}
this.random = random;
}
/**
* Return DH parameters for the given keylength. Uses cache if possible,
* generates new parameters and adds them to the cache otherwise.
*/
public static DHParameterSpec getDHParameterSpec(int keyLength,
SecureRandom random)
throws NoSuchAlgorithmException, InvalidParameterSpecException {
DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
if (spec != null) {
return spec;
}
AlgorithmParameterGenerator gen =
AlgorithmParameterGenerator.getInstance("DH");
gen.init(keyLength, random);
AlgorithmParameters params = gen.generateParameters();
spec = params.getParameterSpec(DHParameterSpec.class);
dhCache.put(Integer.valueOf(keyLength), spec);
return spec;
}
/**
* Return DH parameters for the given keylength. Uses cache if possible,
* generates new parameters and adds them to the cache otherwise.
*/
public static DHParameterSpec getDHParameterSpec(int keyLength,
SecureRandom random)
throws NoSuchAlgorithmException, InvalidParameterSpecException {
DHParameterSpec spec = getCachedDHParameterSpec(keyLength);
if (spec != null) {
return spec;
}
AlgorithmParameterGenerator gen =
AlgorithmParameterGenerator.getInstance("DH");
gen.init(keyLength, random);
AlgorithmParameters params = gen.generateParameters();
spec = params.getParameterSpec(DHParameterSpec.class);
dhCache.put(Integer.valueOf(keyLength), spec);
return spec;
}
/**
* Initializes this key pair generator for the specified parameter
* set and source of randomness.
*
* <p>The given parameter set contains the prime modulus, the base
* generator, and optionally the requested size in bits of the random
* exponent (private value).
*
* @param algParams the parameter set used to generate the key pair
* @param random the source of randomness
*
* @exception InvalidAlgorithmParameterException if the given parameters
* are inappropriate for this key pair generator
*/
public void initialize(AlgorithmParameterSpec algParams,
SecureRandom random) throws InvalidAlgorithmParameterException {
if (!(algParams instanceof DHParameterSpec)){
throw new InvalidAlgorithmParameterException
("Inappropriate parameter type");
}
params = (DHParameterSpec)algParams;
pSize = params.getP().bitLength();
try {
checkKeySize(pSize);
} catch (InvalidParameterException ipe) {
throw new InvalidAlgorithmParameterException(ipe.getMessage());
}
// exponent size is optional, could be 0
lSize = params.getL();
// Require exponentSize < primeSize
if ((lSize != 0) && (lSize > pSize)) {
throw new InvalidAlgorithmParameterException
("Exponent size must not be larger than modulus size");
}
this.random = random;
}
protected void engineInit(AlgorithmParameterSpec paramSpec)
throws InvalidParameterSpecException {
if (!(paramSpec instanceof DHParameterSpec)) {
throw new InvalidParameterSpecException
("Inappropriate parameter specification");
}
this.p = ((DHParameterSpec)paramSpec).getP();
this.g = ((DHParameterSpec)paramSpec).getG();
this.l = ((DHParameterSpec)paramSpec).getL();
}
protected void engineInit(
Key key,
AlgorithmParameterSpec params,
SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
if (!(key instanceof DHPrivateKey))
{
throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
}
DHPrivateKey privKey = (DHPrivateKey)key;
if (params != null)
{
if (!(params instanceof DHParameterSpec))
{
throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
}
DHParameterSpec p = (DHParameterSpec)params;
this.p = p.getP();
this.g = p.getG();
}
else
{
this.p = privKey.getParams().getP();
this.g = privKey.getParams().getG();
}
this.x = this.result = privKey.getX();
}
private static DHPublicKeySpec getDHPublicKeySpec(PublicKey key) {
if (key instanceof DHPublicKey) {
DHPublicKey dhKey = (DHPublicKey)key;
DHParameterSpec params = dhKey.getParams();
return new DHPublicKeySpec(dhKey.getY(),
params.getP(), params.getG());
}
try {
KeyFactory factory = JsseJce.getKeyFactory("DiffieHellman");
return factory.getKeySpec(key, DHPublicKeySpec.class);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
// unlikely
throw new RuntimeException("Unable to get DHPublicKeySpec", e);
}
}
/**
* Returns whether the Diffie-Hellman public key is valid or not.
*
* Per RFC 2631 and NIST SP800-56A, the following algorithm is used to
* validate Diffie-Hellman public keys:
* 1. Verify that y lies within the interval [2,p-1]. If it does not,
* the key is invalid.
* 2. Compute y^q mod p. If the result == 1, the key is valid.
* Otherwise the key is invalid.
*/
private static void validateDHPublicKey(DHPublicKey publicKey)
throws InvalidKeyException {
DHParameterSpec paramSpec = publicKey.getParams();
BigInteger p = paramSpec.getP();
BigInteger g = paramSpec.getG();
BigInteger y = publicKey.getY();
validateDHPublicKey(p, g, y);
}
/**
* Initializes this key pair generator for the specified parameter
* set and source of randomness.
*
* <p>The given parameter set contains the prime modulus, the base
* generator, and optionally the requested size in bits of the random
* exponent (private value).
*
* @param params the parameter set used to generate the key pair
* @param random the source of randomness
*
* @exception InvalidAlgorithmParameterException if the given parameters
* are inappropriate for this key pair generator
*/
public void initialize(AlgorithmParameterSpec algParams,
SecureRandom random) throws InvalidAlgorithmParameterException {
if (!(algParams instanceof DHParameterSpec)){
throw new InvalidAlgorithmParameterException
("Inappropriate parameter type");
}
params = (DHParameterSpec)algParams;
pSize = params.getP().bitLength();
if ((pSize < 512) || (pSize > 2048) ||
(pSize % 64 != 0)) {
throw new InvalidAlgorithmParameterException
("Prime size must be multiple of 64, and can only range "
+ "from 512 to 2048 (inclusive)");
}
// exponent size is optional, could be 0
lSize = params.getL();
// Require exponentSize < primeSize
if ((lSize != 0) && (lSize > pSize)) {
throw new InvalidAlgorithmParameterException
("Exponent size must not be larger than modulus size");
}
this.random = random;
}
protected <T extends AlgorithmParameterSpec>
T engineGetParameterSpec(Class<T> paramSpec)
throws InvalidParameterSpecException {
if (DHParameterSpec.class.isAssignableFrom(paramSpec)) {
return paramSpec.cast(new DHParameterSpec(this.p, this.g, this.l));
} else {
throw new InvalidParameterSpecException
("Inappropriate parameter Specification");
}
}
/**
* Returns the key parameters.
*
* @return the key parameters
*/
public DHParameterSpec getParams() {
if (this.l != 0) {
return new DHParameterSpec(this.p, this.g, this.l);
} else {
return new DHParameterSpec(this.p, this.g);
}
}
public void testGenerateKeyPairSha1Default()
{
DHParameterSpec parameterSpec = DiffieHellmanSession.getDefaultParameter();
KeyPair keyPair = DiffieHellmanSession.generateKeyPair(parameterSpec);
assertNotNull(keyPair);
}
protected <T extends AlgorithmParameterSpec>
T engineGetParameterSpec(Class<T> paramSpec)
throws InvalidParameterSpecException {
if (DHParameterSpec.class.isAssignableFrom(paramSpec)) {
return paramSpec.cast(new DHParameterSpec(this.p, this.g, this.l));
} else {
throw new InvalidParameterSpecException
("Inappropriate parameter Specification");
}
}
protected void engineInit(AlgorithmParameterSpec paramSpec)
throws InvalidParameterSpecException {
if (!(paramSpec instanceof DHParameterSpec)) {
throw new InvalidParameterSpecException
("Inappropriate parameter specification");
}
this.p = ((DHParameterSpec)paramSpec).getP();
this.g = ((DHParameterSpec)paramSpec).getG();
this.l = ((DHParameterSpec)paramSpec).getL();
}
/**
* Returns the key parameters.
*
* @return the key parameters
*/
public DHParameterSpec getParams() {
if (this.l != 0) {
return new DHParameterSpec(this.p, this.g, this.l);
} else {
return new DHParameterSpec(this.p, this.g);
}
}
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof javax.crypto.interfaces.DHPrivateKey)) {
return false;
}
javax.crypto.interfaces.DHPrivateKey other =
(javax.crypto.interfaces.DHPrivateKey) obj;
DHParameterSpec otherParams = other.getParams();
return ((this.x.compareTo(other.getX()) == 0) &&
(this.p.compareTo(otherParams.getP()) == 0) &&
(this.g.compareTo(otherParams.getG()) == 0));
}
/**
* Returns the key parameters.
*
* @return the key parameters
*/
public DHParameterSpec getParams() {
if (this.l != 0) {
return new DHParameterSpec(this.p, this.g, this.l);
} else {
return new DHParameterSpec(this.p, this.g);
}
}
protected void engineInit(AlgorithmParameterSpec paramSpec)
throws InvalidParameterSpecException {
if (!(paramSpec instanceof DHParameterSpec)) {
throw new InvalidParameterSpecException
("Inappropriate parameter specification");
}
this.p = ((DHParameterSpec)paramSpec).getP();
this.g = ((DHParameterSpec)paramSpec).getG();
this.l = ((DHParameterSpec)paramSpec).getL();
}
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof javax.crypto.interfaces.DHPublicKey)) {
return false;
}
javax.crypto.interfaces.DHPublicKey other =
(javax.crypto.interfaces.DHPublicKey) obj;
DHParameterSpec otherParams = other.getParams();
return ((this.y.compareTo(other.getY()) == 0) &&
(this.p.compareTo(otherParams.getP()) == 0) &&
(this.g.compareTo(otherParams.getG()) == 0));
}
protected AlgorithmParameterSpec localEngineGetParameterSpec(
Class paramSpec)
throws InvalidParameterSpecException
{
if (paramSpec == ElGamalParameterSpec.class)
{
return currentSpec;
}
else if (paramSpec == DHParameterSpec.class)
{
return new DHParameterSpec(currentSpec.getP(), currentSpec.getG());
}
throw new InvalidParameterSpecException("unknown parameter spec passed to ElGamal parameters object.");
}
protected void engineInit(AlgorithmParameterSpec paramSpec)
throws InvalidParameterSpecException {
if (!(paramSpec instanceof DHParameterSpec)) {
throw new InvalidParameterSpecException
("Inappropriate parameter specification");
}
this.p = ((DHParameterSpec)paramSpec).getP();
this.g = ((DHParameterSpec)paramSpec).getG();
this.l = ((DHParameterSpec)paramSpec).getL();
}
protected AlgorithmParameters engineGenerateParameters()
{
ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
if (random != null)
{
pGen.init(strength, 20, random);
}
else
{
pGen.init(strength, 20, new SecureRandom());
}
ElGamalParameters p = pGen.generateParameters();
AlgorithmParameters params;
try
{
params = createParametersInstance("ElGamal");
params.init(new DHParameterSpec(p.getP(), p.getG(), l));
}
catch (Exception e)
{
throw new RuntimeException(e.getMessage());
}
return params;
}
protected AlgorithmParameterSpec localEngineGetParameterSpec(
Class paramSpec)
throws InvalidParameterSpecException
{
if (paramSpec == DHParameterSpec.class)
{
return currentSpec;
}
throw new InvalidParameterSpecException("unknown parameter spec passed to DH parameters object.");
}