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


Java KeyAgreement.generateSecret方法代碼示例

本文整理匯總了Java中javax.crypto.KeyAgreement.generateSecret方法的典型用法代碼示例。如果您正苦於以下問題:Java KeyAgreement.generateSecret方法的具體用法?Java KeyAgreement.generateSecret怎麽用?Java KeyAgreement.generateSecret使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.crypto.KeyAgreement的用法示例。


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

示例1: computeSharedSecret

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
public static byte[] computeSharedSecret(ECPrivateKey myPrivateKey, ECPoint publicPoint)
    throws GeneralSecurityException {
  checkPointOnCurve(publicPoint, myPrivateKey.getParams().getCurve());
  // Explicitly reconstruct the peer public key using private key's spec.
  ECParameterSpec privSpec = myPrivateKey.getParams();
  EllipticCurve privCurve = privSpec.getCurve();
  ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(publicPoint, privSpec);
  KeyFactory kf = KeyFactory.getInstance("EC");
  PublicKey publicKey = kf.generatePublic(publicKeySpec);
  KeyAgreement ka = EngineFactory.KEY_AGREEMENT.getInstance("ECDH");
  ka.init(myPrivateKey);
  ka.doPhase(publicKey, true /* lastPhase */);
  byte[] secret = ka.generateSecret();
  validateSharedSecret(secret, myPrivateKey);
  return secret;
}
 
開發者ID:google,項目名稱:tink,代碼行數:17,代碼來源:EllipticCurves.java

示例2: doEcDh

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
@Override
public byte[] doEcDh(final Key privateKey,
		             final byte[] publicKey,
		             final EcCurve curveName) throws NoSuchAlgorithmException,
		                                             InvalidKeyException,
		                                             InvalidKeySpecException {

	if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
		Security.addProvider(new BouncyCastleProvider());
	}
	KeyAgreement ka;
	try {
		ka = KeyAgreement.getInstance(ECDH, BouncyCastleProvider.PROVIDER_NAME);
	}
	catch (final NoSuchProviderException e) {
		ka = KeyAgreement.getInstance(ECDH);
	}
	ka.init(privateKey);
	ka.doPhase(loadEcPublicKey(publicKey, curveName), true);
	return ka.generateSecret();
}
 
開發者ID:MiFirma,項目名稱:mi-firma-android,代碼行數:22,代碼來源:JseCryptoHelper.java

示例3: doEcDh

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
@Override
public final byte[] doEcDh(final Key privateKey,
		             final byte[] publicKey,
		             final EcCurve curveName) throws NoSuchAlgorithmException,
		                                             InvalidKeyException,
		                                             InvalidKeySpecException {

	if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
		Security.insertProviderAt(new BouncyCastleProvider(), 1);
	}
	KeyAgreement ka = null;
	try {
		ka = KeyAgreement.getInstance(ECDH, BouncyCastleProvider.PROVIDER_NAME);

	}
	catch (final NoSuchProviderException e) {
		ka = KeyAgreement.getInstance(ECDH);
	}
	ka.init(privateKey);
	ka.doPhase(loadEcPublicKey(publicKey, curveName), true);
	return ka.generateSecret();
}
 
開發者ID:MiFirma,項目名稱:mi-firma-android,代碼行數:23,代碼來源:JseCryptoHelper.java

示例4: testAlgorithm

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
private static void testAlgorithm(KeyAgreement ka1, KeyPair kp1,
        KeyAgreement ka2, KeyPair kp2, String algorithm) throws Exception {
    SecretKey key1;

    ka1.init(kp1.getPrivate());
    ka1.doPhase(kp2.getPublic(), true);
    System.out.println("Derive " + algorithm + " using SunJCE...");
    key1 = ka1.generateSecret(algorithm);

    ka2.init(kp1.getPrivate());
    ka2.doPhase(kp2.getPublic(), true);
    System.out.println("Derive " + algorithm + " using PKCS#11...");
    SecretKey key2 = ka2.generateSecret(algorithm);

    byte[] b1 = key1.getEncoded();
    byte[] b2 = key2.getEncoded();

    if (Arrays.equals(b1, b2) == false) {
        System.out.println(b1.length + " bytes: " + toString(b1));
        System.out.println(b2.length + " bytes: " + toString(b2));
        throw new Exception(algorithm + " secret mismatch");
    }
}
 
