Example usage for org.bouncycastle.crypto.agreement ECDHBasicAgreement init

List of usage examples for org.bouncycastle.crypto.agreement ECDHBasicAgreement init

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.agreement ECDHBasicAgreement init.

Prototype

public void init(CipherParameters key) 

Source Link

Usage

From source file:card.CardClient.java

License:Open Source License

/**
 * Get an attribute from the card/*w  w w  . j a va2  s  .  c  o  m*/
 * 
 * @param i Index of the attribute.
 * @return Blinded public key, blinded attribute signature and the attribute
 */
public BigInteger[] getAttribute(byte id, ECPoint nonce) {
    BigInteger[] result = new BigInteger[3];

    int i = 0;
    while (i < attribute.length && attribute_id[i] != id)
        i++;

    if (i >= attribute.length || attribute_id[i] != id) {
        return null;
    }
    result[ATTRIBUTE] = attribute[i];

    // generate a blinding factor b
    blinder = (ECPrivateKey) keyGen.generateKeyPair().getPrivate();

    // blind public key, attribute signature and signed nonce
    try {
        ECDHBasicAgreement agreement = new ECDHBasicAgreement();
        agreement.init(new ECPrivateKeyParameters(blinder.getD(), ecDom));

        result[BLINDED_KEY] = agreement
                .calculateAgreement(new ECPublicKeyParameters(((ECPublicKey) keys.getPublic()).getQ(), ecDom));
        result[BLINDED_SIGNATURE] = agreement
                .calculateAgreement(new ECPublicKeyParameters(signature[i], ecDom));
        result[SIGNED_NONCE] = agreement.calculateAgreement(
                new ECPublicKeyParameters(nonce.multiply(((ECPrivateKey) keys.getPrivate()).getD()), ecDom));
    } catch (Exception e) {
        e.printStackTrace();
    }

    // return blinded public key, blinded attribute signature, blinded signed nonce, attribute
    return result;
}

From source file:org.fdroid.enigtext.crypto.AsymmetricMasterCipher.java

License:Open Source License

public String decryptBody(String body) throws IOException, org.fdroid.enigtext.crypto.InvalidMessageException {
    try {//from   w w  w . ja  v  a2  s . co  m
        byte[] combined = Base64.decode(body);
        PublicKey theirPublicKey = new PublicKey(combined, 0);
        byte[] encryptedBodyBytes = new byte[combined.length - PublicKey.KEY_SIZE];
        System.arraycopy(combined, PublicKey.KEY_SIZE, encryptedBodyBytes, 0, encryptedBodyBytes.length);

        ECDHBasicAgreement agreement = new ECDHBasicAgreement();
        agreement.init(asymmetricMasterSecret.getPrivateKey());

        BigInteger secret = KeyUtil.calculateAgreement(agreement, theirPublicKey.getKey());
        MasterCipher masterCipher = getMasterCipherForSecret(secret);
        byte[] decryptedBodyBytes = masterCipher.decryptBytes(encryptedBodyBytes);

        return new String(decryptedBodyBytes);
    } catch (InvalidKeyException ike) {
        throw new org.fdroid.enigtext.crypto.InvalidMessageException(ike);
    } catch (InvalidMessageException e) {
        throw new org.fdroid.enigtext.crypto.InvalidMessageException(e);
    }
}

From source file:org.fdroid.enigtext.crypto.AsymmetricMasterCipher.java

License:Open Source License

public String encryptBody(String body) {
    ECDHBasicAgreement agreement = new ECDHBasicAgreement();
    AsymmetricCipherKeyPair keyPair = KeyUtil.generateKeyPair();

    agreement.init(keyPair.getPrivate());

    BigInteger secret = KeyUtil.calculateAgreement(agreement, asymmetricMasterSecret.getPublicKey().getKey());
    MasterCipher masterCipher = getMasterCipherForSecret(secret);
    byte[] encryptedBodyBytes = masterCipher.encryptBytes(body.getBytes());
    PublicKey publicKey = new PublicKey(31337, (ECPublicKeyParameters) keyPair.getPublic());
    byte[] publicKeyBytes = publicKey.serialize();
    byte[] combined = new byte[publicKeyBytes.length + encryptedBodyBytes.length];

    System.arraycopy(publicKeyBytes, 0, combined, 0, publicKeyBytes.length);
    System.arraycopy(encryptedBodyBytes, 0, combined, publicKeyBytes.length, encryptedBodyBytes.length);

    return Base64.encodeBytes(combined);
}

From source file:org.fdroid.enigtext.crypto.SessionCipher.java

License:Open Source License

private BigInteger calculateSharedSecret(int localKeyId, int remoteKeyId) throws InvalidKeyIdException {
    ECDHBasicAgreement agreement = new ECDHBasicAgreement();
    AsymmetricCipherKeyPair localKeyPair = localRecord.getKeyPairForId(localKeyId).getKeyPair();
    ECPublicKeyParameters remoteKey = remoteRecord.getKeyForId(remoteKeyId).getKey();

    agreement.init(localKeyPair.getPrivate());
    BigInteger secret = KeyUtil.calculateAgreement(agreement, remoteKey);

    return secret;
}

From source file:org.thoughtcrime.securesms.crypto.AsymmetricMasterCipher.java

License:Open Source License

public String decryptBody(String body)
        throws IOException, org.thoughtcrime.securesms.crypto.InvalidMessageException {
    try {/*from   w  w  w .  jav  a2  s .c o m*/
        byte[] combined = Base64.decode(body);
        PublicKey theirPublicKey = new PublicKey(combined, 0);
        byte[] encryptedBodyBytes = new byte[combined.length - PublicKey.KEY_SIZE];
        System.arraycopy(combined, PublicKey.KEY_SIZE, encryptedBodyBytes, 0, encryptedBodyBytes.length);

        ECDHBasicAgreement agreement = new ECDHBasicAgreement();
        agreement.init(asymmetricMasterSecret.getPrivateKey());

        BigInteger secret = KeyUtil.calculateAgreement(agreement, theirPublicKey.getKey());
        MasterCipher masterCipher = getMasterCipherForSecret(secret);
        byte[] decryptedBodyBytes = masterCipher.decryptBytes(encryptedBodyBytes);

        return new String(decryptedBodyBytes);
    } catch (InvalidKeyException ike) {
        throw new org.thoughtcrime.securesms.crypto.InvalidMessageException(ike);
    } catch (InvalidMessageException e) {
        throw new org.thoughtcrime.securesms.crypto.InvalidMessageException(e);
    }
}