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

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

Introduction

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

Prototype

public PaddedBufferedBlockCipher(BlockCipher cipher) 

Source Link

Document

Create a buffered block cipher PKCS7 padding

Usage

From source file:com.javacreed.api.secureproperties.bouncycastle.TwoFishCipherFactory.java

License:Apache License

@Override
protected OutputStream wrapInToCipheredOutputStream(final OutputStream out) throws Exception {
    final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new TwofishEngine()));
    cipher.init(true, new KeyParameter(key.getBytes("UTF-8")));
    final CipherOutputStream stream = new CipherOutputStream(out, cipher);
    return stream;
}

From source file:com.oneops.cms.crypto.CmsCryptoDES.java

License:Apache License

/**
 * Encrypt.//from  ww  w. j a  v  a  2 s. c  o m
 *
 * @param instr the instr
 * @return the string
 * @throws java.security.GeneralSecurityException the general security exception
 */
@Override
public String encrypt(String instr) throws GeneralSecurityException {
    long t1 = System.currentTimeMillis();
    byte[] in = instr.getBytes();
    PaddedBufferedBlockCipher encryptor = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()));
    encryptor.init(true, keyParameter);
    byte[] cipherText = new byte[encryptor.getOutputSize(in.length)];
    int outputLen = encryptor.processBytes(in, 0, in.length, cipherText, 0);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        encryptor.doFinal(cipherText, outputLen);
        Hex.encode(cipherText, os);
    } catch (Exception e) {
        e.printStackTrace();
        throw new GeneralSecurityException(e);
    }
    long t2 = System.currentTimeMillis();
    logger.debug("Time taken to encrypt(millis) :" + (t2 - t1));
    return ENC_PREFIX + os.toString();
}

From source file:com.oneops.cms.crypto.CmsCryptoDES.java

License:Apache License

private String decryptStr(String instr) throws GeneralSecurityException {
    if (StringUtils.isEmpty(instr)) {
        return instr;
    }//  w  w w .  j  a  v a 2  s . co  m
    long t1 = System.currentTimeMillis();
    PaddedBufferedBlockCipher decryptor = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()));
    decryptor.init(false, keyParameter);
    byte[] in = null;
    byte[] cipherText = null;

    try {
        in = Hex.decode(instr);
        cipherText = new byte[decryptor.getOutputSize(in.length)];

        int outputLen = decryptor.processBytes(in, 0, in.length, cipherText, 0);
        decryptor.doFinal(cipherText, outputLen);
    } catch (Exception e) {
        throw new GeneralSecurityException(e);
    }
    long t2 = System.currentTimeMillis();
    logger.debug("Time taken to decrypt(millis) : " + (t2 - t1));
    return (new String(cipherText)).replaceAll("\\u0000+$", "");
}

From source file:com.smedia.sqzserver.common.util.SerialNumber.java

public byte[] decrypt(String cookieValue, byte[] keyBytes)
        throws InvalidKeyException, UnsupportedEncodingException {
    byte[] encryptedData;
    byte[] decryptedData;
    TwofishEngine t = new TwofishEngine();

    // ///////////////////

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

    cipher.init(false, new KeyParameter(keyBytes));

    // create the key
    // byte[] keyBytes = key.getBytes();
    // Object keyObject = twofish.makeKey(keyBytes, 16);
    // make the length of the string a multiple of
    // the block size
    while (cookieValue.length() < (32 * 8 / 5)) {
        cookieValue += "A";
    }/*ww w.  j ava 2  s . c  o m*/
    // initialize byte arrays that will hold encrypted/decrypted
    // text

    encryptedData = sorensonBase32Decode(cookieValue);
    decryptedData = new byte[cookieValue.length() * 2];

    /*
     * int oLen = cipher.processBytes(encryptedData, 0,
     * encryptedData.length, decryptedData, 0); try {
     * cipher.doFinal(decryptedData, oLen); } catch (CryptoException ce) {
     * ce.printStackTrace(); }
     */
    // String test = new String(rv).trim();

    // ///

    encryptedData = sorensonBase32Decode(cookieValue);
    decryptedData = new byte[cookieValue.length() * 2];
    // Iterate over the byte arrays by 16-byte blocks and decrypt.
    for (int i = 0; i < Array.getLength(encryptedData); i += 16) {
        // twofish.decrypt(encryptedData, i, decryptedData, i, keyObject,
        // 16);

        cipher.processBytes(encryptedData, i, 16, decryptedData, 0);

    }

    return decryptedData;
}

From source file:com.sos.CredentialStore.KeePass.pl.sind.keepass.crypto.BcAESCipher.java

License:Apache License

@Override
public byte[] decrypt(final byte[] key, final byte[] data, final byte[] iv) throws CipherException {
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));

    if (iv != null) {
        cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
    } else {//from  www . java2 s .  c o  m
        cipher.init(false, new KeyParameter(key));
    }

    byte[] decoded = new byte[cipher.getOutputSize(data.length)];

    int out = cipher.processBytes(data, 0, data.length, decoded, 0);
    try {
        out += cipher.doFinal(decoded, out);

        if (out < decoded.length) {
            decoded = Arrays.copyOf(decoded, out);
        }

    } catch (DataLengthException e) {
        // we are padding so shouldn happen
        throw new CipherException("Invalid data lenght", e);
    } catch (IllegalStateException e) {
        throw new CipherException("Decrypting error", e);
    } catch (InvalidCipherTextException e) {
        throw new CipherException("Unable to decrypt data", e);
    }

    return decoded;

}

