use of javax.crypto.spec.DHParameterSpec in project wycheproof by google.
the class DhiesTest method testDhiesBasic.
/**
* WARNING: This test uses weak crypto (i.e. DHIESWithAES), if supported. Checks that key
* agreement using DHIES works in the sense that it can decrypt what it encrypts. Unfortunately it
* seems that there is no secure mode using AES.
*/
@SuppressWarnings("InsecureCryptoUsage")
public void testDhiesBasic() throws Exception {
DHParameterSpec params = ike2048();
KeyPairGenerator kf = KeyPairGenerator.getInstance("DH");
kf.initialize(params);
KeyPair keyPair = kf.generateKeyPair();
PrivateKey priv = keyPair.getPrivate();
PublicKey pub = keyPair.getPublic();
byte[] message = "Hello".getBytes("UTF-8");
Cipher dhies;
try {
dhies = Cipher.getInstance("DHIESwithAES");
} catch (NoSuchAlgorithmException ex) {
// The algorithm isn't supported - even better!
return;
}
dhies.init(Cipher.ENCRYPT_MODE, pub);
byte[] ciphertext = dhies.doFinal(message);
System.out.println("testDhiesBasic:" + TestUtil.bytesToHex(ciphertext));
dhies.init(Cipher.DECRYPT_MODE, priv);
byte[] decrypted = dhies.doFinal(ciphertext);
assertEquals(TestUtil.bytesToHex(message), TestUtil.bytesToHex(decrypted));
}
use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.
the class BCDHPrivateKey method readObject.
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
this.dhSpec = new DHParameterSpec((BigInteger) in.readObject(), (BigInteger) in.readObject(), in.readInt());
this.info = null;
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
}
use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.
the class KeyAgreementSpi method engineInit.
protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
if (!(key instanceof DHPrivateKey)) {
throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
}
DHPrivateKey privKey = (DHPrivateKey) key;
if (params != null) {
if (!(params instanceof DHParameterSpec)) {
throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
}
DHParameterSpec p = (DHParameterSpec) params;
this.p = p.getP();
this.g = p.getG();
} else {
this.p = privKey.getParams().getP();
this.g = privKey.getParams().getG();
}
this.x = this.result = privKey.getX();
}
use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.
the class KeyPairGeneratorSpi method initialize.
public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
if (!(params instanceof DHParameterSpec)) {
throw new InvalidAlgorithmParameterException("parameter object not a DHParameterSpec");
}
DHParameterSpec dhParams = (DHParameterSpec) params;
param = new DHKeyGenerationParameters(random, new DHParameters(dhParams.getP(), dhParams.getG(), null, dhParams.getL()));
engine.init(param);
initialised = true;
}
use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.
the class KeyPairGeneratorSpi method generateKeyPair.
public KeyPair generateKeyPair() {
if (!initialised) {
Integer paramStrength = Integers.valueOf(strength);
if (params.containsKey(paramStrength)) {
param = (DHKeyGenerationParameters) params.get(paramStrength);
} else {
DHParameterSpec dhParams = BouncyCastleProvider.CONFIGURATION.getDHDefaultParameters(strength);
if (dhParams != null) {
param = new DHKeyGenerationParameters(random, new DHParameters(dhParams.getP(), dhParams.getG(), null, dhParams.getL()));
} else {
synchronized (lock) {
// our key size.
if (params.containsKey(paramStrength)) {
param = (DHKeyGenerationParameters) params.get(paramStrength);
} else {
DHParametersGenerator pGen = new DHParametersGenerator();
pGen.init(strength, certainty, random);
param = new DHKeyGenerationParameters(random, pGen.generateParameters());
params.put(paramStrength, param);
}
}
}
}
engine.init(param);
initialised = true;
}
AsymmetricCipherKeyPair pair = engine.generateKeyPair();
DHPublicKeyParameters pub = (DHPublicKeyParameters) pair.getPublic();
DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair.getPrivate();
return new KeyPair(new BCDHPublicKey(pub), new BCDHPrivateKey(priv));
}
Aggregations