Example usage for org.bouncycastle.crypto.engines AESEngine AESEngine

List of usage examples for org.bouncycastle.crypto.engines AESEngine AESEngine

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.engines AESEngine AESEngine.

Prototype

public AESEngine() 

Source Link

Document

default constructor - 128 bit block size.

Usage

From source file:net.sourceforge.keepassj2me.keydb.KeydbDatabase.java

License:Open Source License

/**
 * Encode database//  w w w.  j ava 2s .  c o m
 * @return encoded database
 * @throws KeydbException
 */
public byte[] getEncoded() throws KeydbException {//Encrypt content
    if (isLocked())
        return this.encodedContent;

    if ((this.header.numGroups == 0) && (this.header.numEntries == 0))
        throw new KeydbException(Config.getLocaleString(keys.KD_NOTHING_SAVE));

    BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(new AESEngine()));

    //calc padding size
    int block_size = cipher.getBlockSize();
    int pad_size = block_size - this.contentSize % block_size;

    // #ifdef DEBUG
    System.out.println("contentSize: " + this.contentSize);
    System.out.println("block_size: " + block_size);
    System.out.println("pad_size: " + pad_size);
    // #endif

    //add padding to content
    byte temp[] = new byte[this.contentSize + pad_size];
    System.arraycopy(this.plainContent, 0, temp, 0, this.contentSize);
    KeydbUtil.fill(this.plainContent, (byte) 0);
    this.plainContent = temp;
    temp = null;
    PKCS7Padding padding = new PKCS7Padding();
    padding.addPadding(this.plainContent, this.contentSize);

    byte encoded[] = new byte[KeydbHeader.SIZE + this.contentSize + pad_size];

    //encode
    cipher.init(true, new ParametersWithIV(new KeyParameter(this.key), this.header.encryptionIV));

    int paddedEncryptedPartSize = cipher.processBytes(this.plainContent, 0, this.plainContent.length, encoded,
            KeydbHeader.SIZE);

    if (paddedEncryptedPartSize != this.plainContent.length) {
        // #ifdef DEBUG
        System.out.println("Encoding: " + paddedEncryptedPartSize + " != " + this.plainContent.length);
        // #endif
        throw new KeydbException(Config.getLocaleString(keys.KD_ENCRYPTING_FAILED));
    }

    //Set header
    this.header.contentsHash = KeydbUtil.hash(this.plainContent, 0, this.contentSize);
    this.header.write(encoded, 0);

    return encoded;
}

From source file:net.sourceforge.keepassj2me.keydb.KeydbDatabase.java

License:Open Source License

/**
 * Decrypt master key//from  w ww .  j a  va  2 s  . c om
 * @param pKeySeed
 * @param pKey
 * @param rounds
 * @return
 * @throws KeydbException
 */
private byte[] transformMasterKey(byte[] pKeySeed, byte[] pKey, int rounds, int start_procent, int end_procent)
        throws KeydbException {
    byte[] newKey = new byte[pKey.length];
    System.arraycopy(pKey, 0, newKey, 0, pKey.length);

    BufferedBlockCipher cipher = new BufferedBlockCipher(new AESEngine());
    cipher.init(true, new KeyParameter(pKeySeed));

    int procent = start_procent; // start_procent% - progress start
    int step = 5;// % step
    int roundsByStep = rounds * step / ((end_procent - procent)); // end_procent% - progress end
    int count = 0;

    for (int i = 0; i < rounds; i++) {
        cipher.processBytes(newKey, 0, newKey.length, newKey, 0);

        if (++count == roundsByStep) {
            count = 0;
            setProgress(procent += step, null);
        }
        ;
    }
    ;

    byte[] transformedMasterKey = KeydbUtil.hash(newKey);
    KeydbUtil.fill(newKey, (byte) 0);

    return transformedMasterKey;
}

From source file:net.sourceforge.keepassj2me.keydb.KeydbDatabase.java

License:Open Source License

