类javax.crypto.interfaces.DHPrivateKey源码实例Demo

下面列出了怎么用javax.crypto.interfaces.DHPrivateKey的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: protools   文件: ToolDH.java
/**
 * 初始化甲方密钥
 *
 * @return Map 甲方密钥Map
 *
 * @throws Exception
 */
public static Map<String, Object> initKey() throws NoSuchAlgorithmException {

    // 实例化密钥对生成器
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);

    // 初始化密钥对生成器
    keyPairGenerator.initialize(KEY_SIZE);

    // 生成密钥对
    KeyPair keyPair = keyPairGenerator.generateKeyPair();

    // 甲方公钥
    DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic();

    // 甲方私钥
    DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();

    // 将密钥对存储在Map中
    Map<String, Object> keyMap = Maps.newHashMapWithExpectedSize(2);

    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);

    return keyMap;
}
 
源代码2 项目: TorrentEngine   文件: DHUtil.java
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }
                    
    throw new InvalidKeyException("can't identify DH private key.");
}
 
源代码3 项目: wycheproof   文件: DhTest.java
/**
 * Tests the default Diffie-Hellman key pair generation.
 *
 * <p>This test uses NIST SP 800-57 part1, revision 4
 * http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r4.pdf . Table 2 on page
 * 53 recommends 2048 bits as the minimal key length for Diffie-Hellman for new keys that expire
 * before the year 2030.
 *
 * <p>Note that JCE documentation is outdated. According to
 * https://docs.oracle.com/javase/7/docs/api/java/security/KeyPairGenerator.html an implementation
 * of the Java platform is only required to support 1024 bit keys.
 */
@NoPresubmitTest(
  providers = {ProviderType.OPENJDK, ProviderType.BOUNCY_CASTLE},
  bugs = {"b/33190860", "b/33190677"}
)
@SlowTest(providers = {ProviderType.BOUNCY_CASTLE, ProviderType.SPONGY_CASTLE})
@Test
public void testDefaultKeyPairGenerator() throws Exception {
  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
  KeyPair keyPair;
  try {
    keyPair = keyGen.generateKeyPair();
  } catch (Exception ex) {
    // When a provider decides not to implement a default key size then this is still better than
    // implementing a default that is out of date. Hence the test should not fail in this case.
    System.out.println("Cannot generate a DH key without initialize: " + ex.getMessage());
    return;
  }
  DHPrivateKey priv = (DHPrivateKey) keyPair.getPrivate();
  int keySize = priv.getParams().getP().bitLength();
  assertTrue("Default key size for DH is too small. Key size = " + keySize, keySize >= 2048);
  testKeyPair(keyPair, keySize);
}
 