開發者ID:campolake,項目名稱:openjdk9,代碼行數:24,代碼來源:TestDH.java

示例5: testKeyAgreement

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
private static void testKeyAgreement(KeyPair kpA, KeyPair kpB, Provider p)
    throws Exception {
    KeyAgreement ka1 = KeyAgreement.getInstance("ECDH", p);
    ka1.init(kpA.getPrivate());
    ka1.doPhase(kpB.getPublic(), true);
    byte[] s1 = ka1.generateSecret();

    KeyAgreement ka2 = KeyAgreement.getInstance("ECDH", p);
    ka2.init(kpB.getPrivate());
    ka2.doPhase(kpA.getPublic(), true);
    byte[] s2 = ka2.generateSecret();
    if (Arrays.equals(s1, s2) == false) {
        System.out.println("expected: " + toString(s1));
        System.out.println("actual:   " + toString(s2));
        throw new Exception("Generated secrets do not match");
    }
}
 
開發者ID:campolake,項目名稱:openjdk9,代碼行數:18,代碼來源:TestECDH2.java

示例6: generateEcdhSecret

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
private byte[] generateEcdhSecret(PrivateKey privateKey, PublicKey publicKey, ProviderContext providerContext) throws JoseException
{
    String keyAgreementProvider = providerContext.getSuppliedKeyProviderContext().getKeyAgreementProvider();
    KeyAgreement keyAgreement = getKeyAgreement(keyAgreementProvider);

    try
    {
        keyAgreement.init(privateKey);
        keyAgreement.doPhase(publicKey, true);
    }
    catch (java.security.InvalidKeyException e)
    {
        throw new InvalidKeyException("Invalid Key for " + getJavaAlgorithm() + " key agreement." ,e);
    }

    return keyAgreement.generateSecret();
}
 
開發者ID:RbkGh,項目名稱:Jose4j,代碼行數:18,代碼來源:EcdhKeyAgreementAlgorithm.java

示例7: testBasic

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
/** Checks that key agreement using ECDH works. */
@Test
public void testBasic() throws Exception {
  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
  ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
  keyGen.initialize(ecSpec);
  KeyPair keyPairA = keyGen.generateKeyPair();
  KeyPair keyPairB = keyGen.generateKeyPair();

  KeyAgreement kaA = KeyAgreement.getInstance("ECDH");
  KeyAgreement kaB = KeyAgreement.getInstance("ECDH");
  kaA.init(keyPairA.getPrivate());
  kaB.init(keyPairB.getPrivate());
  kaA.doPhase(keyPairB.getPublic(), true);
  kaB.doPhase(keyPairA.getPublic(), true);
  byte[] kAB = kaA.generateSecret();
  byte[] kBA = kaB.generateSecret();
  assertEquals(TestUtil.bytesToHex(kAB), TestUtil.bytesToHex(kBA));
}
 
開發者ID:google,項目名稱:wycheproof,代碼行數:20,代碼來源:EcdhTest.java

示例8: getSecretKey

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
/**
 * 構建密鑰
 * @param publicKey
 * @param privateKey
 * @return
 * @throws Exception
 */
public static byte[] getSecretKey(byte[] publicKey,byte[] privateKey)throws Exception{
	//實例化密鑰工廠
	KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
	//初始化公鑰
	//密鑰材料轉換
	X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
	//產生公鑰
	PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
	//初始化私鑰
	//密鑰材料轉換
	PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
	//產生私鑰
	PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
	//實例化
	KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm());
	keyAgree.init(priKey);
	keyAgree.doPhase(pubKey, true);
	//產生本地密鑰
	SecretKey secretKey = keyAgree.generateSecret(SECRET_ALGORITHM);
	return secretKey.getEncoded();
}
 
