當前位置: 首頁>>代碼示例>>Java>>正文


Java DHPublicKeySpec類代碼示例

本文整理匯總了Java中javax.crypto.spec.DHPublicKeySpec的典型用法代碼示例。如果您正苦於以下問題:Java DHPublicKeySpec類的具體用法?Java DHPublicKeySpec怎麽用?Java DHPublicKeySpec使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


DHPublicKeySpec類屬於javax.crypto.spec包,在下文中一共展示了DHPublicKeySpec類的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: DH_ServerKeyExchange

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的package包/類
DH_ServerKeyExchange(HandshakeInStream input,
        ProtocolVersion protocolVersion)
        throws IOException, GeneralSecurityException {

    this.protocolVersion = protocolVersion;
    this.preferableSignatureAlgorithm = null;

    dh_p = input.getBytes16();
    dh_g = input.getBytes16();
    dh_Ys = input.getBytes16();
    KeyUtil.validate(new DHPublicKeySpec(new BigInteger(1, dh_Ys),
                                         new BigInteger(1, dh_p),
                                         new BigInteger(1, dh_g)));

    signature = null;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:17,代碼來源:HandshakeMessage.java

示例2: engineGeneratePublic

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的package包/類
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)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

示例3: getSharedSecret

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的package包/類
/**
 * Complete the key agreement protocol with the peer public value
 * <code>otherPublic</code> and return the calculated shared secret.
 * 
 * @param otherPublic
 *            The peer public value.
 * @return The shared secret value produced by the protocol.
 */
public byte[] getSharedSecret(final BigInteger otherPublic) throws TorException
{
	try
	{
		KeyFactory factory = KeyFactory.getInstance("DH");
		DHPublicKeySpec pub = new DHPublicKeySpec(otherPublic, P1024, G);
		PublicKey key = factory.generatePublic(pub);
		dh.doPhase(key, true);
		return dh.generateSecret();
	}
	catch (GeneralSecurityException e)
	{
		throw new TorException(e);
	}
}
 
開發者ID:B4dT0bi,項目名稱:silvertunnel-ng,代碼行數:24,代碼來源:TorKeyAgreement.java

示例4: getValueLinkPublicKey

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的package包/類
/**
 * Get a public key object for the ValueLink supplied public key
 * @return PublicKey object of ValueLinks's public key
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
public PublicKey getValueLinkPublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
    // read the valuelink public key
    String publicValue = (String) props.get("payment.valuelink.publicValue");
    byte[] publicKeyBytes = StringUtil.fromHexString(publicValue);

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

    // load the valuelink public key
    KeyFactory keyFactory = KeyFactory.getInstance("DH");
    BigInteger publicKeyInt = new BigInteger(publicKeyBytes);
    DHPublicKeySpec dhPublicSpec = new DHPublicKeySpec(publicKeyInt, dhParamSpec.getP(), dhParamSpec.getG());
    PublicKey vlPublic = keyFactory.generatePublic(dhPublicSpec);

    return vlPublic;
}
 
開發者ID:ilscipio,項目名稱:scipio-erp,代碼行數:23,代碼來源:ValueLinkApi.java

示例5: stringToPublicKey

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的package包/類
protected DHPublicKey stringToPublicKey(String publicKeyBase64)
{
    try
    {
        byte[] yBinary = Base64.decodeBase64(publicKeyBase64.getBytes());
        BigInteger y = new BigInteger(yBinary);

        DHPublicKeySpec dhPublicKeySpec = new DHPublicKeySpec(
                y, _dhParameterSpec.getP(), _dhParameterSpec.getG() );

        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);

        return (DHPublicKey) keyFactory.generatePublic(dhPublicKeySpec);
    }
    catch (GeneralSecurityException e)
    {
        _log.error("Cannot create PublicKey object from: " + publicKeyBase64, e);

        return null;
    }
}
 
開發者ID:jbufu,項目名稱:openid4java,代碼行數:22,代碼來源:DiffieHellmanSession.java

示例6: engineGeneratePublic

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的package包/類
/**
 * Generates a public key object from the provided key specification
 * (key material).
 *
 * @param keySpec the specification (key material) of the public key
 *
 * @return the public key
 *
 * @exception InvalidKeySpecException if the given key specification
 * is inappropriate for this key factory to produce a public key.
 */