源代码4 项目: statelearner   文件: TLSTestService.java
public void loadServerKey() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException, NoSuchProviderException, InvalidAlgorithmParameterException, InvalidKeySpecException {
		char[] password = KEYSTORE_PASSWORD.toCharArray();

		FileInputStream fIn = new FileInputStream(KEYSTORE_FILENAME);
		KeyStore keystore = KeyStore.getInstance("JKS");

		keystore.load(fIn, password);
		serverCertificate = (X509Certificate) keystore.getCertificate("server");
		serverPrivateKey  = (PrivateKey) keystore.getKey("server", password);
		
		// Generate DH keys for this session
		// Use hardcoded DH parameters
		DHParameterSpec dhParams = new DHParameterSpec(new BigInteger(new byte[] {(byte)0x00, (byte)0xad, (byte)0x77, (byte)0xcd, (byte)0xb7, (byte)0x14, (byte)0x6f, (byte)0xfe, (byte)0x08, (byte)0x1a, (byte)0xee, (byte)0xd2, (byte)0x2c, (byte)0x18, (byte)0x29, (byte)0x62, (byte)0x5a, (byte)0xff, (byte)0x03, (byte)0x5d, (byte)0xde, (byte)0xba, (byte)0x0d, (byte)0xd4, (byte)0x36, (byte)0x15, (byte)0x03, (byte)0x11, (byte)0x21, (byte)0x48, (byte)0xd9, (byte)0x77, (byte)0xfb, (byte)0x67, (byte)0xb0, (byte)0x74, (byte)0x2e, (byte)0x68, (byte)0xed, (byte)0x5a, (byte)0x3f, (byte)0x8a, (byte)0x3e, (byte)0xdb, (byte)0x81, (byte)0xa3, (byte)0x3b, (byte)0xaf, (byte)0x26, (byte)0xe4, (byte)0x54, (byte)0x00, (byte)0x85, (byte)0x0d, (byte)0xfd, (byte)0x23, (byte)0x21, (byte)0xc1, (byte)0xfe, (byte)0x69, (byte)0xe4, (byte)0xf3, (byte)0x57, (byte)0xe6, (byte)0x0a, (byte)0x7c, (byte)0x62, (byte)0xc0, (byte)0xd6, (byte)0x40, (byte)0x3e, (byte)0x94, (byte)0x9e, (byte)0x49, (byte)0x72, (byte)0x5a, (byte)0x21, (byte)0x53, (byte)0xb0, (byte)0x83, (byte)0x05, (byte)0x81, (byte)0x5a, (byte)0xde, (byte)0x17, (byte)0x31, (byte)0xbf, (byte)0xa8, (byte)0xa9, (byte)0xe5, (byte)0x28, (byte)0x1a, (byte)0xfc, (byte)0x06, (byte)0x1e, (byte)0x49, (byte)0xfe, (byte)0xdc, (byte)0x08, (byte)0xe3, (byte)0x29, (byte)0xfe, (byte)0x5b, (byte)0x88, (byte)0x66, (byte)0x39, (byte)0xa8, (byte)0x69, (byte)0x62, (byte)0x88, (byte)0x47, (byte)0x36, (byte)0xf5, (byte)0xdd, (byte)0x92, (byte)0x8f, (byte)0xca, (byte)0x32, (byte)0x4b, (byte)0x87, (byte)0xad, (byte)0xbf, (byte)0xab, (byte)0x4a, (byte)0x9d, (byte)0xd5, (byte)0xb8, (byte)0x2c, (byte)0xc4, (byte)0x43, (byte)0xb2, (byte)0x21, (byte)0xb4, (byte)0x2a, (byte)0x9b, (byte)0x42, (byte)0x17, (byte)0x6d, (byte)0xb6, (byte)0x86, (byte)0x42, (byte)0x41, (byte)0xb1, (byte)0xc7, (byte)0x37, (byte)0x37, (byte)0x95, (byte)0x6d, (byte)0x62, (byte)0xca, (byte)0xa6, (byte)0x57, (byte)0x33, (byte)0x88, (byte)0xe2, (byte)0x31, (byte)0xfe, (byte)0xd1, (byte)0x51, (byte)0xe7, (byte)0x73, (byte)0xae, (byte)0x3c, (byte)0xa7, (byte)0x4b, (byte)0xbc, (byte)0x8a, (byte)0x3d, (byte)0xc5, (byte)0x9a, (byte)0x28, (byte)0x9a, (byte)0xf9, (byte)0x57, (byte)0xb6, (byte)0xec, (byte)0xf6, (byte)0x75, (byte)0xaa, (byte)0x56, (byte)0xc1, (byte)0x42, (byte)0x9f, (byte)0x6a, (byte)0x7c, (byte)0x91, (byte)0x8b, (byte)0x5e, (byte)0xea, (byte)0x54, (byte)0x32, (byte)0x90, (byte)0x8a, (byte)0x9d, (byte)0x76, (byte)0x2a, (byte)0x29, (byte)0x1b, (byte)0x84, (byte)0x35, (byte)0xe6, (byte)0x21, (byte)0x07, (byte)0xb2, (byte)0xcb, (byte)0x5c, (byte)0xf9, (byte)0x5b, (byte)0xe9, (byte)0x5e, (byte)0x1b, (byte)0x80, (byte)0xd5, (byte)0x53, (byte)0xd7, (byte)0xa4, (byte)0x26, (byte)0x58, (byte)0xe4, (byte)0xe9, (byte)0x3f, (byte)0xfd, (byte)0xeb, (byte)0x78, (byte)0xf2, (byte)0x25, (byte)0x02, (byte)0x42, (byte)0xf8, (byte)0x50, (byte)0x13, (byte)0xbb, (byte)0x01, (byte)0x39, (byte)0xf3, (byte)0xcf, (byte)0x5c, (byte)0x51, (byte)0xdf, (byte)0xed, (byte)0xc5, (byte)0xfa, (byte)0xd8, (byte)0x4f, (byte)0xae, (byte)0x76, (byte)0xe8, (byte)0x30, (byte)0xfc, (byte)0x85, (byte)0xaa, (byte)0x8c, (byte)0x91, (byte)0x02, (byte)0x2b, (byte)0x61, (byte)0x87
}), new BigInteger(new byte[] { 0x05 }));
		
	    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DiffieHellman");
	    keyPairGenerator.initialize(dhParams);
	    
	    KeyPair keyPair = keyPairGenerator.generateKeyPair();
	    dhPubKey = (DHPublicKey)keyPair.getPublic();
	    dhPrivateKey = (DHPrivateKey)keyPair.getPrivate();
	}
 
