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:heat.crypto.Crypto.java

License:Open Source License

public static byte[] aesEncrypt(byte[] plaintext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) {
    try {//from w  w  w. j  a  va2s.c  o  m
        byte[] dhSharedSecret = new byte[32];
        Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey);
        for (int i = 0; i < 32; i++) {
            dhSharedSecret[i] ^= nonce[i];
        }
        byte[] key = sha256().digest(dhSharedSecret);
        byte[] iv = new byte[16];
        secureRandom.get().nextBytes(iv);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(true, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(plaintext.length)];
        int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
        ciphertextLength += aes.doFinal(output, ciphertextLength);
        byte[] result = new byte[iv.length + ciphertextLength];
        System.arraycopy(iv, 0, result, 0, iv.length);
        System.arraycopy(output, 0, result, iv.length, ciphertextLength);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:heat.crypto.Crypto.java

License:Open Source License

public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) {
    try {//from   w ww  .  ja v  a  2 s. c  o  m
        if (ivCiphertext.length < 16 || ivCiphertext.length % 16 != 0) {
            throw new InvalidCipherTextException("invalid ciphertext");
        }
        byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16);
        byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length);
        byte[] dhSharedSecret = new byte[32];
        Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey);
        for (int i = 0; i < 32; i++) {
            dhSharedSecret[i] ^= nonce[i];
        }
        byte[] key = sha256().digest(dhSharedSecret);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(false, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(ciphertext.length)];
        int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0);
        plaintextLength += aes.doFinal(output, plaintextLength);
        byte[] result = new byte[plaintextLength];
        System.arraycopy(output, 0, result, 0, result.length);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:net.sf.jsignpdf.JSignEncryptor.java

License:Mozilla Public License

/**
 * Initialize the cryptographic engine./*from   w ww  .jav  a2  s  . co m*/
 * 
 * @param aKey
 */
public JSignEncryptor(final byte[] aKey) {
    cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new BlowfishEngine()));
    key = new KeyParameter(aKey);
}

From source file:org.bigmouth.nvwa.utils.degist.AesUtils.java

License:Apache License

/**
 * Encrypt data.//from w  ww .j a v  a2  s  .c o m
 * 
 * @param plain
 * @param key
 * @param iv
 * @return
 * @throws Exception
 */
public static byte[] encrypt(byte[] plain, byte[] key, byte[] iv) throws Exception {
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(true, ivAndKey);
    return cipherData(aes, plain);
}

From source file:org.bigmouth.nvwa.utils.degist.AesUtils.java

License:Apache License

/**
 * Decrypt data./*  ww w  . j a v a 2s  .  c om*/
 * 
 * @param cipher
 * @param key
 * @param iv
 * @return
 * @throws Exception
 */
public static byte[] decrypt(byte[] cipher, byte[] key, byte[] iv) throws Exception {
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(false, ivAndKey);
    return cipherData(aes, cipher);
}

From source file:org.jruby.ext.openssl.x509store.PEMInputOutput.java

License:LGPL

private static PrivateKey derivePrivateKeyPBES2(EncryptedPrivateKeyInfo eIn, AlgorithmIdentifier algId,
        char[] password) throws GeneralSecurityException, InvalidCipherTextException {
    PBES2Parameters pbeParams = PBES2Parameters.getInstance((ASN1Sequence) algId.getParameters());
    CipherParameters cipherParams = extractPBES2CipherParams(password, pbeParams);

    EncryptionScheme scheme = pbeParams.getEncryptionScheme();
    BufferedBlockCipher cipher;/*ww w. java 2  s . co  m*/
    if (scheme.getAlgorithm().equals(PKCSObjectIdentifiers.RC2_CBC)) {
        RC2CBCParameter rc2Params = RC2CBCParameter.getInstance(scheme);
        byte[] iv = rc2Params.getIV();
        CipherParameters param = new ParametersWithIV(cipherParams, iv);
        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new RC2Engine()));
        cipher.init(false, param);
    } else {
        byte[] iv = ((ASN1OctetString) scheme.getObject()).getOctets();
        // this version, done for BC 1.49 compat, caused #1238.
        //            byte[] iv = ASN1OctetString.getInstance(scheme).getOctets();
        CipherParameters param = new ParametersWithIV(cipherParams, iv);
        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()));
        cipher.init(false, param);
    }

    byte[] data = eIn.getEncryptedData();
    byte[] out = new byte[cipher.getOutputSize(data.length)];
    int len = cipher.processBytes(data, 0, data.length, out, 0);
    len += cipher.doFinal(out, len);
    byte[] pkcs8 = new byte[len];
    System.arraycopy(out, 0, pkcs8, 0, len);
    KeyFactory fact = KeyFactory.getInstance("RSA"); // It seems to work for both RSA and DSA.
    return fact.generatePrivate(new PKCS8EncodedKeySpec(pkcs8));
}