開發者ID:DISSIDIA-986,項目名稱:EncDecAboutJava,代碼行數:29,代碼來源:DHCoder.java

示例9: generateSharedSecret

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
private static SecretKey generateSharedSecret(PrivateKey privateKey,
		PublicKey publicKey) {
	try {
		KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH",
				BouncyCastleProvider.PROVIDER_NAME);
		keyAgreement.init(privateKey);
		keyAgreement.doPhase(publicKey, true);

		SecretKey key = keyAgreement.generateSecret("AES");
		System.out.println("Shared key length: " + key.getEncoded().length);
		return key;
	} catch (InvalidKeyException | NoSuchAlgorithmException
			| NoSuchProviderException e) {
		e.printStackTrace();
		return null;
	}
}
 
開發者ID:y12studio,項目名稱:bkbc-tools,代碼行數:18,代碼來源:HelloECDH2.java

示例10: getSecretKey

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
/**
 * 構建密鑰
 * 
 * @param publicKey
 *            公鑰
 * @param privateKey
 *            私鑰
 * @return
 * @throws Exception
 */
private static SecretKey getSecretKey(String publicKey, String privateKey)
        throws Exception {
    // 初始化公鑰
    byte[] pubKeyBytes = decryptBASE64(publicKey);
 
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKeyBytes);
    PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
 
    // 初始化私鑰
    byte[] priKeyBytes = decryptBASE64(privateKey);
 
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(priKeyBytes);
    Key priKey = keyFactory.generatePrivate(pkcs8KeySpec);
 
    KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory
            .getAlgorithm());
    keyAgree.init(priKey);
    keyAgree.doPhase(pubKey, true);
 
    // 生成本地密鑰
    SecretKey secretKey = keyAgree.generateSecret(SECRET_ALGORITHM);
 
    return secretKey;
}
 
開發者ID:zhanggh,項目名稱:mtools,代碼行數:36,代碼來源:DHCoder.java

示例11: calculateAgreedWrapKey

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
private SecretKey calculateAgreedWrapKey(String wrapAlg,
    PublicKey senderPublicKey, PrivateKey receiverPrivateKey, Provider prov)
    throws CMSException, GeneralSecurityException, IOException
{
    String agreeAlg = keyEncAlg.getAlgorithm().getId();

    if (agreeAlg.equals(CMSEnvelopedGenerator.ECMQV_SHA1KDF))
    {
        byte[] ukmEncoding = info.getUserKeyingMaterial().getOctets();
        MQVuserKeyingMaterial ukm = MQVuserKeyingMaterial.getInstance(
            ASN1Object.fromByteArray(ukmEncoding));

        PublicKey ephemeralKey = getPublicKeyFromOriginatorPublicKey(receiverPrivateKey,
            ukm.getEphemeralPublicKey(), prov);

        senderPublicKey = new MQVPublicKeySpec(senderPublicKey, ephemeralKey);
        receiverPrivateKey = new MQVPrivateKeySpec(receiverPrivateKey, receiverPrivateKey);
    }

    KeyAgreement agreement = KeyAgreement.getInstance(agreeAlg, prov);
    agreement.init(receiverPrivateKey);
    agreement.doPhase(senderPublicKey, true);
    return agreement.generateSecret(wrapAlg);
}
 
開發者ID:bullda,項目名稱:DroidText,代碼行數:25,代碼來源:KeyAgreeRecipientInformation.java

