Example usage for org.bouncycastle.crypto StreamCipher processBytes

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

Introduction

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

Prototype

public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff) throws DataLengthException;

Source Link

Document

process a block of bytes from in putting the result into out.

Usage

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

License:Open Source License

/**
 * /*www.  ja  v a 2s.  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  a  2 s  .  co  m
 * @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   ww  w . j  a v  a  2  s  .c  o m*/
        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();
    }//from ww  w .j  a  v  a 2s  . 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:rpc.security.ntlm.NTLMKeyFactory.java

License:Open Source License

byte[] applyARCFOUR(StreamCipher keystream, byte[] data) {
    byte[] retData = new byte[data.length];

    keystream.processBytes(data, 0, data.length, retData, 0);

    //      for (int i = 0; i < data.length; i++) {
    //         retData[i] = (byte) (data[i] ^ keystream.nextByte());
    //      }//  w  ww. jav  a2  s.  co  m

    return retData;
}

From source file:shadowsocks.crypto.Chacha20Crypto.java

License:Apache License

@Override
protected void process(byte[] in, ByteArrayOutputStream out, boolean encrypt) {
    int size;/*w  ww  .ja va  2 s  .co  m*/
    byte[] buffer = new byte[in.length];
    StreamCipher cipher;
    if (encrypt) {
        cipher = mEncryptCipher;
    } else {
        cipher = mDecryptCipher;
    }
    size = cipher.processBytes(in, 0, in.length, buffer, 0);
    out.write(buffer, 0, size);
}