protected PublicKey engineGeneratePublic(KeySpec keySpec)
    throws InvalidKeySpecException
{
    try {
        if (keySpec instanceof DHPublicKeySpec) {
            DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
            return new DHPublicKey(dhPubKeySpec.getY(),
                                   dhPubKeySpec.getP(),
                                   dhPubKeySpec.getG());

        } else if (keySpec instanceof X509EncodedKeySpec) {
            return new DHPublicKey
                (((X509EncodedKeySpec)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

示例7: testDHPrivateKeySpec

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的package包/類
/**
 * DHPublicKeySpec class testing. Tests the equivalence of parameters
 * specified in the constructor with the values returned by getters.
 */
public void testDHPrivateKeySpec() {
    BigInteger[] ys = {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++) {
        DHPublicKeySpec dhpks = new DHPublicKeySpec(ys[i], ps[i], gs[i]);
        assertEquals("The value returned by getY() must be "
                    + "equal to the value specified in the constructor",
                    dhpks.getY(), ys[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,代碼來源:DHPublicKeySpecTest.java

示例8: createNodeKey

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的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

示例9: getHashedSharedSecret

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的package包/類
private byte[] getHashedSharedSecret ( final String base64PublicKey )
  throws IOException
{
  Preconditions.checkNotNull(base64PublicKey, "base64PublicKey required");

  try {
    DHPublicKey publicKey =
        (DHPublicKey) KeyFactory.getInstance("DH").generatePublic(
            new DHPublicKeySpec(new BigInteger(Base64.decode(
                base64PublicKey.toCharArray(), false)),
                DEFAULT_DH_SPEC.getP(), DEFAULT_DH_SPEC.getG()));

    BigInteger zz =
        publicKey.getY().modPow(((DHPrivateKey) keyPair.getPrivate()).getX(),
            DEFAULT_DH_SPEC.getP());

    return MessageDigest.getInstance(algorithm.getName()).digest(zz.toByteArray());
  } catch ( GeneralSecurityException e ) {
    throw new RuntimeException(e);
  }
}
 
開發者ID:calebrichardson,項目名稱:spiff,代碼行數:22,代碼來源:DhSession.java

示例10: engineGeneratePublic

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的package包/類
protected PublicKey engineGeneratePublic(
    KeySpec keySpec)
    throws InvalidKeySpecException
{
    if (keySpec instanceof DHPublicKeySpec)
    {
        return new BCDHPublicKey((DHPublicKeySpec)keySpec);
    }

    return super.engineGeneratePublic(keySpec);
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:12,代碼來源:KeyFactorySpi.java

示例11: engineGeneratePublic

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的package包/類
protected PublicKey engineGeneratePublic(
    KeySpec keySpec)
    throws InvalidKeySpecException
{
    if (keySpec instanceof ElGamalPublicKeySpec)
    {
        return new BCElGamalPublicKey((ElGamalPublicKeySpec)keySpec);
    }
    else if (keySpec instanceof DHPublicKeySpec)
    {
        return new BCElGamalPublicKey((DHPublicKeySpec)keySpec);
    }
    return super.engineGeneratePublic(keySpec);
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:15,代碼來源:KeyFactorySpi.java

示例12: validate

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的package包/類
/**
 * Returns whether the key spec is valid or not.
 * <P>
 * Note that this method is only apply to DHPublicKeySpec at present.
 *
 * @param  keySpec
 *         the key spec object, cannot be null
 *
 * @throws NullPointerException if {@code keySpec} is null
 * @throws InvalidKeyException if {@code keySpec} is invalid
 */
public static final void validate(KeySpec keySpec)
        throws InvalidKeyException {
    if (keySpec == null) {
        throw new NullPointerException(
            "The key spec to be validated cannot be null");
    }

    if (keySpec instanceof DHPublicKeySpec) {
        validateDHPublicKey((DHPublicKeySpec)keySpec);
    }
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:23,代碼來源:KeyUtil.java

示例13: engineTranslateKey

import javax.crypto.spec.DHPublicKeySpec; //導入依賴的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


注:本文中的javax.crypto.spec.DHPublicKeySpec類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。