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:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java

License:Apache License

@Override
public String encryptString(final String string, final String password) throws EncryptionFailedException {
    try {/*from w  w w  . ja  v a2s  . c om*/
        final byte[] salt = generateSalt();
        final byte[] key = generateKey(password, salt);
        final byte[] iv = generateIV();
        final byte[] outputInitBlock = generateOutputInitBlock(salt, iv);

        final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                new CBCBlockCipher(new AESEngine()), new PKCS7Padding());

        final KeyParameter keyParam = new KeyParameter(key);
        final CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(true, params);

        final byte in[] = string.getBytes();
        final byte out[] = new byte[cipher.getOutputSize(in.length)];
        final int len1 = cipher.processBytes(in, 0, in.length, out, 0);

        cipher.doFinal(out, len1);

        final byte[] result = Arrays.concatenate(outputInitBlock, out);

        return Base64.toBase64String(result);
    } catch (InvalidKeySpecException | NoSuchAlgorithmException | InvalidCipherTextException e) {
        throw new EncryptionFailedException(e);
    }
}

From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java

License:Apache License

@Override
public InputStream decryptedInputStream(final Path path, final String password)
        throws IOException, DecryptionFailedException {
    try {//from   w w  w  . j a  va  2s. com
        InputStream in = new BufferedInputStream(Files.newInputStream(path));
        byte[] initBlock = readInitBlock(in);
        byte[] salt = extractSalt(initBlock);
        byte[] iv = extractIV(initBlock);
        byte[] key = generateKey(password, salt);

        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
                new PKCS7Padding());
        KeyParameter keyParam = new KeyParameter(key);
        CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(false, params);

        return new CipherInputStream(in, cipher);
    } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
        throw new DecryptionFailedException(e);
    }
}

From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java

License:Apache License

@Override
public String decryptString(final String string, final String password) throws DecryptionFailedException {
    try {//from  w  ww.j  a v  a2s  .  c om
        byte[] inputBytes = Base64.decode(string);
        byte[] salt = extractSalt(inputBytes);
        byte[] iv = extractIV(inputBytes);
        byte[] key = generateKey(password, salt);
        byte[] encryptedData = extractEncryptedData(inputBytes);

        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
                new PKCS7Padding());
        KeyParameter keyParam = new KeyParameter(key);
        CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(false, params);

        final byte out[] = new byte[cipher.getOutputSize(encryptedData.length)];
        int length = cipher.processBytes(encryptedData, 0, encryptedData.length, out, 0);
        byte[] result;

        length += cipher.doFinal(out, length);
        result = new byte[length];
        System.arraycopy(out, 0, result, 0, length);

        return new String(result);

    } catch (InvalidCipherTextException | InvalidKeySpecException | NoSuchAlgorithmException e) {
        throw new DecryptionFailedException(e);
    }
}

From source file:com.github.horrorho.inflatabledonkey.cache.StreamCryptor.java

License:Open Source License

public CipherOutputStream newCipherOutputStream(OutputStream os, byte[] password) throws IOException {
    byte[] salt = randomBytes(saltLength);
    byte[] nonce = randomBytes(nonceLength);
    os.write(salt);/* ww  w. j  a v a 2s  . c  o m*/
    os.write(nonce);
    byte[] dk = kdf.apply(password, salt);
    GCMBlockCipher cipher = new GCMBlockCipher(new AESEngine());
    AEADParameters parameters = new AEADParameters(new KeyParameter(dk), tagLength * 8, nonce);
    cipher.init(true, parameters);
    return new CipherOutputStream(os, cipher);
}

From source file:com.github.horrorho.inflatabledonkey.cache.StreamCryptor.java

License:Open Source License

public CipherInputStream newCipherInputStream(InputStream is, byte[] password) throws IOException {
    byte[] salt = IOUtils.readFully(is, saltLength);
    byte[] nonce = IOUtils.readFully(is, nonceLength);
    byte[] dk = kdf.apply(password, salt);
    GCMBlockCipher cipher = new GCMBlockCipher(new AESEngine());
    AEADParameters parameters = new AEADParameters(new KeyParameter(dk), tagLength * 8, nonce);
    cipher.init(false, parameters);/*from   w w w  .  j a  v a2 s.c  om*/
    return new CipherInputStream(is, cipher);
}

From source file:com.github.horrorho.inflatabledonkey.dataprotection.DPAESCBCCipher.java

License:Open Source License

public DPAESCBCCipher(int blockSize) {
    this(new CBCBlockCipher(new AESEngine()), blockSize);
}

From source file:com.github.horrorho.liquiddonkey.cloud.file.FileDecrypter.java

License:Open Source License

/**
 * Returns a new instance.//from www .ja v  a2  s  . co  m
 *
 * @return a new instance, not null
 */
public static FileDecrypter create() {
    return FileDecrypter.from(new BufferedBlockCipher(new CBCBlockCipher(new AESEngine())), new SHA1Digest());
}

From source file:com.github.horrorho.liquiddonkey.cloud.store.ChunkDecrypter.java

License:Open Source License

/**
 * Returns a new instance./*from  w  w  w  .j a v  a 2 s. co  m*/
 *
 * @return a new instance, not null
 */
public static ChunkDecrypter create() {
    return new ChunkDecrypter(new CFBBlockCipher(new AESEngine(), 128), new SHA256Digest());
}

From source file:com.healthmarketscience.jackcess.impl.office.ECMAStandardEncryptionProvider.java

License:Apache License

@Override
protected BlockCipher initCipher() {
    return new AESEngine();
}

From source file:com.licel.jcardsim.crypto.SymmetricKeyImpl.java

License:Apache License

/**
 * Return the BouncyCastle <code>BlockCipher</code> for using with this key
 * @return <code>BlockCipher</code> for this key, or null for HMACKey
 * @throws CryptoException if key not initialized
 * @see BlockCipher/*from w  w  w.  j  a va 2  s. c om*/
 */
public BlockCipher getCipher() throws CryptoException {
    if (!key.isInitialized()) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }
    BlockCipher cipher = null;
    switch (type) {
    case KeyBuilder.TYPE_DES:
    case KeyBuilder.TYPE_DES_TRANSIENT_DESELECT:
    case KeyBuilder.TYPE_DES_TRANSIENT_RESET:
        if (size == KeyBuilder.LENGTH_DES) {
            cipher = new DESEngine();
        }
        if (size == KeyBuilder.LENGTH_DES3_2KEY || size == KeyBuilder.LENGTH_DES3_3KEY) {
            cipher = new DESedeEngine();
        }
        break;
    case KeyBuilder.TYPE_AES:
    case KeyBuilder.TYPE_AES_TRANSIENT_DESELECT:
    case KeyBuilder.TYPE_AES_TRANSIENT_RESET:
        cipher = new AESEngine();
        break;
    }
    return cipher;
}