Search in sources :

Example 1 with KeyParameter

use of org.gudy.bouncycastle.crypto.params.KeyParameter in project BiglyBT by BiglySoftware.

the class IESEngine method decryptBlock.

private byte[] decryptBlock(byte[] in_enc, int inOff, int inLen, byte[] z) throws InvalidCipherTextException {
    byte[] M = null;
    KeyParameter macKey = null;
    KDFParameters kParam = new KDFParameters(z, param.getDerivationV());
    int macKeySize = param.getMacKeySize();
    kdf.init(kParam);
    inLen -= mac.getMacSize();
    if (// stream mode
    cipher == null) {
        byte[] buf = new byte[inLen + (macKeySize / 8)];
        M = new byte[inLen];
        kdf.generateBytes(buf, 0, buf.length);
        for (int i = 0; i != inLen; i++) {
            M[i] = (byte) (in_enc[inOff + i] ^ buf[i]);
        }
        macKey = new KeyParameter(buf, inLen, (macKeySize / 8));
    } else {
        int cipherKeySize = ((IESWithCipherParameters) param).getCipherKeySize();
        byte[] buf = new byte[(cipherKeySize / 8) + (macKeySize / 8)];
        cipher.init(false, new KeyParameter(buf, 0, (cipherKeySize / 8)));
        byte[] tmp = new byte[cipher.getOutputSize(inLen)];
        int off = cipher.processBytes(in_enc, inOff, inLen, tmp, 0);
        off += cipher.doFinal(tmp, off);
        M = new byte[off];
        System.arraycopy(tmp, 0, M, 0, off);
        macKey = new KeyParameter(buf, (cipherKeySize / 8), (macKeySize / 8));
    }
    byte[] macIV = param.getEncodingV();
    mac.init(macKey);
    mac.update(in_enc, inOff, inLen);
    mac.update(macIV, 0, macIV.length);
    mac.doFinal(macBuf, 0);
    inOff += inLen;
    for (int t = 0; t < macBuf.length; t++) {
        if (macBuf[t] != in_enc[inOff + t]) {
            throw (new InvalidCipherTextException("Mac codes failed to equal."));
        }
    }
    return M;
}
Also used : KDFParameters(org.gudy.bouncycastle.crypto.params.KDFParameters) KeyParameter(org.gudy.bouncycastle.crypto.params.KeyParameter) IESWithCipherParameters(org.gudy.bouncycastle.crypto.params.IESWithCipherParameters)

Example 2 with KeyParameter

use of org.gudy.bouncycastle.crypto.params.KeyParameter in project BiglyBT by BiglySoftware.

the class IESEngine method encryptBlock.

private byte[] encryptBlock(byte[] in, int inOff, int inLen, byte[] z) throws InvalidCipherTextException {
    byte[] C = null;
    KeyParameter macKey = null;
    KDFParameters kParam = new KDFParameters(z, param.getDerivationV());
    int c_text_length = 0;
    int macKeySize = param.getMacKeySize();
    kdf.init(kParam);
    if (// stream mode
    cipher == null) {
        byte[] buf = new byte[inLen + (macKeySize / 8)];
        C = new byte[inLen + mac.getMacSize()];
        c_text_length = inLen;
        kdf.generateBytes(buf, 0, buf.length);
        for (int i = 0; i != inLen; i++) {
            C[i] = (byte) (in[inOff + i] ^ buf[i]);
        }
        macKey = new KeyParameter(buf, inLen, (macKeySize / 8));
    } else {
        int cipherKeySize = ((IESWithCipherParameters) param).getCipherKeySize();
        byte[] buf = new byte[(cipherKeySize / 8) + (macKeySize / 8)];
        cipher.init(true, new KeyParameter(buf, 0, (cipherKeySize / 8)));
        c_text_length = cipher.getOutputSize(inLen);
        C = new byte[c_text_length + mac.getMacSize()];
        int off = cipher.processBytes(in, inOff, inLen, C, 0);
        cipher.doFinal(C, off);
        macKey = new KeyParameter(buf, (cipherKeySize / 8), (macKeySize / 8));
    }
    byte[] macIV = param.getEncodingV();
    mac.init(macKey);
    mac.update(C, 0, c_text_length);
    mac.update(macIV, 0, macIV.length);
    // 
    // return the message and it's MAC
    // 
    mac.doFinal(C, c_text_length);
    return C;
}
Also used : KDFParameters(org.gudy.bouncycastle.crypto.params.KDFParameters) KeyParameter(org.gudy.bouncycastle.crypto.params.KeyParameter) IESWithCipherParameters(org.gudy.bouncycastle.crypto.params.IESWithCipherParameters)