源代码5 项目: statelearner   文件: TLSTestService.java
public void loadClientKey() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException, InvalidAlgorithmParameterException {
		char[] password = KEYSTORE_PASSWORD.toCharArray();

		FileInputStream fIn = new FileInputStream(KEYSTORE_FILENAME);
		KeyStore keystore = KeyStore.getInstance("JKS");

		keystore.load(fIn, password);
		clientCertificate = (X509Certificate) keystore.getCertificate("client");
		clientPrivateKey  = (PrivateKey) keystore.getKey("client", password);
		
		// Generate DH keys for this session
		// Use hardcoded DH parameters
		DHParameterSpec dhParams = new DHParameterSpec(new BigInteger(new byte[] {(byte)0x00, (byte)0xad, (byte)0x77, (byte)0xcd, (byte)0xb7, (byte)0x14, (byte)0x6f, (byte)0xfe, (byte)0x08, (byte)0x1a, (byte)0xee, (byte)0xd2, (byte)0x2c, (byte)0x18, (byte)0x29, (byte)0x62, (byte)0x5a, (byte)0xff, (byte)0x03, (byte)0x5d, (byte)0xde, (byte)0xba, (byte)0x0d, (byte)0xd4, (byte)0x36, (byte)0x15, (byte)0x03, (byte)0x11, (byte)0x21, (byte)0x48, (byte)0xd9, (byte)0x77, (byte)0xfb, (byte)0x67, (byte)0xb0, (byte)0x74, (byte)0x2e, (byte)0x68, (byte)0xed, (byte)0x5a, (byte)0x3f, (byte)0x8a, (byte)0x3e, (byte)0xdb, (byte)0x81, (byte)0xa3, (byte)0x3b, (byte)0xaf, (byte)0x26, (byte)0xe4, (byte)0x54, (byte)0x00, (byte)0x85, (byte)0x0d, (byte)0xfd, (byte)0x23, (byte)0x21, (byte)0xc1, (byte)0xfe, (byte)0x69, (byte)0xe4, (byte)0xf3, (byte)0x57, (byte)0xe6, (byte)0x0a, (byte)0x7c, (byte)0x62, (byte)0xc0, (byte)0xd6, (byte)0x40, (byte)0x3e, (byte)0x94, (byte)0x9e, (byte)0x49, (byte)0x72, (byte)0x5a, (byte)0x21, (byte)0x53, (byte)0xb0, (byte)0x83, (byte)0x05, (byte)0x81, (byte)0x5a, (byte)0xde, (byte)0x17, (byte)0x31, (byte)0xbf, (byte)0xa8, (byte)0xa9, (byte)0xe5, (byte)0x28, (byte)0x1a, (byte)0xfc, (byte)0x06, (byte)0x1e, (byte)0x49, (byte)0xfe, (byte)0xdc, (byte)0x08, (byte)0xe3, (byte)0x29, (byte)0xfe, (byte)0x5b, (byte)0x88, (byte)0x66, (byte)0x39, (byte)0xa8, (byte)0x69, (byte)0x62, (byte)0x88, (byte)0x47, (byte)0x36, (byte)0xf5, (byte)0xdd, (byte)0x92, (byte)0x8f, (byte)0xca, (byte)0x32, (byte)0x4b, (byte)0x87, (byte)0xad, (byte)0xbf, (byte)0xab, (byte)0x4a, (byte)0x9d, (byte)0xd5, (byte)0xb8, (byte)0x2c, (byte)0xc4, (byte)0x43, (byte)0xb2, (byte)0x21, (byte)0xb4, (byte)0x2a, (byte)0x9b, (byte)0x42, (byte)0x17, (byte)0x6d, (byte)0xb6, (byte)0x86, (byte)0x42, (byte)0x41, (byte)0xb1, (byte)0xc7, (byte)0x37, (byte)0x37, (byte)0x95, (byte)0x6d, (byte)0x62, (byte)0xca, (byte)0xa6, (byte)0x57, (byte)0x33, (byte)0x88, (byte)0xe2, (byte)0x31, (byte)0xfe, (byte)0xd1, (byte)0x51, (byte)0xe7, (byte)0x73, (byte)0xae, (byte)0x3c, (byte)0xa7, (byte)0x4b, (byte)0xbc, (byte)0x8a, (byte)0x3d, (byte)0xc5, (byte)0x9a, (byte)0x28, (byte)0x9a, (byte)0xf9, (byte)0x57, (byte)0xb6, (byte)0xec, (byte)0xf6, (byte)0x75, (byte)0xaa, (byte)0x56, (byte)0xc1, (byte)0x42, (byte)0x9f, (byte)0x6a, (byte)0x7c, (byte)0x91, (byte)0x8b, (byte)0x5e, (byte)0xea, (byte)0x54, (byte)0x32, (byte)0x90, (byte)0x8a, (byte)0x9d, (byte)0x76, (byte)0x2a, (byte)0x29, (byte)0x1b, (byte)0x84, (byte)0x35, (byte)0xe6, (byte)0x21, (byte)0x07, (byte)0xb2, (byte)0xcb, (byte)0x5c, (byte)0xf9, (byte)0x5b, (byte)0xe9, (byte)0x5e, (byte)0x1b, (byte)0x80, (byte)0xd5, (byte)0x53, (byte)0xd7, (byte)0xa4, (byte)0x26, (byte)0x58, (byte)0xe4, (byte)0xe9, (byte)0x3f, (byte)0xfd, (byte)0xeb, (byte)0x78, (byte)0xf2, (byte)0x25, (byte)0x02, (byte)0x42, (byte)0xf8, (byte)0x50, (byte)0x13, (byte)0xbb, (byte)0x01, (byte)0x39, (byte)0xf3, (byte)0xcf, (byte)0x5c, (byte)0x51, (byte)0xdf, (byte)0xed, (byte)0xc5, (byte)0xfa, (byte)0xd8, (byte)0x4f, (byte)0xae, (byte)0x76, (byte)0xe8, (byte)0x30, (byte)0xfc, (byte)0x85, (byte)0xaa, (byte)0x8c, (byte)0x91, (byte)0x02, (byte)0x2b, (byte)0x61, (byte)0x87
}), new BigInteger(new byte[] { 0x05 }));
		
	    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DiffieHellman");
	    keyPairGenerator.initialize(dhParams);
	    
	    KeyPair keyPair = keyPairGenerator.generateKeyPair();
	    dhPubKey = (DHPublicKey)keyPair.getPublic();
	    dhPrivateKey = (DHPrivateKey)keyPair.getPrivate();
	}
 
