Example usage for org.bouncycastle.crypto BlockCipher processBlock

List of usage examples for org.bouncycastle.crypto BlockCipher processBlock

Introduction

In this page you can find the example usage for org.bouncycastle.crypto BlockCipher processBlock.

Prototype

public int processBlock(byte[] in, int inOff, byte[] out, int outOff)
        throws DataLengthException, IllegalStateException;

Source Link

Document

Process one block of input from the array in and write it to the out array.

Usage

From source file:argonms.common.net.external.ClientEncryption.java

License:Open Source License

public static void aesOfbCrypt(byte[] data, byte[] iv) {
    //Maple's OFB is done in piecemeal every 1460 bytes (I'm guessing it has
    //to deal with maximum segment size). First piece is only 1456 bytes
    //because the header, although not encrypted, adds 4 blocks to the first
    //segment.//  w w  w .ja  va2 s  .  c o m
    BlockCipher ciph = aes256.get();
    //loops through each 1460 byte piece (with first piece only 1456 bytes)
    for (int remaining = data.length, offset = 0, pieceSize = Math.min(1456,
            remaining); remaining > 0; remaining -= pieceSize, offset += pieceSize, pieceSize = Math.min(1460,
                    remaining)) {
        byte[] myIv = ByteTool.multiplyBytes(iv, 4, 4);

        //OFB (which is just input block XOR'd with encrypted IV and key) each full block (block size is equal to IV size)
        int fullBlocks = pieceSize / myIv.length;
        for (int i = 0; i < fullBlocks; i++) {
            ciph.processBlock(myIv, 0, myIv, 0); //encrypt IV with key and copy output back to IV
            for (int j = 0; j < myIv.length; j++)
                data[offset + i * myIv.length + j] ^= myIv[j]; //XOR input block with encrypted IV and key
        }
        //OFB the final, incomplete block - OFB doesn't need block size aligned input
        ciph.processBlock(myIv, 0, myIv, 0);
        for (int i = pieceSize % myIv.length - 1; i >= 0; i--)
            data[offset + fullBlocks * myIv.length + i] ^= myIv[i];
    }
}

From source file:com.hanhuy.keepassj.CompositeKey.java

License:Open Source License