private void decrypt(byte[] encoded, int offset, int length) throws KeydbException {
    BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(new AESEngine()));

    cipher.init(false, new ParametersWithIV(new KeyParameter(this.key), this.header.encryptionIV));

    // Decrypt! The first bytes aren't encrypted (that's the header)
    this.plainContent = new byte[encoded.length - KeydbHeader.SIZE];
    int paddedEncryptedPartSize = cipher.processBytes(encoded, offset, length, this.plainContent, 0);

    //detect padding and calc content size 
    this.contentSize = 0;
    PKCS7Padding padding = new PKCS7Padding();
    try {//from www.  ja v  a2  s.c  o m
        this.contentSize = paddedEncryptedPartSize - padding.padCount(this.plainContent);
    } catch (InvalidCipherTextException e) {
        throw new KeydbException(Config.getLocaleString(keys.KD_DB_DECRYPT_ERR, new String[] { "1" }));
    }

    if (!KeydbUtil.compare(KeydbUtil.hash(this.plainContent, 0, this.contentSize), this.header.contentsHash)) {
        throw new KeydbException(Config.getLocaleString(keys.KD_DB_DECRYPT_ERR, new String[] { "2" }));
    }
}

From source file:org.albertschmitt.crypto.AESService.java

License:Open Source License

/**
 * Return a PaddedBufferedBlockCipher for encryption or decryption.
 *
 * @param iv The initialization vector.//from  w w  w .j  ava 2  s . co m
 * @param forEncryption True to encrypt, false to decrypt.
 * @return PaddedBufferedBlockCipher configured to encrypt or decrypt.
 */
private PaddedBufferedBlockCipher getCipher(byte[] iv, Boolean forEncryption) {
    ParametersWithIV ivKeyParam = new ParametersWithIV(aes_key, iv);
    BlockCipherPadding padding = new PKCS7Padding();
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            padding);
    cipher.reset();
    cipher.init(forEncryption, ivKeyParam);

    return cipher;
}

From source file:org.apache.zeppelin.user.Encryptor.java

License:Apache License

public Encryptor(String encryptKey) {
    encryptCipher = new PaddedBufferedBlockCipher(new AESEngine(), new ZeroBytePadding());
    encryptCipher.init(true, new KeyParameter(encryptKey.getBytes()));

    decryptCipher = new PaddedBufferedBlockCipher(new AESEngine(), new ZeroBytePadding());
    decryptCipher.init(false, new KeyParameter(encryptKey.getBytes()));
}

From source file:org.avasquez.seccloudfs.utils.CryptoUtils.java

License:Open Source License

/**
 * Creates a cipher that uses AES encryption with GCM block mode.
 *
 * @param forEncryption true if the cipher is going to be used for encryption, false for decryption
 * @param key           the encryption key
 * @param iv            the initialization vector, or nonce
 *
 * @return the initialized cipher//w  ww . java 2s  . co  m
 */
public static AEADBlockCipher createAesWithGcmCipher(boolean forEncryption, byte[] key, byte[] iv) {
    AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
    cipher.init(forEncryption, new AEADParameters(new KeyParameter(key), 128, iv));

    return cipher;
}

From source file:org.bunkr.core.crypto.CipherBuilder.java

License:Open Source License

/**
 * Build a block cipher for encrypting or decrypting the target file.
 *
 * Intelligently create the correct cipher and initialize it correctly from the encryptionData present on the
 * target file. If the goal is encryption, the encryption data for the file is reinitialized from random.
 *
 * @param file the target FileInventoryItem
 * @param encrypting boolean indicating encryption (true) or decryption (false)
 * @return a BlockCipher//from   www .j a  va 2  s.  c o m
 */