源代码6 项目: RipplePower   文件: KeyAgreementSpi.java
protected void engineInit(
    Key             key,
    SecureRandom    random) 
    throws InvalidKeyException
{
    if (!(key instanceof DHPrivateKey))
    {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey");
    }

    DHPrivateKey    privKey = (DHPrivateKey)key;

    this.p = privKey.getParams().getP();
    this.g = privKey.getParams().getG();
    this.x = this.result = privKey.getX();
}
 
源代码7 项目: RipplePower   文件: KeyFactorySpi.java
protected Key engineTranslateKey(
    Key key)
    throws InvalidKeyException
{
    if (key instanceof DHPublicKey)
    {
        return new BCElGamalPublicKey((DHPublicKey)key);
    }
    else if (key instanceof DHPrivateKey)
    {
        return new BCElGamalPrivateKey((DHPrivateKey)key);
    }
    else if (key instanceof ElGamalPublicKey)
    {
        return new BCElGamalPublicKey((ElGamalPublicKey)key);
    }
    else if (key instanceof ElGamalPrivateKey)
    {
        return new BCElGamalPrivateKey((ElGamalPrivateKey)key);
    }

    throw new InvalidKeyException("key type unknown");
}
 
源代码8 项目: ripple-lib-java   文件: KeyAgreementSpi.java
protected void engineInit(
    Key             key,
    SecureRandom    random) 
    throws InvalidKeyException
{
    if (!(key instanceof DHPrivateKey))
    {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey");
    }

    DHPrivateKey    privKey = (DHPrivateKey)key;

    this.p = privKey.getParams().getP();
    this.g = privKey.getParams().getG();
    this.x = this.result = privKey.getX();
}
 
