Java Code Examples for org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher#init()

The following examples show how to use org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher#init() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: aes.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
public static ByteBuffer encrypt(byte[] key, byte[] iv, byte[] plaintext) {
    assert (key.length == 64 && iv.length == 32);
    try {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));

        cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] outBuf = new byte[cipher.getOutputSize(plaintext.length)];
        int processed = cipher.processBytes(plaintext, 0, plaintext.length, outBuf, 0);
        processed += cipher.doFinal(outBuf, processed);

        ByteBuffer byteBuffer = ByteBuffer.allocate(processed);
        byteBuffer.put(outBuf, 0, processed);
        return byteBuffer;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 2
Source File: aes.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
public static ByteBuffer decrypt(byte[] key, byte[] iv, byte[] cipertext) {
    try {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] clear = new byte[cipher.getOutputSize(cipertext.length)];
        int len = cipher.processBytes(cipertext, 0, cipertext.length, clear, 0);
        len += cipher.doFinal(clear, len);
        ByteBuffer byteBuffer = ByteBuffer.allocate(len);
        byteBuffer.put(clear, 0, len);
        return byteBuffer;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}
 
Example 3
Source File: aes.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
public static ByteBuffer encrypt(byte[] key, byte[] iv, byte[] plaintext) {
    assert (key.length == 32 && iv.length == 16);
    try
    {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));

        cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] outBuf   = new byte[cipher.getOutputSize(plaintext.length)];
        int processed = cipher.processBytes(plaintext, 0, plaintext.length, outBuf, 0);
        processed += cipher.doFinal(outBuf, processed);

        ByteBuffer byteBuffer = ByteBuffer.allocate(processed);
        byteBuffer.put(outBuf, 0, processed);
        return byteBuffer;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return null;
}
 
Example 4
Source File: aes.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
public static ByteBuffer decrypt(byte[] key, byte[] iv, byte[] cipertext) {
    try
    {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] clear = new byte[cipher.getOutputSize(cipertext.length)];
        int len = cipher.processBytes(cipertext, 0, cipertext.length, clear,0);
        len += cipher.doFinal(clear, len);
        ByteBuffer byteBuffer = ByteBuffer.allocate(len);
        byteBuffer.put(clear, 0, len);
        return byteBuffer;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return null;

}
 
Example 5
Source File: aes.java    From bitshares_wallet with MIT License 6 votes vote down vote up
public static ByteBuffer encrypt(byte[] key, byte[] iv, byte[] plaintext) {
    assert (key.length == 32 && iv.length == 16);
    try
    {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));

        cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] outBuf   = new byte[cipher.getOutputSize(plaintext.length)];
        int processed = cipher.processBytes(plaintext, 0, plaintext.length, outBuf, 0);
        processed += cipher.doFinal(outBuf, processed);

        ByteBuffer byteBuffer = ByteBuffer.allocate(processed);
        byteBuffer.put(outBuf, 0, processed);
        return byteBuffer;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return null;
}
 
Example 6
Source File: aes.java    From bitshares_wallet with MIT License 6 votes vote down vote up
public static ByteBuffer decrypt(byte[] key, byte[] iv, byte[] cipertext) {
    try
    {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] clear = new byte[cipher.getOutputSize(cipertext.length)];
        int len = cipher.processBytes(cipertext, 0, cipertext.length, clear,0);
        len += cipher.doFinal(clear, len);
        ByteBuffer byteBuffer = ByteBuffer.allocate(len);
        byteBuffer.put(clear, 0, len);
        return byteBuffer;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return null;

}
 
