本文整理汇总了Java中org.spongycastle.crypto.params.DHPublicKeyParameters类的典型用法代码示例。如果您正苦于以下问题:Java DHPublicKeyParameters类的具体用法?Java DHPublicKeyParameters怎么用?Java DHPublicKeyParameters使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DHPublicKeyParameters类属于org.spongycastle.crypto.params包,在下文中一共展示了DHPublicKeyParameters类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validateDHKeyPair
import org.spongycastle.crypto.params.DHPublicKeyParameters; //导入依赖的package包/类
/**
* A helper routine that verifies that a given Diffie-Hellman keypair is valid.
*
* @return Returns the private key as a BigInteger for further verification.
*/
public BigInteger validateDHKeyPair(AsymmetricCipherKeyPair pair) {
DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair.getPrivate();
DHPublicKeyParameters pub = (DHPublicKeyParameters) pair.getPublic();
assertNotNull(priv);
assertNotNull(pub);
// Check that the public key == g^(private key) mod p.
// This test is overkill, but validates that the we've hooked into the right library.
BigInteger g = Crypto.DH_GROUP_PARAMETERS.getG();
BigInteger p = Crypto.DH_GROUP_PARAMETERS.getP();
assertEquals("Tests that the public key y == g^(private key x) mod p",
pub.getY(), g.modPow(priv.getX(), p));
BigInteger foo = new BigInteger(Crypto.DH_SUBGROUP_SIZE, Crypto.random);
BigInteger gToTheFoo = g.modPow(foo, Crypto.DH_GROUP_PARAMETERS.getP());
BigInteger fooInverse = foo.modInverse(Crypto.DH_GROUP_PARAMETERS.getQ());
BigInteger newG = gToTheFoo.modPow(fooInverse, Crypto.DH_GROUP_PARAMETERS.getP());
assertEquals("Tests that we can inverse group exponentiation", g, newG);
return priv.getX();
}
示例2: encodeDecodeKeysTest
import org.spongycastle.crypto.params.DHPublicKeyParameters; //导入依赖的package包/类
/**
* Check that the individual encode/decode private/public key methods reverse each other.
* Check that the generatePublicID/generatePrivateID methods are reversed by the
* corresponding decode method.
*/
@Test
public void encodeDecodeKeysTest() throws IOException {
AsymmetricCipherKeyPair keypair = Crypto.generateDHKeyPair();
DHPrivateKeyParameters privKey = (DHPrivateKeyParameters) keypair.getPrivate();
DHPublicKeyParameters pubKey = (DHPublicKeyParameters) keypair.getPublic();
assertEquals(pubKey, Crypto.decodeDHPublicKey(Crypto.encodeDHPublicKey(pubKey)));
assertEquals(privKey, Crypto.decodeDHPrivateKey(Crypto.encodeDHPrivateKey(privKey)));
assertEquals(pubKey, Crypto.decodeDHPublicKey(Crypto.generatePublicID(keypair)));
assertEquals(privKey, Crypto.decodeDHPrivateKey(Crypto.generatePrivateID(keypair)));
// Took this out because sometimes pubkey is 129 and sometimes it's 128.
// Not sure why that is (or what else to test here).
// int BYTES_IN_PUBKEY = 128;
// int BYTES_IN_PRIVKEY = 21;
// assertEquals(BYTES_IN_PUBKEY, Crypto.encodeDHPublicKey(pubKey).length);
// assertEquals(BYTES_IN_PRIVKEY, Crypto.encodeDHPrivateKey(privKey).length);
}
示例3: encodeDecodePublicKeyTest
import org.spongycastle.crypto.params.DHPublicKeyParameters; //导入依赖的package包/类
/**
* Tests that we can encode and decode public keys.
*/
@Test
public void encodeDecodePublicKeyTest() {
AsymmetricCipherKeyPair pair = Crypto.generateDHKeyPair();
byte[] encoded1 = Crypto.encodeDHPublicKey((DHPublicKeyParameters) pair.getPublic());
assertNotNull(encoded1);
DHPublicKeyParameters pub = Crypto.decodeDHPublicKey(encoded1);
byte[] encoded2 = Crypto.encodeDHPublicKey((DHPublicKeyParameters) pub);
assertNotNull(encoded2);
assertArrayEquals("Testing that pubkey encoding/decoding", encoded1, encoded2);
}
示例4: generateIDAndEncodeTest
import org.spongycastle.crypto.params.DHPublicKeyParameters; //导入依赖的package包/类
/**
* Tests that the the public ID generated is the same regardless of how it's produced.
*/
@Test
public void generateIDAndEncodeTest() {
AsymmetricCipherKeyPair id = Crypto.generateUserID();
assertNotNull(id);
byte[] encoded1 = Crypto.encodeDHPublicKey((DHPublicKeyParameters) id.getPublic());
byte[] encoded2 = Crypto.generatePublicID(id);
assertArrayEquals("Testing that public ID generation works", encoded1, encoded2);
}
示例5: generateDHKeyPair
import org.spongycastle.crypto.params.DHPublicKeyParameters; //导入依赖的package包/类
public KeyPair generateDHKeyPair() throws OtrCryptoException {
// Generate a AsymmetricCipherKeyPair using BC.
DHParameters dhParams = new DHParameters(MODULUS, GENERATOR, null,
DH_PRIVATE_KEY_MINIMUM_BIT_LENGTH);
DHKeyGenerationParameters params = new DHKeyGenerationParameters(new SecureRandom(),
dhParams);
DHKeyPairGenerator kpGen = new DHKeyPairGenerator();
kpGen.init(params);
AsymmetricCipherKeyPair pair = kpGen.generateKeyPair();
// Convert this AsymmetricCipherKeyPair to a standard JCE KeyPair.
DHPublicKeyParameters pub = (DHPublicKeyParameters) pair.getPublic();
DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair.getPrivate();
try {
KeyFactory keyFac = KeyFactory.getInstance("DH");
DHPublicKeySpec pubKeySpecs = new DHPublicKeySpec(pub.getY(), MODULUS, GENERATOR);
DHPublicKey pubKey = (DHPublicKey) keyFac.generatePublic(pubKeySpecs);
DHParameters dhParameters = priv.getParameters();
DHPrivateKeySpec privKeySpecs = new DHPrivateKeySpec(priv.getX(), dhParameters.getP(),
dhParameters.getG());
DHPrivateKey privKey = (DHPrivateKey) keyFac.generatePrivate(privKeySpecs);
return new KeyPair(pubKey, privKey);
} catch (Exception e) {
throw new OtrCryptoException(e);
}
}
示例6: main
import org.spongycastle.crypto.params.DHPublicKeyParameters; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
Enumeration<?> names = SECNamedCurves.getNames();
while (names.hasMoreElements()) {
System.out.println("\t" + (String) names.nextElement());
}
// All clients must agree on the curve to use by agreement. Bitcoin uses secp256k1.
// X9ECParameters params = SECNamedCurves.getByName("secp256k1");
BigInteger g512 = new BigInteger("20145d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16);
BigInteger p512 = new BigInteger("2015fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16);
DHKeyPairGenerator kpGen = getDHKeyPairGenerator(g512, p512);
//
// generate first pair
//
AsymmetricCipherKeyPair pair = kpGen.generateKeyPair();
DHPublicKeyParameters pu1 = (DHPublicKeyParameters) pair.getPublic();
DHPrivateKeyParameters pv1 = (DHPrivateKeyParameters) pair.getPrivate();
//
// generate second pair
//
pair = kpGen.generateKeyPair();
DHPublicKeyParameters pu2 = (DHPublicKeyParameters) pair.getPublic();
DHPrivateKeyParameters pv2 = (DHPrivateKeyParameters) pair.getPrivate();
//
// two way ?
//
DHAgreement e1 = new DHAgreement();
DHAgreement e2 = new DHAgreement();
e1.init(pv1);
e2.init(pv2);
BigInteger m1 = e1.calculateMessage();
BigInteger m2 = e2.calculateMessage();
BigInteger k1 = e1.calculateAgreement(pu2, m2);
BigInteger k2 = e2.calculateAgreement(pu1, m1);
System.out.println(k1);
System.out.println(k2);
if (!k1.equals(k2)) {
System.out.println("test failed.");
}else{
System.out.println("test ok.");
}
}
示例7: encodeDHPublicKey
import org.spongycastle.crypto.params.DHPublicKeyParameters; //导入依赖的package包/类
/**
* Extracts a byte array representation of a public key given a keypair.
*
* @param pubkey The keypair from which to get the public key.
*
* @return The underlying byte array of the public key or null upon failure.
*/
protected static byte[] encodeDHPublicKey(DHPublicKeyParameters pubkey) {
return pubkey.getY().toByteArray();
}
示例8: decodeDHPublicKey
import org.spongycastle.crypto.params.DHPublicKeyParameters; //导入依赖的package包/类
/**
* Creates a public key object from the given encoded key.
*
* @param encoded The encoded key bytes to generate the key from.
*
* @return The PublicKey object or null upon failure.
*/
protected static DHPublicKeyParameters decodeDHPublicKey(byte[] encoded) {
BigInteger i = new BigInteger(encoded);
return new DHPublicKeyParameters(i, DH_GROUP_PARAMETERS);
}
示例9: generatePublicID
import org.spongycastle.crypto.params.DHPublicKeyParameters; //导入依赖的package包/类
/**
* Generates a public ID that can be shared with friends given a user's long-term ID.
*
* @param id The user ID represented as their key pair.
*
* @return An encoded form of the public ID (public key) or null upon failure.
*/
public static byte[] generatePublicID(AsymmetricCipherKeyPair id) {
return encodeDHPublicKey((DHPublicKeyParameters) id.getPublic());
}