源代码9 项目: ripple-lib-java   文件: KeyFactorySpi.java
protected Key engineTranslateKey(
    Key key)
    throws InvalidKeyException
{
    if (key instanceof DHPublicKey)
    {
        return new BCElGamalPublicKey((DHPublicKey)key);
    }
    else if (key instanceof DHPrivateKey)
    {
        return new BCElGamalPrivateKey((DHPrivateKey)key);
    }
    else if (key instanceof ElGamalPublicKey)
    {
        return new BCElGamalPublicKey((ElGamalPublicKey)key);
    }
    else if (key instanceof ElGamalPrivateKey)
    {
        return new BCElGamalPrivateKey((ElGamalPrivateKey)key);
    }

    throw new InvalidKeyException("key type unknown");
}
 
源代码10 项目: protools   文件: ToolDH.java
/**
 * 初始化乙方密钥
 *
 * @param key
 *         甲方公钥
 *
 * @return Map 乙方密钥Map
 *
 * @throws Exception
 */
public static Map<String, Object> initKey(byte[] key) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException {

    // 解析甲方公钥
    // 转换公钥材料
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);

    // 实例化密钥工厂
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

    // 产生公钥
    PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);

    // 由甲方公钥构建乙方密钥
    DHParameterSpec dhParamSpec = ((DHPublicKey) pubKey).getParams();

    // 实例化密钥对生成器
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyFactory.getAlgorithm());

    // 初始化密钥对生成器
    keyPairGenerator.initialize(dhParamSpec);

    // 产生密钥对
    KeyPair keyPair = keyPairGenerator.genKeyPair();

    // 乙方公钥
    DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic();

    // 乙方私钥
    DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();

    // 将密钥对存储在Map中
    Map<String, Object> keyMap = Maps.newHashMapWithExpectedSize(2);

    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);

    return keyMap;
}
 
源代码11 项目: BiglyBT   文件: DHUtil.java
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }

    throw new InvalidKeyException("can't identify DH private key.");
}
 
源代码12 项目: openid4java   文件: DiffieHellmanSession.java
protected byte[] getDigestedZZ(String otherPublicKeyBase64)
{
    DHPublicKey  dhPublicKey  = stringToPublicKey(otherPublicKeyBase64);
    DHPrivateKey dhPrivateKey = getPrivateKey();
    BigInteger xa = dhPrivateKey.getX();
    BigInteger yb = dhPublicKey.getY();
    BigInteger p  = _dhParameterSpec.getP();

    BigInteger zz = yb.modPow(xa, p);

    return _hDigest.digest(zz.toByteArray());
}
 
源代码13 项目: RipplePower   文件: DHUtil.java
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }
                    
    throw new InvalidKeyException("can't identify DH private key.");
}
 
源代码14 项目: RipplePower   文件: KeyAgreementSpi.java
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();
}
 