Example 7
Source File: SensitiveDataPreApi23.java    From android-java-connect-rest-sample with MIT License 6 votes vote down vote up
protected byte[] encrypt(byte[] data) {
    // 16 bytes is the IV size for AES256
    try {
        SecretKey key = loadKey();

        // Random IV
        SecureRandom rng = new SecureRandom();
        byte[] ivBytes = new byte[16];                                                                  // 16 bytes is the IV size for AES256
        rng.nextBytes(ivBytes);

        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(true, new ParametersWithIV(new KeyParameter(key.getEncoded()), ivBytes));

        byte[] encryptedData = cipherData(cipher, data);
        byte[] encryptedDataWithIV = new byte[encryptedData.length + ivBytes.length];                   // Make room for IV
        System.arraycopy(ivBytes, 0, encryptedDataWithIV, 0, ivBytes.length);                           // Add IV
        System.arraycopy(encryptedData, 0, encryptedDataWithIV, ivBytes.length, encryptedData.length);  // Then the encrypted data
        return encryptedDataWithIV;
    }
    catch(InvalidCipherTextException e) {
        Log.e(TAG, "Can't encrypt data", e);
    }
    return null;
}
 
Example 8
Source File: SensitiveDataPreApi23.java    From android-java-connect-rest-sample with MIT License 6 votes vote down vote up
protected byte[] decrypt(byte[] data) {

        try {
            SecretKey key = loadKey();

            byte[] ivBytes = new byte[16];                                                                  // 16 bytes is the IV size for AES256
            System.arraycopy(data, 0, ivBytes, 0, ivBytes.length);                                          // Get IV from data
            byte[] dataWithoutIV = new byte[data.length - ivBytes.length];                                  // Remove the room made for the IV
            System.arraycopy(data, ivBytes.length, dataWithoutIV, 0, dataWithoutIV.length);                 // Then the encrypted data

            PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
            cipher.init(false, new ParametersWithIV(new KeyParameter(key.getEncoded()), ivBytes));

            return cipherData(cipher, dataWithoutIV);
        }
        catch(InvalidCipherTextException e) {
            Log.e(TAG, "Can't decrypt data", e);
        }
        return null;
    }
 
Example 9
Source File: IoUtil.java    From OpenYOLO-Android with Apache License 2.0 5 votes vote down vote up
static PaddedBufferedBlockCipher createAes128CtrPkcs7PaddingCipher(
        boolean encrypting,
        byte[] iv,
        byte[] key) {
    AESEngine aes = new AESEngine();
    SICBlockCipher aesCtr = new SICBlockCipher(aes);
    PaddedBufferedBlockCipher aesCtrPkcs7 =
            new PaddedBufferedBlockCipher(aesCtr, new PKCS7Padding());
    aesCtrPkcs7.init(encrypting, new ParametersWithIV(new KeyParameter(key), iv));

    return aesCtrPkcs7;
}
 
Example 10
Source File: Encryption.java    From KeePassJava2 with Apache License 2.0 5 votes vote down vote up
/**
 * Create a decrypted input stream from an encrypted one
 */
public static InputStream getDecryptedInputStream (InputStream encryptedInputStream, byte[] keyData, byte[] ivData) {
    final ParametersWithIV keyAndIV = new ParametersWithIV(new KeyParameter(keyData), ivData);
    PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    pbbc.init(false, keyAndIV);
    return new CipherInputStream(encryptedInputStream, pbbc);
}
 
Example 11
Source File: Encryption.java    From KeePassJava2 with Apache License 2.0 5 votes vote down vote up
/**
 * Create an encrypted output stream from an unencrypted output stream
 */
public static OutputStream getEncryptedOutputStream (OutputStream decryptedOutputStream, byte[] keyData, byte[] ivData) {
    final ParametersWithIV keyAndIV = new ParametersWithIV(new KeyParameter(keyData), ivData);
    PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    pbbc.init(true, keyAndIV);
    return new CipherOutputStream(decryptedOutputStream, pbbc);
}
 
Example 12
Source File: Crypto.java    From KeePassJava2 with Apache License 2.0 2 votes vote down vote up
/**
 * Get a cipher
 *
 * @param mode encryption or decryption
 * @param iv   a 16 byte iv
 * @return an initialised Cipher
 */
PaddedBufferedBlockCipher getCipher(CMode mode, byte[] iv) {
    PaddedBufferedBlockCipher result = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    result.init(mode.getEncrypt(), new ParametersWithIV(new KeyParameter(getKey()), iv));
    return result;
}