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.android.isoma.enc.Encryption.java

License:Open Source License

public synchronized byte[] encrypt(byte[] data)
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    byte[] iv = null;
    if (data == null || data.length == 0) {
        return new byte[0];
    }//from  w ww  . java2 s .c om
    BufferedBlockCipher encryptcipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new ZeroBytePadding());

    String source = "00000000";
    iv = source.getBytes();

    CipherParameters ivAndKey = new ParametersWithIV(key, iv);
    encryptcipher.init(true, ivAndKey);

    return callCipher(encryptcipher, data);
}

From source file:com.android.isoma.enc.Encryption.java

License:Open Source License

public synchronized byte[] decrypt(byte[] data)
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    byte[] iv = null;
    if (data == null || data.length == 0) {
        return new byte[0];
    }/*w ww .j  a v  a  2 s  . c o m*/

    BufferedBlockCipher decryptcipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new ZeroBytePadding());

    String source = "00000000";
    iv = source.getBytes();

    CipherParameters ivAndKey = new ParametersWithIV(key, iv);

    decryptcipher.init(false, ivAndKey);

    return callCipher(decryptcipher, data);
}

From source file:com.codename1.crypto.EncryptedStorage.java

License:Open Source License

private void InitCiphers() {
    encryptCipher = new PaddedBufferedBlockCipher(new AESEngine());
    encryptCipher.init(true, new KeyParameter(key));
    decryptCipher = new PaddedBufferedBlockCipher(new AESEngine());
    decryptCipher.init(false, new KeyParameter(key));
}

From source file:com.completetrsst.crypto.Crypto.java

License:Apache License

private static byte[] _cryptIES(byte[] input, Key recipient, boolean forEncryption)
        throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    IESCipher cipher = new IESCipher(new IESEngine(new ECDHBasicAgreement(),
            new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA256Digest()),
            new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()))));

    cipher.engineInit(forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, recipient, new SecureRandom());
    return cipher.engineDoFinal(input, 0, input.length);
}

From source file:com.completetrsst.crypto.Crypto.java

License:Apache License

private static byte[] _cryptBytesAES(byte[] input, byte[] key, boolean forEncryption)
        throws InvalidCipherTextException {
    assert key.length == 32; // 32 bytes == 256 bits
    return process(input, new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())),
            new KeyParameter(key), forEncryption);
    // note: using zero IV because we generate a new key for every message
}

From source file:com.giacomodrago.immediatecrypt.aes.AESFacadeImpl.java

License:Open Source License

@Override
public AESEncryptedMessage encrypt(byte[] plaintext, String password) throws EncryptionException {

    // Check password is not empty
    if (password.isEmpty()) {
        throw new EncryptionException("Password is empty.");
    }//  w  w  w  . j a  v  a2 s.  c o m

    // Generate random password salt
    String salt = RandomStringUtils.randomAlphanumeric(SALT_LENGTH);

    ParametersWithIV params = createEncryptionParameters(password, salt);
    byte[] iv = params.getIV();

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

    cipher.init(true, params);

    byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)];

    int outputLen = cipher.processBytes(plaintext, 0, plaintext.length, ciphertext, 0);
    try {
        cipher.doFinal(ciphertext, outputLen);
    } catch (DataLengthException ex) {
        throw new EncryptionException(ex);
    } catch (IllegalStateException ex) {
        throw new EncryptionException(ex);
    } catch (InvalidCipherTextException ex) {
        throw new EncryptionException(ex);
    }

    return new AESEncryptedMessage(salt, iv, ciphertext);

}

From source file:com.giacomodrago.immediatecrypt.aes.AESFacadeImpl.java

License:Open Source License