源代码15 项目: RipplePower   文件: KeyFactorySpi.java
protected Key engineTranslateKey(
    Key key)
    throws InvalidKeyException
{
    if (key instanceof DHPublicKey)
    {
        return new BCDHPublicKey((DHPublicKey)key);
    }
    else if (key instanceof DHPrivateKey)
    {
        return new BCDHPrivateKey((DHPrivateKey)key);
    }

    throw new InvalidKeyException("key type unknown");
}
 
源代码16 项目: RipplePower   文件: BCDHPrivateKey.java
public boolean equals(
    Object o)
{
    if (!(o instanceof DHPrivateKey))
    {
        return false;
    }

    DHPrivateKey other = (DHPrivateKey)o;

    return this.getX().equals(other.getX())
        && this.getParams().getG().equals(other.getParams().getG())
        && this.getParams().getP().equals(other.getParams().getP())
        && this.getParams().getL() == other.getParams().getL();
}
 
源代码17 项目: RipplePower   文件: DHUtil.java
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }
                    
    throw new InvalidKeyException("can't identify DH private key.");
}
 
源代码18 项目: RipplePower   文件: BCElGamalPrivateKey.java
public boolean equals(
    Object o)
{
    if (!(o instanceof DHPrivateKey))
    {
        return false;
    }

    DHPrivateKey other = (DHPrivateKey)o;

    return this.getX().equals(other.getX())
        && this.getParams().getG().equals(other.getParams().getG())
        && this.getParams().getP().equals(other.getParams().getP())
        && this.getParams().getL() == other.getParams().getL();
}
 
源代码19 项目: ripple-lib-java   文件: DHUtil.java
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }
                    
    throw new InvalidKeyException("can't identify DH private key.");
}
 
源代码20 项目: ripple-lib-java   文件: KeyAgreementSpi.java
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();
}
 
源代码21 项目: ripple-lib-java   文件: KeyFactorySpi.java
protected Key engineTranslateKey(
    Key key)
    throws InvalidKeyException
{
    if (key instanceof DHPublicKey)
    {
        return new BCDHPublicKey((DHPublicKey)key);
    }
    else if (key instanceof DHPrivateKey)
    {
        return new BCDHPrivateKey((DHPrivateKey)key);
    }

    throw new InvalidKeyException("key type unknown");
}
 
源代码22 项目: ripple-lib-java   文件: BCDHPrivateKey.java
public boolean equals(
    Object o)
{
    if (!(o instanceof DHPrivateKey))
    {
        return false;
    }

    DHPrivateKey other = (DHPrivateKey)o;

    return this.getX().equals(other.getX())
        && this.getParams().getG().equals(other.getParams().getG())
        && this.getParams().getP().equals(other.getParams().getP())
        && this.getParams().getL() == other.getParams().getL();
}
 
