当前位置: 首页>>代码示例>>Java>>正文


Java DHPrivateKeySpec类代码示例

本文整理汇总了Java中javax.crypto.spec.DHPrivateKeySpec的典型用法代码示例。如果您正苦于以下问题:Java DHPrivateKeySpec类的具体用法?Java DHPrivateKeySpec怎么用?Java DHPrivateKeySpec使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


DHPrivateKeySpec类属于javax.crypto.spec包,在下文中一共展示了DHPrivateKeySpec类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: engineGeneratePrivate

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification", e);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:35,代码来源:DHKeyFactory.java

示例2: engineGeneratePrivate

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
/**
 * Generates a private key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the private key
 *
 * @return the private key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a private key.
 */
protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPrivateKeySpec) {
            DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
            return new DHPrivateKey(dhPrivKeySpec.getX(),
                                    dhPrivKeySpec.getP(),
                                    dhPrivKeySpec.getG());

        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            return new DHPrivateKey
                (((PKCS8EncodedKeySpec)keySpec).getEncoded());

        } else {
            throw new InvalidKeySpecException
                ("Inappropriate key specification");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeySpecException
            ("Inappropriate key specification");
    }
}
 
开发者ID:openjdk,项目名称:jdk7-jdk,代码行数:35,代码来源:DHKeyFactory.java

示例3: testDHPrivateKeySpec

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
/**
 * DHPrivateKeySpec class testing. Tests the equivalence of parameters
 * specified in the constructor with the values returned by getters.
 */
public void testDHPrivateKeySpec() {
    BigInteger[] xs = {new BigInteger("-1000000000000"), BigInteger.ZERO,
                        BigInteger.ONE, new BigInteger("1000000000000")};
    BigInteger[] ps = {new BigInteger("-1000000000000"), BigInteger.ZERO,
                        BigInteger.ONE, new BigInteger("1000000000000")};
    BigInteger[] gs = {new BigInteger("-1000000000000"), BigInteger.ZERO,
                        BigInteger.ONE, new BigInteger("1000000000000")};
    for (int i=0; i<ps.length; i++) {
        DHPrivateKeySpec dhpks = new DHPrivateKeySpec(xs[i], ps[i], gs[i]);
        assertEquals("The value returned by getX() must be "
                    + "equal to the value specified in the constructor",
                    dhpks.getX(), xs[i]);
        assertEquals("The value returned by getP() must be "
                    + "equal to the value specified in the constructor",
                    dhpks.getP(), ps[i]);
        assertEquals("The value returned by getG() must be "
                    + "equal to the value specified in the constructor",
                    dhpks.getG(), gs[i]);
    }
}
 
开发者ID:shannah,项目名称:cn1,代码行数:25,代码来源:DHPrivateKeySpecTest.java

示例4: createNodeKey

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
public byte[] createNodeKey(byte[] pubKeyNode) throws NoSuchAlgorithmException,
        InvalidKeySpecException, InvalidKeyException, IllegalStateException {

    // add this public key node to agreement
    KeyFactory keyFac = KeyFactory.getInstance("DH");
    BigInteger y = new BigInteger(1, pubKeyNode);
    DHPublicKeySpec spec = new DHPublicKeySpec(y, sP, sG);
    PublicKey nodePubKey = keyFac.generatePublic(spec);
    mKA.doPhase(nodePubKey, true);

    // complete this phase of agreement by generating secret
    BigInteger x = new BigInteger(1, mKA.generateSecret());
    BigInteger v = sG.modPow(x, sP);

    DHPrivateKeySpec specX = new DHPrivateKeySpec(x, sP, sG);
    PrivateKey nodePrivKey = keyFac.generatePrivate(specX);
    mKA.doPhase(nodePubKey, true);

    mKA = KeyAgreement.getInstance("DH");
    mKA.init(nodePrivKey);

    return getBytes(v);
}
 