public static long TransformKeyBenchmark(long uMilliseconds, long uStep) {
    long uRounds;

    // Try native method
    //         if(NativeLib.TransformKeyBenchmark256(uMilliseconds, out uRounds))
    //            return uRounds;

    byte[] pbKey = new byte[32];
    byte[] pbNewKey = new byte[32];
    for (int i = 0; i < pbKey.length; ++i) {
        pbKey[i] = (byte) i;
        pbNewKey[i] = (byte) i;
    }/*from   w  w  w .  ja v  a  2 s.  c  o m*/

    byte[] pbIV = new byte[16];
    Arrays.fill(pbIV, (byte) 0);
    try {
        BlockCipher engine = AesEngines.createAesEngine();
        engine.init(true, new KeyParameter(pbKey));

        if (engine.getBlockSize() != (128 / 8)) // AES block size
        {
            throw new RuntimeException();
        }

        uRounds = 0;
        long tStart = System.currentTimeMillis();
        while (true) {
            for (long j = 0; j < uStep; ++j) {
                engine.processBlock(pbNewKey, 0, pbNewKey, 0);
                engine.processBlock(pbNewKey, 16, pbNewKey, 16);
            }

            uRounds += uStep;
            if (uRounds < uStep) // Overflow check
            {
                uRounds = Long.MAX_VALUE;
                break;
            }

            long tElapsed = System.currentTimeMillis() - tStart;
            if (tElapsed > uMilliseconds)
                break;
        }

        return uRounds;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.tsenger.animamea.crypto.AmAESCrypto.java

License:Open Source License

/**
 * Dekodiert einen Block mit AES/*  w  w  w.  j  ava  2s  . c  o m*/
 * 
 * @param key
 *            Byte-Array enthlt den AES-Schlssel
 * @param z
 *            verschlsselter Block
 * @return entschlsselter block
 */
@Override
public byte[] decryptBlock(byte[] key, byte[] z) {
    byte[] s = new byte[blockSize];
    KeyParameter encKey = new KeyParameter(key);
    BlockCipher cipher = new AESFastEngine();
    cipher.init(false, encKey);
    cipher.processBlock(z, 0, s, 0);
    return s;
}

From source file:de.tsenger.animamea.crypto.AmAESCrypto.java

License:Open Source License

/**
 * Kodiert einen Block mit AES/*from  w  w w . j a v  a2s . com*/
 * 
 * @param key
 *            Byte-Array enthlt den AES-Schlssel
 * @param z
 *            verschlsselter Block
 * @return entschlsselter block
 */

public byte[] encryptBlock(byte[] key, byte[] z) {
    byte[] s = new byte[blockSize];
    KeyParameter encKey = new KeyParameter(key);
    BlockCipher cipher = new AESFastEngine();
    cipher.init(true, encKey);
    cipher.processBlock(z, 0, s, 0);
    return s;
}

From source file:de.tsenger.animamea.crypto.AmDESCrypto.java

License:Open Source License

/**
 * Dekodiert einen Block mit DES//ww w  .  j a v  a2s.  c o m
 * 
 * @param key
 *            Byte-Array enthlt den 3DES-Schlssel
 * @param z
 *            verschlsselter Block
 * @return entschlsselter block
 */
@Override
public byte[] decryptBlock(byte[] key, byte[] z) {
    byte[] s = new byte[16];
    KeyParameter encKey = new KeyParameter(key);
    BlockCipher cipher = new DESedeEngine();
    cipher.init(false, encKey);
    cipher.processBlock(z, 0, s, 0);
    return s;
}

From source file:gnu.java.zrtp.jmf.transform.srtp.SRTPCipherCTR.java

License:LGPL

/**
 * Computes the cipher stream for AES CM mode. See section 4.1.1 in RFC3711
 * for detailed description.//from  ww  w  .j av  a  2s .  c  om
 * 
 * @param out
 *            byte array holding the output cipher stream
 * @param length
 *            length of the cipher stream to produce, in bytes
 * @param iv
 *            initialization vector used to generate this cipher stream
 */
public void getCipherStream(BlockCipher aesCipher, byte[] out, int length, byte[] iv) {
    System.arraycopy(iv, 0, cipherInBlock, 0, 14);

    int ctr;
    for (ctr = 0; ctr < length / BLKLEN; ctr++) {
        // compute the cipher stream
        cipherInBlock[14] = (byte) ((ctr & 0xFF00) >> 8);
        cipherInBlock[15] = (byte) ((ctr & 0x00FF));

        aesCipher.processBlock(cipherInBlock, 0, out, ctr * BLKLEN);
    }

    // Treat the last bytes:
    cipherInBlock[14] = (byte) ((ctr & 0xFF00) >> 8);
    cipherInBlock[15] = (byte) ((ctr & 0x00FF));

    aesCipher.processBlock(cipherInBlock, 0, tmpCipherBlock, 0);
    System.arraycopy(tmpCipherBlock, 0, out, ctr * BLKLEN, length % BLKLEN);
}

From source file:gnu.java.zrtp.jmf.transform.srtp.SRTPCipherF8.java

License:LGPL

public static void process(BlockCipher cipher, byte[] data, int off, int len, byte[] iv, BlockCipher f8Cipher) {
    F8Context f8ctx = new SRTPCipherF8().new F8Context();

    /*//from w ww . ja va2 s  .c o m
     * Get memory for the derived IV (IV')
     */
    f8ctx.ivAccent = new byte[BLKLEN];

    /*
     * Use the derived IV encryption setup to encrypt the original IV to produce IV'.
     */
    f8Cipher.processBlock(iv, 0, f8ctx.ivAccent, 0);

    f8ctx.J = 0; // initialize the counter
    f8ctx.S = new byte[BLKLEN]; // get the key stream buffer

    Arrays.fill(f8ctx.S, (byte) 0);

    int inLen = len;

    while (inLen >= BLKLEN) {
        processBlock(cipher, f8ctx, data, off, data, off, BLKLEN);
        inLen -= BLKLEN;
        off += BLKLEN;
    }

    if (inLen > 0) {
        processBlock(cipher, f8ctx, data, off, data, off, inLen);
    }
}

From source file:gnu.java.zrtp.jmf.transform.srtp.SRTPCipherF8.java

License:LGPL

/**
 * Encrypt / Decrypt a block using F8 Mode AES algorithm, read len bytes
 * data from in at inOff and write the output into out at outOff
 * /*w ww. ja  v  a  2s. co  m*/
 * @param f8ctx
 *            F8 encryption context
 * @param in
 *            byte array holding the data to be processed
 * @param inOff
 *            start offset of the data to be processed inside in array
 * @param out
 *            byte array that will hold the processed data
 * @param outOff
 *            start offset of output data in out
 * @param len
 *            length of the input data
 */
private static void processBlock(BlockCipher cipher, F8Context f8ctx, byte[] in, int inOff, byte[] out,
        int outOff, int len) {
    /*
     * XOR the previous key stream with IV'
     * ( S(-1) xor IV' )
     */
    for (int i = 0; i < BLKLEN; i++)
        f8ctx.S[i] ^= f8ctx.ivAccent[i];

    /*
     * Now XOR (S(n-1) xor IV') with the current counter, then increment 
     * the counter
     */
    f8ctx.S[12] ^= f8ctx.J >> 24;
    f8ctx.S[13] ^= f8ctx.J >> 16;
    f8ctx.S[14] ^= f8ctx.J >> 8;
    f8ctx.S[15] ^= f8ctx.J >> 0;
    f8ctx.J++;

    /*
     * Now compute the new key stream using AES encrypt
     */
    cipher.processBlock(f8ctx.S, 0, f8ctx.S, 0);

    /*
     * As the last step XOR the plain text with the key stream to produce
     * the cipher text.
     */
    for (int i = 0; i < len; i++)
        out[outOff + i] = (byte) (in[inOff + i] ^ f8ctx.S[i]);
}

From source file:jazmin.server.relay.udp.webrtc.SRTPCipherF8.java

License:LGPL

public static void process(BlockCipher cipher, ByteBuffer data, int off, int len, byte[] iv,
        BlockCipher f8Cipher) {
    F8Context f8ctx = new SRTPCipherF8().new F8Context();

    /*//from w ww  .j  a  va  2 s .  co  m
     * Get memory for the derived IV (IV')
     */
    f8ctx.ivAccent = new byte[BLKLEN];

    /*
     * Use the derived IV encryption setup to encrypt the original IV to produce IV'.
     */
    f8Cipher.processBlock(iv, 0, f8ctx.ivAccent, 0);

    f8ctx.J = 0; // initialize the counter
    f8ctx.S = new byte[BLKLEN]; // get the key stream buffer

    Arrays.fill(f8ctx.S, (byte) 0);

    int inLen = len;

    while (inLen >= BLKLEN) {
        processBlock(cipher, f8ctx, data, off, data, off, BLKLEN);
        inLen -= BLKLEN;
        off += BLKLEN;
    }

    if (inLen > 0) {
        processBlock(cipher, f8ctx, data, off, data, off, inLen);
    }
}

From source file:jazmin.server.relay.udp.webrtc.SRTPCipherF8.java

License:LGPL

/**
 * Encrypt / Decrypt a block using F8 Mode AES algorithm, read len bytes
 * data from in at inOff and write the output into out at outOff
 * //from w ww .  j  av  a  2s  . c o  m
 * @param f8ctx
 *            F8 encryption context
 * @param in
 *            byte array holding the data to be processed
 * @param inOff
 *            start offset of the data to be processed inside in array
 * @param out
 *            byte array that will hold the processed data
 * @param outOff
 *            start offset of output data in out
 * @param len
 *            length of the input data
 */
private static void processBlock(BlockCipher cipher, F8Context f8ctx, ByteBuffer in, int inOff, ByteBuffer out,
        int outOff, int len) {
    /*
     * XOR the previous key stream with IV'
     * ( S(-1) xor IV' )
     */
    for (int i = 0; i < BLKLEN; i++)
        f8ctx.S[i] ^= f8ctx.ivAccent[i];

    /*
     * Now XOR (S(n-1) xor IV') with the current counter, then increment 
     * the counter
     */
    f8ctx.S[12] ^= f8ctx.J >> 24;
    f8ctx.S[13] ^= f8ctx.J >> 16;
    f8ctx.S[14] ^= f8ctx.J >> 8;
    f8ctx.S[15] ^= f8ctx.J >> 0;
    f8ctx.J++;

    /*
     * Now compute the new key stream using AES encrypt
     */
    cipher.processBlock(f8ctx.S, 0, f8ctx.S, 0);

    /*
     * As the last step XOR the plain text with the key stream to produce
     * the cipher text.
     */
    for (int i = 0; i < len; i++)
        out.put(outOff + i, (byte) (in.get(inOff + i) ^ f8ctx.S[i]));
}