源代码23 项目: ripple-lib-java   文件: DHUtil.java
static public AsymmetricKeyParameter generatePrivateKeyParameter(
    PrivateKey    key)
    throws InvalidKeyException
{
    if (key instanceof DHPrivateKey)
    {
        DHPrivateKey    k = (DHPrivateKey)key;

        return new DHPrivateKeyParameters(k.getX(),
            new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
    }
                    
    throw new InvalidKeyException("can't identify DH private key.");
}
 
源代码24 项目: ripple-lib-java   文件: BCElGamalPrivateKey.java
public boolean equals(
    Object o)
{
    if (!(o instanceof DHPrivateKey))
    {
        return false;
    }

    DHPrivateKey other = (DHPrivateKey)o;

    return this.getX().equals(other.getX())
        && this.getParams().getG().equals(other.getParams().getG())
        && this.getParams().getP().equals(other.getParams().getP())
        && this.getParams().getL() == other.getParams().getL();
}
 
源代码25 项目: openjdk-jdk9   文件: SupportedDHKeys.java
private static void checkKeyPair(KeyPair kp, int pSize,
            Provider provider) throws Exception {

    DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();
    BigInteger p = privateKey.getParams().getP();
    if (p.bitLength() != pSize) {
        throw new Exception(
            "Invalid modulus size: " + p.bitLength() + "/" + pSize);
    }

    // System.out.println("P(" + pSize + "): " + p.toString());
    if (!p.isProbablePrime(128)) {
        throw new Exception("Good luck, the modulus is composite!");
    }

    DHPublicKey publicKey = (DHPublicKey)kp.getPublic();
    p = publicKey.getParams().getP();
    if (p.bitLength() != pSize) {
        throw new Exception(
            "Invalid modulus size: " + p.bitLength() + "/" + pSize);
    }

    BigInteger leftOpen = BigInteger.ONE;
    BigInteger rightOpen = p.subtract(BigInteger.ONE);

    // ignore the private key range checking on Solaris at present
    if (!provider.getName().equals("SunPKCS11-Solaris")) {
        BigInteger x = privateKey.getX();
        if ((x.compareTo(leftOpen) <= 0) ||
                (x.compareTo(rightOpen) >= 0)) {
            throw new Exception(
                "X outside range [2, p - 2]:  x: " + x + " p: " + p);
        }
    }

    BigInteger y = publicKey.getY();
    if ((y.compareTo(leftOpen) <= 0) ||
            (y.compareTo(rightOpen) >= 0)) {
        throw new Exception(
            "Y outside range [2, p - 2]:  y: " + y + " p: " + p);
    }
}
 
源代码26 项目: BiglyBT   文件: JDKKeyFactory.java
@Override
protected Key engineTranslateKey(
    Key    key)
    throws InvalidKeyException
{
    if (key instanceof RSAPublicKey)
    {
        return new JCERSAPublicKey((RSAPublicKey)key);
    }
    else if (key instanceof RSAPrivateCrtKey)
    {
        //return new JCERSAPrivateCrtKey((RSAPrivateCrtKey)key);
    }
    else if (key instanceof RSAPrivateKey)
    {
        //return new JCERSAPrivateKey((RSAPrivateKey)key);
    }
    else if (key instanceof DHPublicKey)
    {
        //return new JCEDHPublicKey((DHPublicKey)key);
    }
    else if (key instanceof DHPrivateKey)
    {
        //return new JCEDHPrivateKey((DHPrivateKey)key);
    }
    else if (key instanceof DSAPublicKey)
    {
        //return new JDKDSAPublicKey((DSAPublicKey)key);
    }
    else if (key instanceof DSAPrivateKey)
    {
        //return new JDKDSAPrivateKey((DSAPrivateKey)key);
    }
    else if (key instanceof ElGamalPublicKey)
    {
        //return new JCEElGamalPublicKey((ElGamalPublicKey)key);
    }
    else if (key instanceof ElGamalPrivateKey)
    {
        //return new JCEElGamalPrivateKey((ElGamalPrivateKey)key);
    }

    throw new InvalidKeyException("key type unknown");
}
 
源代码27 项目: TorrentEngine   文件: JDKKeyFactory.java
protected Key engineTranslateKey(
    Key    key)
    throws InvalidKeyException
{
    if (key instanceof RSAPublicKey)
    {
        return new JCERSAPublicKey((RSAPublicKey)key);
    }
    else if (key instanceof RSAPrivateCrtKey)
    {
        //return new JCERSAPrivateCrtKey((RSAPrivateCrtKey)key);
    }
    else if (key instanceof RSAPrivateKey)
    {
        //return new JCERSAPrivateKey((RSAPrivateKey)key);
    }
    else if (key instanceof DHPublicKey)
    {
        //return new JCEDHPublicKey((DHPublicKey)key);
    }
    else if (key instanceof DHPrivateKey)
    {
        //return new JCEDHPrivateKey((DHPrivateKey)key);
    }
    else if (key instanceof DSAPublicKey)
    {
        //return new JDKDSAPublicKey((DSAPublicKey)key);
    }
    else if (key instanceof DSAPrivateKey)
    {
        //return new JDKDSAPrivateKey((DSAPrivateKey)key);
    }
    else if (key instanceof ElGamalPublicKey)
    {
        //return new JCEElGamalPublicKey((ElGamalPublicKey)key);
    }
    else if (key instanceof ElGamalPrivateKey)
    {
        //return new JCEElGamalPrivateKey((ElGamalPrivateKey)key);
    }

    throw new InvalidKeyException("key type unknown");
}
 
源代码28 项目: wycheproof   文件: DhTest.java
private void testKeyPair(KeyPair keyPair, int expectedKeySize) throws Exception {
  DHPrivateKey priv = (DHPrivateKey) keyPair.getPrivate();
  BigInteger p = priv.getParams().getP();
  BigInteger g = priv.getParams().getG();
  int keySize = p.bitLength();
  assertEquals("wrong key size", expectedKeySize, keySize);

  // Checks the key size of the private key.
  // NIST SP 800-56A requires that x is in the range (1, q-1).
  // Such a choice would require a full key validation. Since such a validation
  // requires the value q (which is not present in the DH parameters) larger keys
  // should be chosen to prevent attacks.
  int minPrivateKeyBits = keySize / 2;
  BigInteger x = priv.getX();
  assertTrue(x.bitLength() >= minPrivateKeyBits - 32);
  // TODO(bleichen): add tests for weak random number generators.

  // Verify the DH parameters.
  System.out.println("p=" + p.toString(16));
  System.out.println("g=" + g.toString(16));
  System.out.println("testKeyPairGenerator L=" + priv.getParams().getL());
  // Basic parameter checks
  assertTrue("Expecting g > 1", g.compareTo(BigInteger.ONE) > 0);
  assertTrue("Expecting g < p - 1", g.compareTo(p.subtract(BigInteger.ONE)) < 0);
  // Expecting p to be prime.
  // No high certainty is needed, since this is a unit test.
  assertTrue(p.isProbablePrime(4));
  // The order of g should be a large prime divisor q of p-1.
  // (see e.g. NIST SP 800-56A, section 5.5.1.1.)
  // If the order of g is composite then the Decision Diffie Hellman assumption is
  // not satisfied for the group generated by g. Moreover, attacks using Pohlig-Hellman
  // might be feasible.
  // A good way to achieve these requirements is to select a safe prime p (i.e. a prime
  // where q=(p-1)/2 is prime too. NIST SP 800-56A does not require (or even recommend)
  // safe primes and allows Diffie-Hellman parameters where q is significantly smaller.
  // Unfortunately, the key does not contain q and thus the conditions above  cannot be
  // tested easily.
  // We perform a partial test that performs a partial factorization of p-1 and then
  // test whether one of the small factors found by the partial factorization divides
  // the order of g.
  boolean isSafePrime = p.shiftRight(1).isProbablePrime(4);
  System.out.println("p is a safe prime:" + isSafePrime);
  BigInteger r; // p-1 divided by small prime factors.
  if (isSafePrime) {
    r = p.shiftRight(1);
  } else {
    BigInteger p1 = p.subtract(BigInteger.ONE);
    r = p1.divide(smoothDivisor(p1));
  }
  System.out.println("r=" + r.toString(16));
  assertEquals(
      "g likely does not generate a prime oder subgroup", BigInteger.ONE, g.modPow(r, p));

  // Checks that there are not too many short prime factors.
  // I.e., subgroup confinment attacks can find at least keySize - r.bitLength() bits of the key.
  // At least 160 unknown bits should remain.
  // Only very weak parameters are detected here, since the factorization above only finds small
  // prime factors.
  assertTrue(minPrivateKeyBits - (keySize - r.bitLength()) > 160);

  // DH parameters are sometime misconfigures and g and q are swapped.
  // A large g that divides p-1 is suspicious.
  if (g.bitLength() >= 160) {
    assertTrue(p.mod(g).compareTo(BigInteger.ONE) > 0);
  }
}
 
源代码29 项目: openid4java   文件: DiffieHellmanSession.java
protected DHPrivateKey getPrivateKey()
{
    return (DHPrivateKey) _keyPair.getPrivate();
}
 
源代码30 项目: openid4java   文件: DiffieHellmanSessionTest.java
private static String privateKeyToString(DHPrivateKey dhPrivateKey)
{
    return new String(Base64.encodeBase64(dhPrivateKey.getX().toByteArray()));
}