Search in sources :

Example 1 with IESParameterSpec

use of org.gudy.bouncycastle.jce.spec.IESParameterSpec in project BiglyBT by BiglySoftware.

the class CryptoHandlerECC method decrypt.

@Override
public byte[] decrypt(byte[] other_public_key, byte[] data, String reason) throws CryptoManagerException {
    try {
        IEKeySpec key_spec = new IEKeySpec(getMyPrivateKey(reason), CryptoECCUtils.rawdataToPubkey(other_public_key));
        byte[] d = new byte[16];
        byte[] e = new byte[16];
        System.arraycopy(data, 0, d, 0, 16);
        System.arraycopy(data, 16, e, 0, 16);
        IESParameterSpec param = new IESParameterSpec(d, e, 128);
        InternalECIES cipher = new InternalECIES();
        cipher.internalEngineInit(Cipher.DECRYPT_MODE, key_spec, param, null);
        return (cipher.internalEngineDoFinal(data, 32, data.length - 32));
    } catch (CryptoManagerException e) {
        throw (e);
    } catch (Throwable e) {
        throw (new CryptoManagerException("Decrypt failed", e));
    }
}
Also used : IESParameterSpec(org.gudy.bouncycastle.jce.spec.IESParameterSpec) IEKeySpec(org.gudy.bouncycastle.jce.spec.IEKeySpec)

Example 2 with IESParameterSpec

use of org.gudy.bouncycastle.jce.spec.IESParameterSpec in project BiglyBT by BiglySoftware.

the class JCEIESCipher method engineInit.

public void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (!(key instanceof IESKey)) {
        throw new InvalidKeyException("must be passed IE key");
    }
    if (params == null && (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE)) {
        // 
        // if nothing is specified we set up for a 128 bit mac, with
        // 128 bit derivation vectors.
        // 
        byte[] d = new byte[16];
        byte[] e = new byte[16];
        if (random == null) {
            random = new SecureRandom();
        }
        random.nextBytes(d);
        random.nextBytes(e);
        params = new IESParameterSpec(d, e, 128);
    } else if (!(params instanceof IESParameterSpec)) {
        throw new InvalidAlgorithmParameterException("must be passed IES parameters");
    }
    IESKey ieKey = (IESKey) key;
    CipherParameters pubKey;
    CipherParameters privKey;
    if (ieKey.getPublic() instanceof JCEECPublicKey) {
        pubKey = ECUtil.generatePublicKeyParameter(ieKey.getPublic());
        privKey = ECUtil.generatePrivateKeyParameter(ieKey.getPrivate());
    } else {
        pubKey = DHUtil.generatePublicKeyParameter(ieKey.getPublic());
        privKey = DHUtil.generatePrivateKeyParameter(ieKey.getPrivate());
    }
    this.engineParams = (IESParameterSpec) params;
    IESParameters p = new IESParameters(engineParams.getDerivationV(), engineParams.getEncodingV(), engineParams.getMacKeySize());
    this.state = opmode;
    buffer.reset();
    switch(opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            cipher.init(true, privKey, pubKey, p);
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            cipher.init(false, privKey, pubKey, p);
            break;
        default:
            System.out.println("eeek!");
    }
}
Also used : CipherParameters(org.gudy.bouncycastle.crypto.CipherParameters) IESParameterSpec(org.gudy.bouncycastle.jce.spec.IESParameterSpec) IESKey(org.gudy.bouncycastle.jce.interfaces.IESKey) IESParameters(org.gudy.bouncycastle.crypto.params.IESParameters)

Example 3 with IESParameterSpec

use of org.gudy.bouncycastle.jce.spec.IESParameterSpec in project BiglyBT by BiglySoftware.

the class CryptoHandlerECC method encrypt.

@Override
public byte[] encrypt(byte[] other_public_key, byte[] data, String reason) throws CryptoManagerException {
    try {
        IEKeySpec key_spec = new IEKeySpec(getMyPrivateKey(reason), CryptoECCUtils.rawdataToPubkey(other_public_key));
        byte[] d = new byte[16];
        byte[] e = new byte[16];
        RandomUtils.nextSecureBytes(d);
        RandomUtils.nextSecureBytes(e);
        IESParameterSpec param = new IESParameterSpec(d, e, 128);
        InternalECIES cipher = new InternalECIES();
        cipher.internalEngineInit(Cipher.ENCRYPT_MODE, key_spec, param, null);
        byte[] encrypted = cipher.internalEngineDoFinal(data, 0, data.length);
        byte[] result = new byte[32 + encrypted.length];
        System.arraycopy(d, 0, result, 0, 16);
        System.arraycopy(e, 0, result, 16, 16);
        System.arraycopy(encrypted, 0, result, 32, encrypted.length);
        return (result);
    } catch (CryptoManagerException e) {
        throw (e);
    } catch (Throwable e) {
        throw (new CryptoManagerException("Encrypt failed", e));
    }
}
Also used : IESParameterSpec(org.gudy.bouncycastle.jce.spec.IESParameterSpec) IEKeySpec(org.gudy.bouncycastle.jce.spec.IEKeySpec)

Aggregations

IESParameterSpec (org.gudy.bouncycastle.jce.spec.IESParameterSpec)3 IEKeySpec (org.gudy.bouncycastle.jce.spec.IEKeySpec)2 CipherParameters (org.gudy.bouncycastle.crypto.CipherParameters)1 IESParameters (org.gudy.bouncycastle.crypto.params.IESParameters)1 IESKey (org.gudy.bouncycastle.jce.interfaces.IESKey)1