public static BlockCipher buildCipherForFile(FileInventoryItem file, boolean encrypting) {
    Encryption alg = file.getEncryptionAlgorithm();

    if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.AES)) {
        if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) {
            SICBlockCipher fileCipher = new SICBlockCipher(new AESEngine());
            byte[] edata = file.getEncryptionData();
            if (encrypting) {
                edata = new byte[alg.keyByteLength + fileCipher.getBlockSize()];
                RandomMaker.fill(edata);
                file.setEncryptionData(edata);
            }
            byte[] ekey = Arrays.copyOfRange(edata, 0, alg.keyByteLength);
            byte[] eiv = Arrays.copyOfRange(edata, alg.keyByteLength,
                    alg.keyByteLength + fileCipher.getBlockSize());
            fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(ekey), eiv));
            return fileCipher;
        }
    }

    else if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.TWOFISH)) {
        if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) {
            SICBlockCipher fileCipher = new SICBlockCipher(new TwofishEngine());
            byte[] edata = file.getEncryptionData();
            if (encrypting) {
                edata = new byte[alg.keyByteLength + fileCipher.getBlockSize()];
                RandomMaker.fill(edata);
                file.setEncryptionData(edata);
            }
            byte[] ekey = Arrays.copyOfRange(edata, 0, alg.keyByteLength);
            byte[] eiv = Arrays.copyOfRange(edata, alg.keyByteLength,
                    alg.keyByteLength + fileCipher.getBlockSize());
            fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(ekey), eiv));
            return fileCipher;
        }
    }

    throw new IllegalArgumentException(String.format("Unsupported algorithm: %s", alg));
}

From source file:org.bunkr.core.crypto.CipherBuilder.java

License:Open Source License

/**
 * Simple version of buildCipherForFile, this time without the encryption data manipulation or file object.
 *
 * @param alg the encryption algorithm being used
 * @param key encryption key data bytes//from w  w  w . j  a v  a 2s . co  m
 * @param iv initialization vector data bytes
 * @return a BlockCipher
 */
public static BlockCipher buildCipher(Encryption alg, boolean encrypting, byte[] key, byte[] iv) {
    if (key.length != alg.keyByteLength)
        throw new IllegalArgumentException(String.format("Supplied key length %s != required key length %s",
                key.length, alg.keyByteLength));
    if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.AES)) {
        SICBlockCipher fileCipher = new SICBlockCipher(new AESEngine());
        if (iv.length != fileCipher.getBlockSize())
            throw new IllegalArgumentException(String.format("Supplied iv length %s != required iv length %s",
                    iv.length, fileCipher.getBlockSize()));
        if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) {
            fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(key), iv));
            return fileCipher;
        }
    }

    else if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.TWOFISH)) {
        SICBlockCipher fileCipher = new SICBlockCipher(new TwofishEngine());
        if (iv.length != fileCipher.getBlockSize())
            throw new IllegalArgumentException(String.format("Supplied iv length %s != required iv length %s",
                    iv.length, fileCipher.getBlockSize()));
        if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) {
            fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(key), iv));
            return fileCipher;
        }
    }

    throw new IllegalArgumentException(String.format("Unsupported algorithm: %s", alg));
}

From source file:org.ccnx.ccn.impl.security.crypto.jce.AESWrapWithPadEngine.java

License:Open Source License

public AESWrapWithPadEngine() {
    super(new AESEngine());
}

From source file:org.cryptacular.util.CipherUtilTest.java

License:Open Source License

@DataProvider(name = "block-cipher")
public Object[][] getBlockCipherData() {
    return new Object[][] { new Object[] {
            // Plaintext is NOT multiple of block size
            "Able was I ere I saw elba.", new CBCBlockCipher(new AESEngine()), new RBGNonce(16), },
            // Plaintext is multiple of block size
            new Object[] { "Four score and seven years ago, our forefathers ",
                    new CBCBlockCipher(new BlowfishEngine()), new RBGNonce(8), },
            // OFB
            new Object[] { "Have you passed through this night?", new OFBBlockCipher(new BlowfishEngine(), 64),
                    new LongCounterNonce(), },
            // CFB
            new Object[] {
                    "I went to the woods because I wished to live deliberately, to "
                            + "front only the essential facts of life",
                    new CFBBlockCipher(new AESEngine(), 128), new RBGNonce(16), }, };
}