From source file:org.ScripterRon.BitcoinCore.EncryptedPrivateKey.java

License:Apache License

/**
 * Create a new EncryptedPrivateKey using the supplied private key and key phrase
 *
 * @param       privKey                 Private key
 * @param       keyPhrase               Phrase used to derive the encryption key
 * @throws      ECException             Unable to complete a cryptographic function
 *///from www.  j a  va2  s  .  com
public EncryptedPrivateKey(BigInteger privKey, String keyPhrase) throws ECException {
    //
    // Derive the AES encryption key
    //
    salt = new byte[KEY_LENGTH];
    secureRandom.nextBytes(salt);
    KeyParameter aesKey = deriveKey(keyPhrase, salt);
    //
    // Encrypt the private key using the generated AES key
    //
    try {
        iv = new byte[BLOCK_LENGTH];
        secureRandom.nextBytes(iv);
        ParametersWithIV keyWithIV = new ParametersWithIV(aesKey, iv);
        CBCBlockCipher blockCipher = new CBCBlockCipher(new AESFastEngine());
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher);
        cipher.init(true, keyWithIV);
        byte[] privKeyBytes = privKey.toByteArray();
        int encryptedLength = cipher.getOutputSize(privKeyBytes.length);
        encKeyBytes = new byte[encryptedLength];
        int length = cipher.processBytes(privKeyBytes, 0, privKeyBytes.length, encKeyBytes, 0);
        cipher.doFinal(encKeyBytes, length);
    } catch (Exception exc) {
        throw new ECException("Unable to encrypt the private key", exc);
    }
}

From source file:org.ScripterRon.BitcoinCore.EncryptedPrivateKey.java

License:Apache License

/**
 * Returns the decrypted private key//from  w ww.  jav  a 2  s  .co  m
 *
 * @param       keyPhrase       Key phrase used to derive the encryption key
 * @return                      Private key
 * @throws      ECException     Unable to complete a cryptographic function
 */
public BigInteger getPrivKey(String keyPhrase) throws ECException {
    KeyParameter aesKey = deriveKey(keyPhrase, salt);
    //
    // Decrypt the private key using the generated AES key
    //
    BigInteger privKey;
    try {
        ParametersWithIV keyWithIV = new ParametersWithIV(aesKey, iv);
        CBCBlockCipher blockCipher = new CBCBlockCipher(new AESFastEngine());
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher);
        cipher.init(false, keyWithIV);
        int bufferLength = cipher.getOutputSize(encKeyBytes.length);
        byte[] outputBytes = new byte[bufferLength];
        int length1 = cipher.processBytes(encKeyBytes, 0, encKeyBytes.length, outputBytes, 0);
        int length2 = cipher.doFinal(outputBytes, length1);
        int actualLength = length1 + length2;
        byte[] privKeyBytes = new byte[actualLength];
        System.arraycopy(outputBytes, 0, privKeyBytes, 0, actualLength);
        privKey = new BigInteger(privKeyBytes);
    } catch (Exception exc) {
        throw new ECException("Unable to decrypt the private key", exc);
    }
    return privKey;
}

From source file:org.sejda.sambox.encryption.ConcatenatingAESEngine.java

License:Apache License

ConcatenatingAESEngine() {
    super(new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())));
    random = new SecureRandom();
}

From source file:org.sejda.sambox.pdmodel.encryption.SecurityHandler.java

License:Apache License

/**
 * Encrypt or decrypt data with AES256.//w ww .  j  a  v  a  2 s . c o  m
 *
 * @param data The data to encrypt.
 * @param output The output to write the encrypted data to.
 *
 * @throws IOException If there is an error reading the data.
 */
private void decryptDataAES256(InputStream data, OutputStream output) throws IOException {
    byte[] iv = new byte[16];

    // read IV from stream
    int ivSize = data.read(iv);
    if (ivSize == -1) {
        return;
    }

    if (ivSize != iv.length) {
        throw new IOException("AES initialization vector not fully read: only " + ivSize
                + " bytes read instead of " + iv.length);
    }
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    cipher.init(false, new ParametersWithIV(new KeyParameter(encryptionKey), iv));
    try (CipherInputStream cis = new CipherInputStream(data, cipher)) {
        org.apache.commons.io.IOUtils.copy(cis, output);
    }
}