开发者ID:SafeSlingerProject,项目名称:exchange-android,代码行数:24,代码来源:CryptoAccess.java

示例5: engineGeneratePrivate

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
protected PrivateKey engineGeneratePrivate(
    KeySpec keySpec)
    throws InvalidKeySpecException
{
    if (keySpec instanceof DHPrivateKeySpec)
    {
        return new BCDHPrivateKey((DHPrivateKeySpec)keySpec);
    }

    return super.engineGeneratePrivate(keySpec);
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:12,代码来源:KeyFactorySpi.java

示例6: engineGeneratePrivate

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
protected PrivateKey engineGeneratePrivate(
    KeySpec keySpec)
    throws InvalidKeySpecException
{
    if (keySpec instanceof ElGamalPrivateKeySpec)
    {
        return new BCElGamalPrivateKey((ElGamalPrivateKeySpec)keySpec);
    }
    else if (keySpec instanceof DHPrivateKeySpec)
    {
        return new BCElGamalPrivateKey((DHPrivateKeySpec)keySpec);
    }

    return super.engineGeneratePrivate(keySpec);
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:16,代码来源:KeyFactorySpi.java

示例7: engineTranslateKey

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = engineGetKeySpec(key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = engineGetKeySpec(key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key", e);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:47,代码来源:DHKeyFactory.java

示例8: bad10

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
public void bad10() throws Exception {
    BigInteger bigInteger = new BigInteger("12345", 5);
    new DSAPrivateKeySpec(bigInteger, null, null, null);
    new DSAPublicKeySpec(bigInteger, null, bigInteger, null); // report once
    new DHPrivateKeySpec(bigInteger, null, null);
    new DHPublicKeySpec(bigInteger, null, null);
    new ECPrivateKeySpec(bigInteger, null);
    new RSAPrivateKeySpec(bigInteger, null);
    new RSAMultiPrimePrivateCrtKeySpec(bigInteger, null, null, null, null, null, null, null, null);
    new RSAPrivateCrtKeySpec(bigInteger, null, null, null, null, null, null, null);
    new RSAPublicKeySpec(bigInteger, null);
    new DSAPublicKeyImpl(bigInteger, null, null, null);
}
 
开发者ID:blackarbiter,项目名称:Android_Code_Arbiter,代码行数:14,代码来源:ConstantPasswords.java

示例9: getPrivateKey

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
/**
 * Get merchant Private Key
 * @return PrivateKey object for the merchant
 */
public PrivateKey getPrivateKey() throws InvalidKeySpecException, NoSuchAlgorithmException {
    byte[] privateKeyBytes = this.getPrivateKeyBytes();

    // initialize the parameter spec
    DHParameterSpec dhParamSpec = this.getDHParameterSpec();

    // load the private key
    KeyFactory keyFactory = KeyFactory.getInstance("DH");
    BigInteger privateKeyInt = new BigInteger(privateKeyBytes);
    DHPrivateKeySpec dhPrivateSpec = new DHPrivateKeySpec(privateKeyInt, dhParamSpec.getP(), dhParamSpec.getG());
    PrivateKey privateKey = keyFactory.generatePrivate(dhPrivateSpec);

    return privateKey;
}
 
开发者ID:ilscipio,项目名称:scipio-erp,代码行数:19,代码来源:ValueLinkApi.java

示例10: decodeDHPrivateKey

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
/**
 * @param spec an instance of {@link DHPrivateKeySpec} to decode.
 * @return an instance of a {@link DHPrivateKey} constructed from the
 *         information in the designated key-specification.
 * @throws InvalidKeySpecException if no concrete implementation of the
 *           {@link DHPrivateKey} interface exists at run-time, or if an
 *           exception occurs during its instantiation.
 */
private DHPrivateKey decodeDHPrivateKey(DHPrivateKeySpec spec)
    throws InvalidKeySpecException
{
  BigInteger p = spec.getP();
  BigInteger g = spec.getG();
  BigInteger x = spec.getX();
  Object[] params = new Object[] {Integer.valueOf(Registry.PKCS8_ENCODING_ID),
                                  null, p, g, x};
  Object obj = invokeConstructor("gnu.javax.crypto.key.dh.GnuDHPrivateKey",
                                 params);
  return (DHPrivateKey) obj;
}
 
开发者ID:vilie,项目名称:javify,代码行数:21,代码来源:EncodedKeyFactory.java

示例11: generateDHKeyPair

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
public KeyPair generateDHKeyPair() throws OtrCryptoException {

        // Generate a AsymmetricCipherKeyPair using BC.
        DHParameters dhParams = new DHParameters(MODULUS, GENERATOR, null,
                DH_PRIVATE_KEY_MINIMUM_BIT_LENGTH);
        DHKeyGenerationParameters params = new DHKeyGenerationParameters(new SecureRandom(),
                dhParams);
        DHKeyPairGenerator kpGen = new DHKeyPairGenerator();

        kpGen.init(params);
        AsymmetricCipherKeyPair pair = kpGen.generateKeyPair();

        // Convert this AsymmetricCipherKeyPair to a standard JCE KeyPair.
        DHPublicKeyParameters pub = (DHPublicKeyParameters) pair.getPublic();
        DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair.getPrivate();

        try {
            KeyFactory keyFac = KeyFactory.getInstance("DH");

            DHPublicKeySpec pubKeySpecs = new DHPublicKeySpec(pub.getY(), MODULUS, GENERATOR);
            DHPublicKey pubKey = (DHPublicKey) keyFac.generatePublic(pubKeySpecs);

            DHParameters dhParameters = priv.getParameters();
            DHPrivateKeySpec privKeySpecs = new DHPrivateKeySpec(priv.getX(), dhParameters.getP(),
                    dhParameters.getG());
            DHPrivateKey privKey = (DHPrivateKey) keyFac.generatePrivate(privKeySpecs);

            return new KeyPair(pubKey, privKey);
        } catch (Exception e) {
            throw new OtrCryptoException(e);
        }
    }
 
开发者ID:zom,项目名称:Zom-Android,代码行数:33,代码来源:OtrCryptoEngineImpl.java

示例12: engineTranslateKey

import javax.crypto.spec.DHPrivateKeySpec; //导入依赖的package包/类
/**
 * Translates a key object, whose provider may be unknown or potentially
 * untrusted, into a corresponding key object of this key factory.
 *
 * @param key the key whose provider is unknown or untrusted
 *
 * @return the translated key
 *
 * @exception InvalidKeyException if the given key cannot be processed by
 * this key factory.
 */
protected Key engineTranslateKey(Key key)
    throws InvalidKeyException
{
    try {

        if (key instanceof javax.crypto.interfaces.DHPublicKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPublicKey) {
                return key;
            }
            // Convert key to spec
            DHPublicKeySpec dhPubKeySpec
                = (DHPublicKeySpec)engineGetKeySpec
                (key, DHPublicKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePublic(dhPubKeySpec);

        } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
            // Check if key originates from this factory
            if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
                return key;
            }
            // Convert key to spec
            DHPrivateKeySpec dhPrivKeySpec
                = (DHPrivateKeySpec)engineGetKeySpec
                (key, DHPrivateKeySpec.class);
            // Create key from spec, and return it
            return engineGeneratePrivate(dhPrivKeySpec);

        } else {
            throw new InvalidKeyException("Wrong algorithm type");
        }

    } catch (InvalidKeySpecException e) {
        throw new InvalidKeyException("Cannot translate key");
    }
}
 
开发者ID:openjdk,项目名称:jdk7-jdk,代码行数:49,代码来源:DHKeyFactory.java


注:本文中的javax.crypto.spec.DHPrivateKeySpec类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。