Example usage for org.bouncycastle.crypto BlockCipher init

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

Introduction

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

Prototype

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

Source Link

Document

Initialise the cipher.

Usage

From source file:com.github.horrorho.inflatabledonkey.file.FileStreamWriter.java

License:Open Source License

static InputStream decryptStream(InputStream in, XFileKey keyCipher) {
    BlockCipher cipher = keyCipher.ciphers().get();
    cipher.init(false, new KeyParameter(keyCipher.key()));
    return new CipherInputStream(in, new BufferedBlockCipher(cipher));
}

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;
    }// 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:com.skplanet.jose.jwa.crypto.CryptoUtils.java

License:Open Source License

public static byte[] aesGcmEncrypt(Transformation transformation, byte[] raw, byte[] secret, int atLength,
        byte[] iv, byte[] aad) throws Exception {
    BlockCipher blockCipher = new AESEngine();
    blockCipher.init(true, new KeyParameter(new SecretKeySpec(secret, "AES").getEncoded()));

    GCMBlockCipher aGCMBlockCipher = new GCMBlockCipher(blockCipher);
    aGCMBlockCipher.init(true, new AEADParameters(new KeyParameter(secret), atLength, iv, aad));

    int len = aGCMBlockCipher.getOutputSize(raw.length);
    byte[] out = new byte[len];
    int outOff = aGCMBlockCipher.processBytes(raw, 0, raw.length, out, 0);
    aGCMBlockCipher.doFinal(out, outOff);

    return out;// ww  w . jav  a 2  s .c  o  m
}

From source file:com.skplanet.jose.jwa.crypto.CryptoUtils.java

License:Open Source License

public static byte[] aesGcmDecrypt(Transformation transformation, byte[] encryptedData, byte[] secret,
        int atLength, byte[] iv, byte[] aad) throws Exception {
    BlockCipher blockCipher = new AESEngine();
    blockCipher.init(false, new KeyParameter(new SecretKeySpec(secret, "AES").getEncoded()));

    GCMBlockCipher aGCMBlockCipher = new GCMBlockCipher(blockCipher);
    aGCMBlockCipher.init(false, new AEADParameters(new KeyParameter(secret), atLength, iv, aad));

    int len = aGCMBlockCipher.getOutputSize(encryptedData.length);
    byte[] out = new byte[len];
    int outOff = aGCMBlockCipher.processBytes(encryptedData, 0, encryptedData.length, out, 0);
    aGCMBlockCipher.doFinal(out, outOff);

    return out;//from   www  . j a  v a  2s. c  om
}

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

License:Open Source License

/**
 * Dekodiert einen Block mit AES/*from   w w w  . j  a  va 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  .  jav  a2  s.  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/*from  ww  w  .  j  av  a 2s  .c  om*/
 * 
 * @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.SRTPCipherF8.java

License:LGPL

public static void deriveForIV(BlockCipher f8Cipher, byte[] key, byte[] salt) {
    /*/*from w  ww.ja v a  2 s .c o m*/
     * Get memory for the special key. This is the key to compute the
     * derived IV (IV').
     */
    byte[] saltMask = new byte[key.length];
    byte[] maskedKey = new byte[key.length];

    /*
     * First copy the salt into the mask field, then fill with 0x55 to get a
     * full key.
     */
    System.arraycopy(salt, 0, saltMask, 0, salt.length);
    for (int i = salt.length; i < saltMask.length; ++i)
        saltMask[i] = 0x55;

    /*
     * XOR the original key with the above created mask to get the special
     * key.
     */
    for (int i = 0; i < key.length; i++)
        maskedKey[i] = (byte) (key[i] ^ saltMask[i]);

    /*
     * Prepare the f8Cipher with the special key to compute IV'
     */
    KeyParameter encryptionKey = new KeyParameter(maskedKey);
    f8Cipher.init(true, encryptionKey);
    saltMask = null;
    maskedKey = null;
}

From source file:jcrypter.JCrypterFrame.java

License:Apache License

/**
 * Test if the unlimited strength policy files are installed
 *//*w  w  w .j a v  a2s  .  c o m*/
private void testUnlimitedPolicy() {
    try {
        byte[] data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };

        // create a 64 bit secret key from raw bytes

        byte[] key = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; //NOI18N

        byte[] out = new byte[256];

        // create a cipher and attempt to encrypt the data block with our key

        BlockCipher c = new BlowfishEngine();

        c.init(true, new KeyParameter(key));
        c.processBlock(data, 0, out, 0);
        c.reset();

        // create a 192 bit secret key from raw bytes

        SecretKey key192 = new SecretKeySpec(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 },
                "Blowfish"); //NOI18N

        // now try encrypting with the larger key

        c.init(true, new KeyParameter(out));
        c.processBlock(data, 0, out, 0);
        //If no exception is thrown before
        System.out.println(i18n.getString("Unrestricted_policy_test:_passed"));
    }
    /*catch (InvalidKeyException ex)
    {
    JOptionPane.showMessageDialog(this, i18n.getString("The_Unrestricted_Policy_Files_are_not_installed_in_your_JRE.") +
            i18n.getString("Please_install_them_to_enable_strong_cryptography!"), i18n.getString("Restricted_policy_files"),
            JOptionPane.ERROR_MESSAGE);
    }*/
    catch (Exception ex) {
        Logger.getLogger(JCrypterFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.cryptomator.crypto.aes256.AesSivCipherUtil.java

License:Open Source License

static byte[] sivEncrypt(byte[] aesKey, byte[] macKey, byte[] plaintext, byte[]... additionalData)
        throws InvalidKeyException {
    if (aesKey.length != 16 && aesKey.length != 24 && aesKey.length != 32) {
        throw new InvalidKeyException("Invalid aesKey length " + aesKey.length);
    }//from   w  w  w  .j a v  a 2 s . c o  m

    final byte[] iv = s2v(macKey, plaintext, additionalData);

    final int numBlocks = (plaintext.length + 15) / 16;

    // clear out the 31st and 63rd (rightmost) bit:
    final byte[] ctr = Arrays.copyOf(iv, 16);
    ctr[8] = (byte) (ctr[8] & 0x7F);
    ctr[12] = (byte) (ctr[12] & 0x7F);
    final ByteBuffer ctrBuf = ByteBuffer.wrap(ctr);
    final long initialCtrVal = ctrBuf.getLong(8);

    final byte[] x = new byte[numBlocks * 16];
    final BlockCipher aes = new AESFastEngine();
    aes.init(true, new KeyParameter(aesKey));
    for (int i = 0; i < numBlocks; i++) {
        final long ctrVal = initialCtrVal + i;
        ctrBuf.putLong(8, ctrVal);
        aes.processBlock(ctrBuf.array(), 0, x, i * 16);
        aes.reset();
    }

    final byte[] ciphertext = xor(plaintext, x);

    return ArrayUtils.addAll(iv, ciphertext);
}