Example 3 with KeyParameter

use of org.gudy.bouncycastle.crypto.params.KeyParameter in project BiglyBT by BiglySoftware.

the class CryptoManagerImpl method obfuscate.

@Override
public byte[] obfuscate(byte[] data) {
    RC4Engine engine = new RC4Engine();
    CipherParameters params = new KeyParameter(new SHA1Simple().calculateHash(getOBSID()));
    engine.init(true, params);
    byte[] temp = new byte[1024];
    engine.processBytes(temp, 0, 1024, temp, 0);
    final byte[] obs_value = new byte[data.length];
    engine.processBytes(data, 0, data.length, obs_value, 0);
    return (obs_value);
}
Also used : CipherParameters(org.gudy.bouncycastle.crypto.CipherParameters) KeyParameter(org.gudy.bouncycastle.crypto.params.KeyParameter) RC4Engine(org.gudy.bouncycastle.crypto.engines.RC4Engine)

Example 4 with KeyParameter

use of org.gudy.bouncycastle.crypto.params.KeyParameter in project BiglyBT by BiglySoftware.

the class UDPConnectionSet method getCipher.

private RC4Engine getCipher(byte[] key) {
    SecretKeySpec secret_key_spec = new SecretKeySpec(key, "RC4");
    RC4Engine rc4_engine = new RC4Engine();
    CipherParameters params_a = new KeyParameter(secret_key_spec.getEncoded());
    // for RC4 enc/dec is irrelevant
    rc4_engine.init(true, params_a);
    // skip first 1024 bytes of stream to protected against a Fluhrer, Mantin and Shamir attack
    byte[] temp = new byte[1024];
    rc4_engine.processBytes(temp, 0, temp.length, temp, 0);
    return (rc4_engine);
}
Also used : CipherParameters(org.gudy.bouncycastle.crypto.CipherParameters) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeyParameter(org.gudy.bouncycastle.crypto.params.KeyParameter) RC4Engine(org.gudy.bouncycastle.crypto.engines.RC4Engine)

Example 5 with KeyParameter

use of org.gudy.bouncycastle.crypto.params.KeyParameter in project BiglyBT by BiglySoftware.

the class DHTControlImpl method getObfuscatedValue.

protected byte[] getObfuscatedValue(byte[] plain_key) {
    RC4Engine engine = new RC4Engine();
    CipherParameters params = new KeyParameter(new SHA1Simple().calculateHash(plain_key));
    engine.init(true, params);
    byte[] temp = new byte[1024];
    engine.processBytes(temp, 0, 1024, temp, 0);
    final byte[] obs_value = new byte[plain_key.length];
    engine.processBytes(plain_key, 0, plain_key.length, obs_value, 0);
    return (obs_value);
}
Also used : CipherParameters(org.gudy.bouncycastle.crypto.CipherParameters) KeyParameter(org.gudy.bouncycastle.crypto.params.KeyParameter) RC4Engine(org.gudy.bouncycastle.crypto.engines.RC4Engine)

Aggregations

KeyParameter (org.gudy.bouncycastle.crypto.params.KeyParameter)9 CipherParameters (org.gudy.bouncycastle.crypto.CipherParameters)3 RC4Engine (org.gudy.bouncycastle.crypto.engines.RC4Engine)3 ParametersWithIV (org.gudy.bouncycastle.crypto.params.ParametersWithIV)3 IESWithCipherParameters (org.gudy.bouncycastle.crypto.params.IESWithCipherParameters)2 KDFParameters (org.gudy.bouncycastle.crypto.params.KDFParameters)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 OpenSSLPBEParametersGenerator (org.gudy.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator)1