dhkeypairgenerator.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 55 行

JAVA
55
字号
package org.bouncycastle.crypto.generators;import java.math.BigInteger;import java.security.SecureRandom;import org.bouncycastle.crypto.KeyGenerationParameters;import org.bouncycastle.crypto.AsymmetricCipherKeyPair;import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;import org.bouncycastle.crypto.params.DHParameters;import org.bouncycastle.crypto.params.DHPublicKeyParameters;import org.bouncycastle.crypto.params.DHPrivateKeyParameters;import org.bouncycastle.crypto.params.DHKeyGenerationParameters;/** * a Diffie-Helman key pair generator. * * This generates keys consistent for use in the MTI/A0 key agreement protocol * as described in "Handbook of Applied Cryptography", Pages 516-519. */public class DHKeyPairGenerator    implements AsymmetricCipherKeyPairGenerator{    private DHKeyGenerationParameters param;    public void init(        KeyGenerationParameters param)    {        this.param = (DHKeyGenerationParameters)param;    }    public AsymmetricCipherKeyPair generateKeyPair()    {        BigInteger      p, q, g, x, y;        int             qLength = param.getStrength() - 1;        DHParameters    dhParams = param.getParameters();        p = dhParams.getP();        g = dhParams.getG();            //        // calculate the private key        //        x = new BigInteger(qLength, param.getRandom());        //        // calculate the public key.        //        y = g.modPow(x, p);        return new AsymmetricCipherKeyPair(                new DHPublicKeyParameters(y, dhParams),                new DHPrivateKeyParameters(x, dhParams));    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?