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


Java KeyAgreement.init方法代码示例

本文整理汇总了Java中javax.crypto.KeyAgreement.init方法的典型用法代码示例。如果您正苦于以下问题:Java KeyAgreement.init方法的具体用法?Java KeyAgreement.init怎么用?Java KeyAgreement.init使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.crypto.KeyAgreement的用法示例。


在下文中一共展示了KeyAgreement.init方法的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: 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

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

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

示例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:campolake,项目名称:openjdk9,代码行数:18,代码来源:TestECDH2.java

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

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

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

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

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

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

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

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

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


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