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


Java KeyAgreement類代碼示例

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


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

示例1: getSecretKey

import javax.crypto.KeyAgreement; //導入依賴的package包/類
/**
 * 根據對方的公鑰和自己的私鑰生成本地密鑰
 */
public static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception{
	//實例化密鑰工廠
	KeyFactory keyFactory = KeyFactory.getInstance("DH");
	//將公鑰從字節數組轉換為publicKey
	X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKey);
	PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
	//將私鑰從字節數組轉換為privateKey
	PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(privateKey);
	PrivateKey priKey = keyFactory.generatePrivate(priKeySpec);
	//準備根據以上公鑰和私鑰生成本地密鑰SecretKey
	//先實例化KeyAgreement
	KeyAgreement keyAgreement = KeyAgreement.getInstance("DH");
	//用自己的私鑰初始化keyAgreement
	keyAgreement.init(priKey);
	//結合對方的公鑰進行運算
	keyAgreement.doPhase(pubKey, true);
	//開始生成本地密鑰secretKey   密鑰算法為對稱密碼算法
	SecretKey secretKey = keyAgreement.generateSecret("DES");  //DES、3DES、AES
	return secretKey.getEncoded();
}
 
開發者ID:laidu,項目名稱:java-learn,代碼行數:24,代碼來源:DHUtil.java

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

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

示例4: testGetKeyAgreementFromProvider

import javax.crypto.KeyAgreement; //導入依賴的package包/類
@Test
public void testGetKeyAgreementFromProvider()
    throws NoSuchProviderException, NoSuchAlgorithmException {

    KeyAgreement ka;

    /* try to get all available options we expect to have */
    for (int i = 0; i < wolfJCEAlgos.length; i++) {
        ka = KeyAgreement.getInstance(wolfJCEAlgos[i], "wolfJCE");
    }

    /* getting a garbage algorithm should throw an exception */
    try {
        ka = KeyAgreement.getInstance("NotValid", "wolfJCE");

        fail("KeyAgreement.getInstance should throw " +
             "NoSuchAlgorithmException when given bad algorithm value");

    } catch (NoSuchAlgorithmException e) { }
}
 
開發者ID:wolfSSL,項目名稱:wolfcrypt-jni,代碼行數:21,代碼來源:WolfCryptKeyAgreementTest.java

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

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

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

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

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

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

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

示例12: testDHDistinctParameters

import javax.crypto.KeyAgreement; //導入依賴的package包/類
/** This test tries a key agreement with keys using distinct parameters. */
@SuppressWarnings("InsecureCryptoUsage")
@Test
public void testDHDistinctParameters() throws Exception {
  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
  keyGen.initialize(ike1536());
  KeyPair keyPairA = keyGen.generateKeyPair();

  keyGen.initialize(ike2048());
  KeyPair keyPairB = keyGen.generateKeyPair();

  KeyAgreement kaA = KeyAgreement.getInstance("DH");
  kaA.init(keyPairA.getPrivate());
  try {
    kaA.doPhase(keyPairB.getPublic(), true);
    byte[] kAB = kaA.generateSecret();
    fail("Generated secrets with mixed keys " + TestUtil.bytesToHex(kAB) + ", ");
  } catch (java.security.GeneralSecurityException ex) {
    // This is expected.
  }
}
 
開發者ID:google,項目名稱:wycheproof,代碼行數:22,代碼來源:DhTest.java

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

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

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


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