@Override
public byte[] decrypt(AESEncryptedMessage encryptedMessage, String password) throws EncryptionException {

    byte[] ciphertext = encryptedMessage.getCiphertext();
    String salt = encryptedMessage.getSalt();
    byte[] iv = encryptedMessage.getIv();

    ParametersWithIV params = createDecryptionParameters(password, salt, iv);

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

    try {//from w  w w  .  j a va2 s.c  o m
        cipher.init(false, params);
    } catch (IllegalArgumentException ex) {
        throw new EncryptionException(ex);
    }

    byte[] plaintext = new byte[cipher.getOutputSize(ciphertext.length)];

    int outputLen = cipher.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0);
    try {
        cipher.doFinal(plaintext, outputLen);
    } catch (DataLengthException ex) {
        throw new EncryptionException(ex);
    } catch (IllegalStateException ex) {
        throw new EncryptionException(ex);
    } catch (InvalidCipherTextException ex) {
        throw new EncryptionException(ex);
    }

    return plaintext;

}

From source file:com.github.flbaue.jcrypttool.v1.DecryptionRunnable.java

License:Apache License

@Override
public void run() {
    PaddedBufferedBlockCipher cipher;//  w  ww. java 2s.co  m
    byte[] key;
    byte[] iv;
    byte[] salt;

    try (InputStream fileInputStream = new BufferedInputStream(
            new FileInputStream(encryptionSettings.inputFile))) {

        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
        salt = extractSalt(fileInputStream);
        iv = extractIV(fileInputStream);
        key = generateKey(encryptionSettings.password, salt);
        KeyParameter keyParam = new KeyParameter(key);
        CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(false, params);

        /*
        System.out.println(getClass().getName() + " salt:\t" + Base64.toBase64String(salt) + " (" + salt.length + " byte)");
        System.out.println(getClass().getName() + " key:\t" + Base64.toBase64String(key) + " (" + key.length + " byte)");
        System.out.println(getClass().getName() + " iv:\t\t" + Base64.toBase64String(iv) + " (" + iv.length + " byte)");
        */

        InputStream in = null;
        OutputStream out = null;
        try {
            in = new GZIPInputStream(new CipherInputStream(fileInputStream, cipher));
            out = new BufferedOutputStream(new FileOutputStream(encryptionSettings.outputFile));
            processStreams(in, out);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeStream(in);
            closeStream(out);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.flbaue.jcrypttool.v1.EncryptionRunnable.java

License:Apache License

@Override
public void run() {
    progress.start();/*  w  w w .j a  v  a 2s .  c  om*/

    final byte[] salt = generateSalt();
    final byte[] key = generateKey(encryptionSettings.password, salt);
    final byte[] iv;
    final PaddedBufferedBlockCipher cipher;

    try (OutputStream fileOutputStream = new BufferedOutputStream(
            new FileOutputStream(encryptionSettings.outputFile))) {

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

        /*
        System.out.println(getClass().getName() + " salt:\t" + Base64.toBase64String(salt) + " (" + salt.length + " byte)");
        System.out.println(getClass().getName() + " key:\t" + Base64.toBase64String(key) + " (" + key.length + " byte)");
        System.out.println(getClass().getName() + " iv:\t\t" + Base64.toBase64String(iv) + " (" + iv.length + " byte)");
        */

        InputStream in = null;
        OutputStream out = null;
        try {
            writeInitBlock(fileOutputStream, salt, iv);
            in = new BufferedInputStream(new FileInputStream(encryptionSettings.inputFile));
            out = new GZIPOutputStream(new CipherOutputStream(fileOutputStream, cipher));
            processStreams(in, out);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeStream(in);
            closeStream(out);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    progress.setFinished();
}

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

License:Apache License

@Override
public OutputStream encryptedOutputStream(final Path path, final String password)
        throws IOException, EncryptionFailedException {
    try {// w w  w  . ja v  a  2  s  . c o  m
        final byte[] salt = generateSalt();
        final byte[] key = generateKey(password, salt);
        final byte[] iv = generateIV();
        final byte[] fileInitBlock = 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 BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(path));
        out.write(fileInitBlock);

        return new CipherOutputStream(out, cipher);
    } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
        throw new EncryptionFailedException(e);
    }
}