Example usage for org.bouncycastle.crypto.paddings PKCS7Padding PKCS7Padding

List of usage examples for org.bouncycastle.crypto.paddings PKCS7Padding PKCS7Padding

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.paddings PKCS7Padding PKCS7Padding.

Prototype

PKCS7Padding

Source Link

Usage

From source file:ch.dissem.bitmessage.cryptography.bc.BouncyCryptography.java

License:Apache License

@Override
public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) {
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new PKCS7Padding());
    CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector);

    cipher.init(encrypt, params);/*from  w w  w .  ja  v a  2s  . c  om*/

    byte[] buffer = new byte[cipher.getOutputSize(data.length)];
    int length = cipher.processBytes(data, 0, data.length, buffer, 0);
    try {
        length += cipher.doFinal(buffer, length);
    } catch (InvalidCipherTextException e) {
        throw new IllegalArgumentException(e);
    }
    if (length < buffer.length) {
        return Arrays.copyOfRange(buffer, 0, length);
    }
    return buffer;
}

From source file:co.lqnt.lockbox.Cipher.java

License:Open Source License

/**
 * Construct a new bi-directional cipher.
 *//*from   w w  w .j a  v a 2s.  c o  m*/
public Cipher() {
    CodecInterface base64UriCodec = new Base64UriCodec();
    AsymmetricBlockCipher rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest());
    BufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new PKCS7Padding());
    Digest sha1Digest = new SHA1Digest();
    SecureRandom random = new SecureRandom();

    this.encryptionCipher = new EncryptionCipher(base64UriCodec, rsaCipher, aesCipher, sha1Digest, random);
    this.decryptionCipher = new DecryptionCipher(base64UriCodec, rsaCipher, aesCipher, sha1Digest);
}

From source file:co.lqnt.lockbox.DecryptionCipher.java

License:Open Source License

/**
 * Construct a new decryption cipher./*from   ww w.j a v a2  s. c  om*/
 */
public DecryptionCipher() {
    this.base64UriCodec = new Base64UriCodec();
    this.rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest());
    this.aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
    this.sha1Digest = new SHA1Digest();
    this.asciiCharset = Charset.forName("US-ASCII");
}

From source file:co.lqnt.lockbox.EncryptionCipher.java

License:Open Source License

/**
 * Construct a new encryption cipher.// w  w w.  j  av  a 2 s  . c o m
 */
public EncryptionCipher() {
    this.base64UriCodec = new Base64UriCodec();
    this.rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest());
    this.aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
    this.sha1Digest = new SHA1Digest();
    this.random = new SecureRandom();
    this.asciiCharset = Charset.forName("US-ASCII");
}

From source file:com.geoxp.oss.CryptoHelper.java

License:Apache License

/**
 * Pad data using PKCS7./*  w  ww .  j a  v  a 2s  .  c  o  m*/
 * 
 * @param alignment Alignement on which to pad, e.g. 8
 * @param data Data to pad
 * @return The padded data
 */
public static byte[] padPKCS7(int alignment, byte[] data, int offset, int len) {

    //
    // Allocate the target byte array. Its size is a multiple of 'alignment'.
    // If data to pad is a multiple of 'alignment', the target array will be
    // 'alignment' bytes longer than the data to pad.
    //

    byte[] target = new byte[len + (alignment - len % alignment)];

    //
    // Copy the data to pad into the target array
    //

    System.arraycopy(data, offset, target, 0, len);

    //
    // Add padding bytes
    //

    PKCS7Padding padding = new PKCS7Padding();

    padding.addPadding(target, len);

    return target;
}

From source file:com.geoxp.oss.CryptoHelper.java

License:Apache License

/**
 * Remove PKCS7 padding from padded data
 * @param padded The padded data to 'unpad'
 * @return The original unpadded data//from   w ww  . j av a2  s.c  o m
 * @throws InvalidCipherTextException if data is not correctly padded
 */
public static byte[] unpadPKCS7(byte[] padded) throws InvalidCipherTextException {
    PKCS7Padding padding = new PKCS7Padding();

    //
    // Determine length of padding
    //

    int pad = padding.padCount(padded);

    //
    // Allocate array for unpadded data
    //

    byte[] unpadded = new byte[padded.length - pad];

    //
    // Copy data without the padding
    //

    System.arraycopy(padded, 0, unpadded, 0, padded.length - pad);

    return unpadded;
}

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

License:Apache License

@Override
public void run() {
    PaddedBufferedBlockCipher cipher;//from w  ww .ja  va2s. c o  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();//from www  . j av a 2s.com

    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 {//from   w w  w .j  a  v a2  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);
    }
}

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.  j  a v a 2s .com
        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);
    }
}