From source file:com.sos.CredentialStore.KeePass.pl.sind.keepass.crypto.BcAESCipher.java

License:Apache License

@Override
public byte[] encrypt(final byte[] key, final byte[] data, final byte[] iv, final int rounds,
        final boolean padding) throws CipherException {
    BufferedBlockCipher cipher = null;//from   w w  w .j  a v  a 2 s. c om

    if (padding) {
        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
    } else {
        cipher = new BufferedBlockCipher(new AESEngine());
    }

    if (iv != null) {
        cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
    } else {
        cipher.init(true, new KeyParameter(key));
    }

    byte[] encoded = null;
    if (padding) {
        encoded = new byte[cipher.getOutputSize(data.length)];
    } else {
        encoded = new byte[data.length];
    }

    int out = cipher.processBytes(data, 0, data.length, encoded, 0);
    if (rounds > 1) {
        for (int i = 1; i < rounds; i++) {
            out = cipher.processBytes(encoded, 0, encoded.length, encoded, 0);
        }
    }

    try {
        if (padding && out < encoded.length) {
            cipher.doFinal(encoded, out);
        }
    } catch (DataLengthException e) {
        // we are padding so shouldn happen
        throw new CipherException("Invalid data lenght", e);
    } catch (IllegalStateException e) {
        throw new CipherException("Decrypting error", e);
    } catch (InvalidCipherTextException e) {
        throw new CipherException("Unable to decrypt data", e);
    }

    return encoded;
}

From source file:com.thecorpora.qbo.androidapk.AESCipher.java

License:Open Source License

public String encrypt(byte configData[], String key) throws Exception {
    byte[] encryptedConfigData = null;
    AESEngine blockCipher = new AESEngine();
    blockCipher.reset();/*from   w  ww  .  j a v a  2s  .  co  m*/
    CBCBlockCipher cbcCipher = new CBCBlockCipher(blockCipher);
    BufferedBlockCipher bbc = new PaddedBufferedBlockCipher(cbcCipher);

    byte[] salt = new byte[8];
    SecureRandom secure = new SecureRandom();
    secure.nextBytes(salt);

    //intialising in the encryption mode with Key and IV
    bbc.init(true, getKeyParamWithIv(key, salt));
    byte[] encryptedData = new byte[bbc.getOutputSize(configData.length)];

    //process array of bytes
    int noOfBytes = bbc.processBytes(configData, 0, configData.length, encryptedData, 0);

    //process the last block in the buffer
    bbc.doFinal(encryptedData, noOfBytes);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    //writing encrypted data along with the salt in the format readable by open ssl api
    bos.write("Salted__".getBytes());
    bos.write(salt);
    bos.write(encryptedData);
    encryptedConfigData = bos.toByteArray();
    bos.close();

    //      return encryptedConfigData;
    return Base64.encodeToString(encryptedConfigData, Base64.DEFAULT);

}

From source file:com.thoughtworks.go.security.DESEncrypter.java

License:Apache License

private static String decrypt(byte[] key, String cipherText) throws CryptoException {
    try {//from   ww  w  . j  a va 2s. co m
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine()));
        cipher.init(false, new KeyParameter(key));
        byte[] cipherTextBytes = DECODER.decode(cipherText);

        byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)];
        int outputLength = cipher.processBytes(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0);
        cipher.doFinal(plainTextBytes, outputLength);
        int paddingStarts = plainTextBytes.length - 1;
        for (; paddingStarts >= 0; paddingStarts--) {
            if (plainTextBytes[paddingStarts] != 0) {
                break;
            }
        }
        return new String(plainTextBytes, 0, paddingStarts + 1);
    } catch (Exception e) {
        throw new CryptoException(e);
    }
}

From source file:com.thoughtworks.go.security.DESEncrypter.java

License:Apache License

private static String encrypt(byte[] key, String plainText) throws CryptoException {
    try {/*from  w ww  .  j a  va2s . c  o m*/
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine()));
        KeyParameter keyParameter = new KeyParameter(key);
        cipher.init(true, keyParameter);
        byte[] plainTextBytes = plainText.getBytes();
        byte[] cipherTextBytes = new byte[cipher.getOutputSize(plainTextBytes.length)];
        int outputLength = cipher.processBytes(plainTextBytes, 0, plainTextBytes.length, cipherTextBytes, 0);
        cipher.doFinal(cipherTextBytes, outputLength);
        return ENCODER.encodeToString(cipherTextBytes).trim();
    } catch (Exception e) {
        throw new CryptoException(e);
    }
}

From source file:com.thoughtworks.go.security.GoCipher.java

License:Apache License

String cipher(byte[] key, String plainText) throws InvalidCipherTextException {
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine()));
    KeyParameter keyParameter = new KeyParameter(Hex.decode(key));
    cipher.init(true, keyParameter);/*from   w w  w  .  j a  v  a 2  s.c  om*/
    byte[] plainTextBytes = plainText.getBytes();
    byte[] cipherTextBytes = new byte[cipher.getOutputSize(plainTextBytes.length)];
    int outputLength = cipher.processBytes(plainTextBytes, 0, plainTextBytes.length, cipherTextBytes, 0);
    cipher.doFinal(cipherTextBytes, outputLength);
    return Base64.encodeBase64String(cipherTextBytes).trim();
}