Example usage for org.bouncycastle.crypto StreamCipher init

List of usage examples for org.bouncycastle.crypto StreamCipher init

Introduction

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

Prototype

public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException;

Source Link

Document

Initialise the cipher.

Usage

From source file:com.DSC.crypto.Cipher.java

License:Open Source License

/**
 * /*from  w  w w  . ja  v a 2  s .c om*/
 * @param symmetricKey
 * @param IV
 * @return
 */
public static byte[] encryptMsg(byte[] symmetricKey, byte[] IV, byte[] data) {
    byte[] cipherText = new byte[data.length];

    /* Grain stream cipher */
    StreamCipher grain = new Grain128Engine();

    /* Initialize stream cipher */
    ParametersWithIV param = new ParametersWithIV(new KeyParameter(symmetricKey), IV);
    grain.init(true, param);

    /* Encrypt the message */
    grain.processBytes(data, 0, data.length, cipherText, 0);
    return cipherText;
}

From source file:com.DSC.crypto.Cipher.java

License:Open Source License

/**
 * /*w  w  w .j  a  v a2 s .  com*/
 * @param symmetricKey
 * @param IV
 * @return
 */
public static byte[] decryptMsg(byte[] symmetricKey, byte[] IV, byte[] data) {
    byte[] cipherText = new byte[data.length];

    /* Grain stream cipher */
    StreamCipher grain = new Grain128Engine();

    /* Initialize stream cipher */
    ParametersWithIV param = new ParametersWithIV(new KeyParameter(symmetricKey), IV);
    grain.init(false, param);

    /* Decrypt the message */
    grain.processBytes(data, 0, data.length, cipherText, 0);
    return cipherText;
}

From source file:com.github.capone.protocol.crypto.SymmetricKey.java

License:Open Source License

public byte[] encrypt(Nonce nonce, byte[] message) throws EncryptionException {
    try {/*from w  w w  . j a va2 s.c  om*/
        byte[] subkey = new byte[SUBKEYBYTES];
        byte[] encrypted = new byte[message.length + MACBYTES];

        StreamCipher cipher = new XSalsa20Engine();
        cipher.init(true, new ParametersWithIV(key, nonce.nonce));
        cipher.processBytes(subkey, 0, subkey.length, subkey, 0);
        cipher.processBytes(message, 0, message.length, encrypted, MACBYTES);

        Mac auth = new Poly1305();
        auth.init(new KeyParameter(subkey));
        auth.update(encrypted, MACBYTES, message.length);
        auth.doFinal(encrypted, 0);

        return encrypted;
    } catch (Exception e) {
        throw new EncryptionException();
    }
}

From source file:com.github.capone.protocol.crypto.SymmetricKey.java

License:Open Source License

public byte[] decrypt(Nonce nonce, byte[] encrypted) throws DecryptionException {
    byte[] subkey = new byte[SUBKEYBYTES];
    byte[] mac = new byte[MACBYTES];
    byte[] plain = new byte[encrypted.length - MACBYTES];

    if (encrypted.length < MACBYTES) {
        throw new DecryptionException();
    }//ww w  . j  a v a 2  s . c o  m

    StreamCipher cipher = new XSalsa20Engine();
    cipher.init(true, new ParametersWithIV(key, nonce.nonce));
    cipher.processBytes(subkey, 0, subkey.length, subkey, 0);

    Mac auth = new Poly1305();
    auth.init(new KeyParameter(subkey));
    auth.update(encrypted, MACBYTES, encrypted.length - MACBYTES);
    auth.doFinal(mac, 0);

    boolean validMac = true;
    for (int i = 0; i < MACBYTES; i++) {
        validMac &= mac[i] == encrypted[i];
    }
    if (!validMac) {
        throw new DecryptionException();
    }

    cipher.processBytes(encrypted, MACBYTES, encrypted.length - MACBYTES, plain, 0);

    return plain;
}

From source file:com.keepassdroid.crypto.PwStreamCipherFactory.java

License:Open Source License

private static StreamCipher getSalsa20(byte[] key) {
    // Build stream cipher key
    MessageDigest md;//from   w w w.j  a v  a  2s. c o  m
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        throw new RuntimeException("SHA 256 not supported");
    }
    byte[] key32 = md.digest(key);

    KeyParameter keyParam = new KeyParameter(key32);
    ParametersWithIV ivParam = new ParametersWithIV(keyParam, SALSA_IV);

    StreamCipher cipher = new Salsa20Engine();
    cipher.init(true, ivParam);

    return cipher;
}

From source file:rpc.security.ntlm.NTLMKeyFactory.java

License:Open Source License

StreamCipher getARCFOUR(byte[] key) {
    HashMap attrib = new HashMap();
    //      IRandom keystream = new ARCFour();
    //      attrib.put(ARCFour.ARCFOUR_KEY_MATERIAL, key);
    //      keystream.init(attrib);
    StreamCipher keystream = new RC4Engine();
    CipherParameters params = new KeyParameter(key);
    keystream.init(true, params);
    return keystream;
}

From source file:shadowsocks.crypto.Chacha20Crypto.java

License:Apache License

@Override
protected StreamCipher createCipher(byte[] iv, boolean encrypt) throws CryptoException {
    StreamCipher c;
    if (mName.equals(CIPHER_CHACHA20_IETF)) {
        c = new ChaCha7539Engine();
    } else {/* w  w  w . j a v  a2 s  .  c om*/
        c = new ChaChaEngine();
    }
    ParametersWithIV parameterIV = new ParametersWithIV(new KeyParameter(mKey), iv, 0, mIVLength);
    c.init(encrypt, parameterIV);
    return c;
}