本文整理汇总了Java中org.bouncycastle.crypto.agreement.ECDHBasicAgreement.init方法的典型用法代码示例。如果您正苦于以下问题:Java ECDHBasicAgreement.init方法的具体用法?Java ECDHBasicAgreement.init怎么用?Java ECDHBasicAgreement.init使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.crypto.agreement.ECDHBasicAgreement
的用法示例。
在下文中一共展示了ECDHBasicAgreement.init方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateSharedSecret
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement; //导入方法依赖的package包/类
/**
* Generate a shared AES key using ECDH.
*/
public static byte[] generateSharedSecret(byte[] privateKey, byte[] publicKey) {
try {
ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(SECP256K1);
ECDomainParameters domain =
new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN(), spec.getH());
ECPublicKeyParameters pubKey =
new ECPublicKeyParameters(spec.getCurve().decodePoint(publicKey), domain);
ECPrivateKeyParameters prvkey =
new ECPrivateKeyParameters(new BigInteger(1, privateKey), domain);
ECDHBasicAgreement agreement = new ECDHBasicAgreement();
agreement.init(prvkey);
byte[] password = agreement.calculateAgreement(pubKey).toByteArray();
return Aes.generateKey(ByteUtilities.toHexString(password), password);
} catch (Exception e) {
LOGGER.error(null, e);
return new byte[0];
}
}
示例2: curveTest
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement; //导入方法依赖的package包/类
@Test
public void curveTest() {
final X9ECParameters curve = NISTNamedCurves.getByName("P-384");
final ECDomainParameters domainParameters = new ECDomainParameters(
curve.getCurve(), curve.getG(), curve.getN());
final SecureRandom random = new SecureRandom();
final ECKeyPairGenerator gen = new ECKeyPairGenerator();
gen.init(new ECKeyGenerationParameters(domainParameters, random));
final AsymmetricCipherKeyPair senderPair = gen.generateKeyPair();
final AsymmetricCipherKeyPair receiverPair = gen.generateKeyPair();
final ECDHBasicAgreement senderAgreement = new ECDHBasicAgreement();
senderAgreement.init(senderPair.getPrivate());
final BigInteger senderResult = senderAgreement.calculateAgreement(
receiverPair.getPublic());
final ECDHBasicAgreement receiverAgreement = new ECDHBasicAgreement();
receiverAgreement.init(receiverPair.getPrivate());
final BigInteger receiverResult = receiverAgreement.calculateAgreement(
senderPair.getPublic());
assertEquals(senderResult, receiverResult);
//System.out.println(receiverResult);
}
示例3: calculateECDHBasicAgreement
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement; //导入方法依赖的package包/类
public static byte[] calculateECDHBasicAgreement(ECPublicKeyParameters publicKey, ECPrivateKeyParameters privateKey)
{
ECDHBasicAgreement basicAgreement = new ECDHBasicAgreement();
basicAgreement.init(privateKey);
BigInteger agreementValue = basicAgreement.calculateAgreement(publicKey);
/*
* RFC 4492 5.10. Note that this octet string (Z in IEEE 1363 terminology) as output by
* FE2OSP, the Field Element to Octet String Conversion Primitive, has constant length for
* any given field; leading zeros found in this octet string MUST NOT be truncated.
*/
return BigIntegers.asUnsignedByteArray(basicAgreement.getFieldSize(), agreementValue);
}
示例4: calculateECDHBasicAgreement
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement; //导入方法依赖的package包/类
public static byte[] calculateECDHBasicAgreement(ECPublicKeyParameters publicKey, ECPrivateKeyParameters privateKey)
{
ECDHBasicAgreement basicAgreement = new ECDHBasicAgreement();
basicAgreement.init(privateKey);
BigInteger agreementValue = basicAgreement.calculateAgreement(publicKey);
/*
* RFC 4492 5.10. Note that this octet string (Z in IEEE 1363 terminology) as output by
* FE2OSP, the Field Element to Octet String Conversion Primitive, has constant length for
* any given field; leading zeros found in this octet string MUST NOT be truncated.
*/
return BigIntegers.asUnsignedByteArray(basicAgreement.getFieldSize(), agreementValue);
}
示例5: main
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement; //导入方法依赖的package包/类
public static void main( String[] args ) throws InvalidKeySpecException, InvalidKeyException, IOException {
ECPublicKey clientKey = (ECPublicKey) ECDH_KEY_FACTORY.generatePublic( new X509EncodedKeySpec( Base64.getDecoder().decode( "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEDEKneqEvcqUqqFMM1HM1A4zWjJC+I8Y+aKzG5dl+6wNOHHQ4NmG2PEXRJYhujyodFH+wO0dEr4GM1WoaWog8xsYQ6mQJAC0eVpBM96spUB1eMN56+BwlJ4H3Qx4TAvAs" ) ) );
ECPrivateKey privateKey = (ECPrivateKey) ECDH_KEY_FACTORY.generatePrivate( new PKCS8EncodedKeySpec( Base64.getDecoder().decode( "MB8CAQAwEAYHKoZIzj0CAQYFK4EEACIECDAGAgEBBAEB" ) ) );
ECDHBasicAgreement agreement = new ECDHBasicAgreement();
agreement.init( PrivateKeyFactory.createKey( privateKey.getEncoded() ) );
byte[] secret = agreement.calculateAgreement( PublicKeyFactory.createKey( clientKey.getEncoded() ) ).toByteArray();
System.out.println( Util.toHexString( secret ) );
System.out.println( Util.toHexString( Base64.getDecoder().decode( "DEKneqEvcqUqqFMM1HM1A4zWjJC+I8Y+aKzG5dl+6wNOHHQ4NmG2PEXRJYhujyod" ) ) );
}
示例6: sessionKey
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement; //导入方法依赖的package包/类
public static AesKey sessionKey(
final CipherParameters receiverKey,
final CipherParameters senderKey,
final CipherParameters privateKey,
final ECPublicKeyParameters publicKey
) {
final ECDHBasicAgreement senderAgreement = new ECDHBasicAgreement();
senderAgreement.init(privateKey);
final EcdhSharedSecret sharedSecret = new EcdhSharedSecret(
((ECPublicKeyParameters)receiverKey).getQ(),
((ECPublicKeyParameters)senderKey).getQ(),
senderAgreement.calculateAgreement(publicKey));
final DigestSha384 hash = DigestSha384.digest(sharedSecret);
return new AesKey(Arrays.copyOf(hash.getBytes(), AesKey.AES_KEY_BYTES));
}