示例12: getDhResult

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
@Override
public byte[] getDhResult(byte[] aMsg, int offset , boolean isLegacyClient) throws ZrtpException {
	try {
		int PV_LENGTH = keyType.pvLengthInWords * 4;
		byte[] encodedKey = new byte[keyType.pvLengthInWords * 4 + 1];
		encodedKey[0] = 4;
		System.arraycopy(aMsg, offset, encodedKey, 1, encodedKey.length - 1);
		EncodedKeySpec keySpec = getSpec(encodedKey, keyPair.getPublic());
		PublicKey pub = keyFactory.generatePublic(keySpec);
		KeyAgreement agreement = KeyAgreement.getInstance(algorithm, "ZBC");
		agreement.init(keyPair.getPrivate());
		agreement.doPhase(pub, true);
		byte[] secret = agreement.generateSecret();
		byte[] iDHResult = null;
		if(!isLegacyClient) {
		    iDHResult = new byte[secret.length];
		    System.arraycopy(secret, 0, iDHResult, 0, secret.length);    
		} else {
		    iDHResult = new byte[PV_LENGTH];
            System.arraycopy(secret, 0, iDHResult, iDHResult.length - secret.length, secret.length);    
		}
		return iDHResult;
	} catch (Exception e) {
		throw new ZrtpException(e);
	}
}
 
開發者ID:opentelecoms-org,項目名稱:zrtp-java,代碼行數:27,代碼來源:BCDHSuite.java

示例13: getSharedSecret

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
private static byte[] getSharedSecret(KeyPair serverPair, PublicKey clientKey) throws InvalidKeyException {
    KeyAgreement agreement;

    try {
        agreement = KeyAgreement.getInstance("ECDH");
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }

    agreement.init(serverPair.getPrivate());
    agreement.doPhase(clientKey, true);
    return agreement.generateSecret();
}
 
開發者ID:KernelFreeze,項目名稱:BedrockProxy,代碼行數:14,代碼來源:Security.java

示例14: getAgreedSecret

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
/**
 * Get the secret data that has been agreed on through Diffie-Hellman
 * key agreement protocol.  Note that in the two party protocol, if
 * the peer keys are already known, no other data needs to be sent in
 * order to agree on a secret.  That is, a secured message may be
 * sent without any mandatory round-trip overheads.
 *
 * <P>It is illegal to call this member function if the private key
 * has not been set (or generated).
 *
 * @param  peerPublicKey the peer's public key.
 * @param  keyIsValidated whether the {@code peerPublicKey} has beed
 *         validated
 * @return the secret, which is an unsigned big-endian integer
 *         the same size as the Diffie-Hellman modulus.
 */
SecretKey getAgreedSecret(BigInteger peerPublicValue,
        boolean keyIsValidated) throws SSLHandshakeException {
    try {
        KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman");
        DHPublicKeySpec spec =
                    new DHPublicKeySpec(peerPublicValue, modulus, base);
        PublicKey publicKey = kf.generatePublic(spec);
        KeyAgreement ka = JsseJce.getKeyAgreement("DiffieHellman");

        // validate the Diffie-Hellman public key
        if (!keyIsValidated &&
                !KeyUtil.isOracleJCEProvider(ka.getProvider().getName())) {
            try {
                KeyUtil.validate(spec);
            } catch (InvalidKeyException ike) {
                // prefer handshake_failure alert to internal_error alert
                throw new SSLHandshakeException(ike.getMessage());
            }
        }

        ka.init(privateKey);
        ka.doPhase(publicKey, true);
        return ka.generateSecret("TlsPremasterSecret");
    } catch (GeneralSecurityException e) {
        throw (SSLHandshakeException) new SSLHandshakeException(
            "Could not generate secret").initCause(e);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:45,代碼來源:DHCrypt.java

示例15: SymmetricKeyGenerator

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
public SymmetricKeyGenerator(DHPublicKey pubKey, DHPrivateKey priKey) throws Exception
{
    byte[] publicKey = pubKey.getEncoded();
    byte[] privateKey = priKey.getEncoded();
    java.security.KeyFactory keyFactory = KeyFactory.getInstance("DH");
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
    PublicKey pubKey1 = keyFactory.generatePublic(x509KeySpec);
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
    PrivateKey priKey1 = keyFactory.generatePrivate(pkcs8KeySpec);
    KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm());
    keyAgree.init(priKey1);
    keyAgree.doPhase(pubKey1, true);
    secretKey = keyAgree.generateSecret(KEY_ALGORITHM);
}
 
開發者ID:SlientWizard,項目名稱:Zen-of-Affair,代碼行數:15,代碼來源:SymmetricKeyGenerator.java


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