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


Java KeyAgreement.getInstance方法代碼示例

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


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

示例1: generateSecret

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
protected static byte[] generateSecret(String dhSKAlgo, PrivateKey privateKey,
    PublicKey otherPublicKey) throws Exception {
  KeyAgreement ka = KeyAgreement.getInstance("DH");
  ka.init(privateKey);
  ka.doPhase(otherPublicKey, true);

  int keysize = getKeySize(dhSKAlgo);
  int blocksize = getBlockSize(dhSKAlgo);

  if (keysize == -1 || blocksize == -1) {
    SecretKey sKey = ka.generateSecret(dhSKAlgo);
    return sKey.getEncoded();
  } else {
    return ka.generateSecret();
  }
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:17,代碼來源:GMSEncrypt.java

示例2: testDh

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
/** Check that key agreement using DH works. */
@SuppressWarnings("InsecureCryptoUsage")
@Test
public void testDh() throws Exception {
  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
  DHParameterSpec dhparams = ike2048();
  keyGen.initialize(dhparams);
  KeyPair keyPairA = keyGen.generateKeyPair();
  KeyPair keyPairB = keyGen.generateKeyPair();

  KeyAgreement kaA = KeyAgreement.getInstance("DH");
  KeyAgreement kaB = KeyAgreement.getInstance("DH");
  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,代碼行數:21,代碼來源:DhTest.java

示例3: 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:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:TestECDH2.java

示例4: 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

示例5: getSharedSecret

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
public static ECDHKeySet getSharedSecret (ECKey keyServer, ECKey keyClient) {
    try {

        ECPrivateKeySpec specPrivate = new ECPrivateKeySpec(keyServer.getPrivKey(), ecParameters);
        ECPublicKeySpec specPublic = new ECPublicKeySpec(new ECPoint(keyClient.getPubKeyPoint().getXCoord().toBigInteger(), keyClient.getPubKeyPoint()
                .getYCoord().toBigInteger()), ecParameters);

        ECPrivateKey privateKey = (ECPrivateKey) kf.generatePrivate(specPrivate);
        ECPublicKey publicKey = (ECPublicKey) kf.generatePublic(specPublic);

        JCEECPrivateKey ecPrivKey = new JCEECPrivateKey(privateKey);
        JCEECPublicKey ecPubKey = new JCEECPublicKey(publicKey);

        KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH");
        aKeyAgree.init(ecPrivKey);
        aKeyAgree.doPhase(ecPubKey, true);

        return new ECDHKeySet(aKeyAgree.generateSecret(), keyServer.getPubKey(), keyClient.getPubKey());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
開發者ID:blockchain,項目名稱:thunder,代碼行數:23,代碼來源:ECDH.java

示例6: run

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
@Override
public void run() {
    try {
        Log.d(TAG, "read start!");
        DERObject responseDERObject = readDERObject();
        Log.d(TAG, "read finished!");
        Log.d(TAG, "write start!");
        writeDERObject(new DERObject(this.getKeyPair().getPublic().getEncoded()));
        Log.d(TAG, "write finished!");
        KeyFactory keyFactory = KeyFactory.getInstance("ECDH", "SC");
        X509EncodedKeySpec x509ks = new X509EncodedKeySpec(responseDERObject.getPayload());
        PublicKey publicKey = keyFactory.generatePublic(x509ks);
        KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH", "SC");
        keyAgreement.init(this.getKeyPair().getPrivate());
        keyAgreement.doPhase(publicKey, true);
        final byte[] sharedKey = Arrays.copyOfRange(keyAgreement.generateSecret(), 0, Constants.SYMMETRIC_KEY_SIZE);
        Log.d(TAG, "exchange finished!");
        this.getResultListener().onSuccess(new SecretKeySpec(sharedKey, Constants.SYMMETRIC_CIPHER_ALGORITH));
    } catch (Exception e) {
        this.getResultListener().onError(e.getMessage());
    }
}
 
開發者ID:coinblesk,項目名稱:coinblesk-client-gui,代碼行數:23,代碼來源:DHKeyExchangeServerHandler.java

示例7: run

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
@Override
public void run() {
    try {
        Log.d(TAG, "write start!");
        writeDERObject(new DERObject(this.getKeyPair().getPublic().getEncoded()));
        Log.d(TAG, "write finished!");
        Log.d(TAG, "read start!");
        DERObject responseDERObject = readDERObject();
        Log.d(TAG, "read finished!");
        KeyFactory keyFactory = KeyFactory.getInstance("ECDH", "SC");
        X509EncodedKeySpec x509ks = new X509EncodedKeySpec(responseDERObject.getPayload());
        PublicKey publicKey = keyFactory.generatePublic(x509ks);
        KeyAgreement keyAgreement = KeyAgreement.getInstance("ECDH", "SC");
        keyAgreement.init(this.getKeyPair().getPrivate());
        keyAgreement.doPhase(publicKey, true);
        final byte[] sharedKey = Arrays.copyOfRange(keyAgreement.generateSecret(), 0, Constants.SYMMETRIC_KEY_SIZE);
        Log.d(TAG, "exchange finished!");
        this.getResultListener().onSuccess(new SecretKeySpec(sharedKey, Constants.SYMMETRIC_CIPHER_ALGORITH));
    } catch (Exception e) {
        this.getResultListener().onError(e.getMessage());
    }
}
 
開發者ID:coinblesk,項目名稱:coinblesk-client-gui,代碼行數:23,代碼來源:DHKeyExchangeClientHandler.java

示例8: initDiffieHellman

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
protected void initDiffieHellman(DHPrivateKey dhKey, SecureRandom random)
  throws SSLException
{
  try
    {
      keyAgreement = KeyAgreement.getInstance("DH");
      keyAgreement.init(dhKey, random);
    }
  catch (InvalidKeyException ike)
    {
      throw new SSLException(ike);
    }
  catch (NoSuchAlgorithmException nsae)
    {
      throw new SSLException(nsae);
    }
}
 
開發者ID:vilie,項目名稱:javify,代碼行數:18,代碼來源:AbstractHandshake.java

示例9: 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

示例10: DHExchange

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
/**
 * Use 1536 for Fast Prime Generation (less secure) or 2048 for slow prime generation (secure)
 * @param strength
 * @throws InvalidKeyException
 * @throws IllegalStateException
 * @throws InvalidAlgorithmParameterException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 * @throws InvalidKeySpecException
 * @throws InvalidParameterSpecException
 */
public DHExchange(int strength) throws InvalidKeyException, IllegalStateException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidParameterSpecException {
	Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider());
	
	AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance("DH", "SC");
	paramGen.init(strength); // number of bits
	AlgorithmParameters params = paramGen.generateParameters();
	DHParameterSpec dhSpec = (DHParameterSpec)params.getParameterSpec(DHParameterSpec.class);

	BigInteger p = dhSpec.getP();
	BigInteger g = dhSpec.getG();
	
    DHParameterSpec dhParams = new DHParameterSpec(p, g);
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH", "SC");
    keyGen.initialize(dhParams, new SecureRandom());
    KeyAgreement aKeyAgree = KeyAgreement.getInstance("DH", "SC");
    KeyPair aPair = keyGen.generateKeyPair();
    KeyAgreement bKeyAgree = KeyAgreement.getInstance("DH", "SC");
    KeyPair bPair = keyGen.generateKeyPair();

    aKeyAgree.init(aPair.getPrivate());
    bKeyAgree.init(bPair.getPrivate());
    
    aKeyAgree.doPhase(bPair.getPublic(), true);
    bKeyAgree.doPhase(aPair.getPublic(), true);
}
 
開發者ID:manuelsc,項目名稱:Raven-Messenger,代碼行數:37,代碼來源:DHExchange.java

示例11: testGetInstanceString02

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
/**
 * Test for <code> getInstance(String algorithm) </code> method Assertions:
 * returns KeyAgreement object
 */
@TestTargetNew(
    level = TestLevel.PARTIAL_COMPLETE,
    notes = "This is a complete subset of tests for getInstance method.",
    method = "getInstance",
    args = {java.lang.String.class}
)
public void testGetInstanceString02() throws NoSuchAlgorithmException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyAgreement keyA;
    for (int i = 0; i < validValues.length; i++) {
        keyA = KeyAgreement.getInstance(validValues[i]);
        assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
                validValues[i]);
    }
}
 
開發者ID:keplersj,項目名稱:In-the-Box-Fork,代碼行數:23,代碼來源:KeyAgreementTest.java

示例12: testGetInstanceStringProvider03

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
/**
 * Test for <code> getInstance(String algorithm, Provider provider)</code>
 * method Assertions: returns KeyAgreement object
 */
@TestTargetNew(
    level = TestLevel.PARTIAL_COMPLETE,
    notes = "This is a complete subset of tests for getInstance method.",
    method = "getInstance",
    args = {java.lang.String.class, java.security.Provider.class}
)
public void testGetInstanceStringProvider03()
        throws IllegalArgumentException, NoSuchAlgorithmException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyAgreement keyA;
    for (int i = 0; i < validValues.length; i++) {
        keyA = KeyAgreement.getInstance(validValues[i], defaultProvider);
        assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
                validValues[i]);
        assertEquals("Incorrect provider", keyA.getProvider(),
                defaultProvider);
    }
}
 
開發者ID:keplersj,項目名稱:In-the-Box-Fork,代碼行數:26,代碼來源:KeyAgreementTest.java

示例13: test_initLjava_security_KeyLjava_security_SecureRandom

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
@TestTargetNew(
        level = TestLevel.COMPLETE,
        notes = "",
        method = "init",
        args = {java.security.Key.class, java.security.SecureRandom.class}
    )
public void test_initLjava_security_KeyLjava_security_SecureRandom() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    KeyAgreement ka = KeyAgreement.getInstance("DH");

    ka.init(privKey, new SecureRandom());
    try {
        ka.init(publKey, new SecureRandom());
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
        //expected
    }
}
 
開發者ID:keplersj,項目名稱:In-the-Box-Fork,代碼行數:24,代碼來源:KeyAgreementTest.java

示例14: testGetInstanceStringProvider03

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
/**
 * Test for <code> getInstance(String algorithm, Provider provider)</code>
 * method Assertions: returns KeyAgreement object
 */
public void testGetInstanceStringProvider03()
        throws IllegalArgumentException, NoSuchAlgorithmException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyAgreement keyA;
    for (int i = 0; i < validValues.length; i++) {
        keyA = KeyAgreement.getInstance(validValues[i], defaultProvider);
        assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
                validValues[i]);
        assertEquals("Incorrect provider", keyA.getProvider(),
                defaultProvider);
    }
}
 
開發者ID:shannah,項目名稱:cn1,代碼行數:20,代碼來源:KeyAgreementTest.java

示例15: generateKeyPair

import javax.crypto.KeyAgreement; //導入方法依賴的package包/類
/**
 * Creates a Diffie-Hellman key pair.
 * 
 * @return dh keypair
 */
protected KeyPair generateKeyPair() {
    KeyPair keyPair = null;
    DHParameterSpec keySpec = new DHParameterSpec(DH_MODULUS, DH_BASE);
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
        keyGen.initialize(keySpec);
        keyPair = keyGen.generateKeyPair();
        keyAgreement = KeyAgreement.getInstance("DH");
        // key agreement is initialized with "this" ends private key
        keyAgreement.init(keyPair.getPrivate());
    } catch (Exception e) {
        log.error("Error generating keypair", e);
    }
    return keyPair;
}
 
開發者ID:Red5,項目名稱:red5-server-common,代碼行數:21,代碼來源:RTMPHandshake.java


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