Search in sources :

Example 1 with PaddedBufferedBlockCipher

use of org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher in project rskj by rsksmart.

the class KeyCrypterAes method encrypt.

/**
 * Password based encryption using AES - CBC 256 bits.
 */
@Override
public EncryptedData encrypt(byte[] plainBytes, KeyParameter key) {
    checkNotNull(plainBytes);
    checkNotNull(key);
    try {
        // Generate iv - each encryption call has a different iv.
        byte[] iv = new byte[BLOCK_LENGTH];
        secureRandom.nextBytes(iv);
        ParametersWithIV keyWithIv = new ParametersWithIV(key, iv);
        // Encrypt using AES.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(true, keyWithIv);
        byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
        final int length1 = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
        final int length2 = cipher.doFinal(encryptedBytes, length1);
        return new EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2));
    } catch (Exception e) {
        throw new KeyCrypterException("Could not encrypt bytes.", e);
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine)

Example 2 with PaddedBufferedBlockCipher

use of org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher in project bitcoin-wallet by bitcoin-wallet.

the class Crypto method encryptRaw.

/**
 * Password based encryption using AES - CBC 256 bits.
 *
 * @param plainBytes
 *            The bytes to encrypt
 * @param password
 *            The password to use for encryption
 * @return SALT_LENGTH bytes of salt followed by the encrypted bytes.
 * @throws IOException
 */
private static byte[] encryptRaw(final byte[] plainTextAsBytes, final char[] password) throws IOException {
    try {
        // Generate salt - each encryption call has a different salt.
        final byte[] salt = new byte[SALT_LENGTH];
        secureRandom.nextBytes(salt);
        final ParametersWithIV key = (ParametersWithIV) getAESPasswordKey(password, salt);
        // The following code uses an AES cipher to encrypt the message.
        final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(true, key);
        final byte[] encryptedBytes = new byte[cipher.getOutputSize(plainTextAsBytes.length)];
        final int processLen = cipher.processBytes(plainTextAsBytes, 0, plainTextAsBytes.length, encryptedBytes, 0);
        final int doFinalLen = cipher.doFinal(encryptedBytes, processLen);
        // The result bytes are the SALT_LENGTH bytes followed by the encrypted bytes.
        return concat(salt, Arrays.copyOf(encryptedBytes, processLen + doFinalLen));
    } catch (final InvalidCipherTextException | DataLengthException x) {
        throw new IOException("Could not encrypt bytes", x);
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) DataLengthException(org.spongycastle.crypto.DataLengthException) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) IOException(java.io.IOException)

Example 3 with PaddedBufferedBlockCipher

use of org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher in project jmulticard by ctt-gob-es.

the class AmAESCrypto method initCiphers.

private void initCiphers(final byte[] key, final byte[] iv) {
    // get the keyBytes
    this.keyBytes = new byte[key.length];
    System.arraycopy(key, 0, this.keyBytes, 0, key.length);
    this.keyP = new KeyParameter(this.keyBytes);
    // get the IV
    this.IV = new byte[BLOCK_SIZE];
    System.arraycopy(iv, 0, this.IV, 0, this.IV.length);
    // create the ciphers
    // AES block cipher in CBC mode with ISO7816d4 padding
    this.encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new ISO7816d4Padding());
    this.decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new ISO7816d4Padding());
    // create the IV parameter
    final ParametersWithIV parameterIV = new ParametersWithIV(this.keyP, this.IV);
    this.encryptCipher.init(true, parameterIV);
    this.decryptCipher.init(false, parameterIV);
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.spongycastle.crypto.engines.AESEngine) ISO7816d4Padding(org.spongycastle.crypto.paddings.ISO7816d4Padding) KeyParameter(org.spongycastle.crypto.params.KeyParameter) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher)

Example 4 with PaddedBufferedBlockCipher

use of org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher in project bitcoin-wallet by bitcoin-wallet.

the class Crypto method decryptRaw.

/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param bytesToDecode
 *            The bytes to decrypt
 * @param passwordbThe
 *            password to use for decryption
 * @return The decrypted bytes
 * @throws IOException
 */
private static byte[] decryptRaw(final byte[] bytesToDecode, final char[] password) throws IOException {
    try {
        // separate the salt and bytes to decrypt
        final byte[] salt = new byte[SALT_LENGTH];
        System.arraycopy(bytesToDecode, 0, salt, 0, SALT_LENGTH);
        final byte[] cipherBytes = new byte[bytesToDecode.length - SALT_LENGTH];
        System.arraycopy(bytesToDecode, SALT_LENGTH, cipherBytes, 0, bytesToDecode.length - SALT_LENGTH);
        final ParametersWithIV key = (ParametersWithIV) getAESPasswordKey(password, salt);
        // decrypt the message
        final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, key);
        final byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        final int processLen = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        final int doFinalLen = cipher.doFinal(decryptedBytes, processLen);
        return Arrays.copyOf(decryptedBytes, processLen + doFinalLen);
    } catch (final InvalidCipherTextException | DataLengthException x) {
        throw new IOException("Could not decrypt bytes", x);
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) DataLengthException(org.spongycastle.crypto.DataLengthException) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) IOException(java.io.IOException)

Example 5 with PaddedBufferedBlockCipher

use of org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher in project nuls by nuls-io.

the class AESEncrypt method encrypt.

/**
 * 加密
 *
 * @param plainBytes
 * @param iv
 * @param aesKey
 * @return EncryptedData
 */
public static EncryptedData encrypt(byte[] plainBytes, byte[] iv, KeyParameter aesKey) throws NulsRuntimeException {
    Utils.checkNotNull(plainBytes);
    Utils.checkNotNull(aesKey);
    try {
        if (iv == null) {
            iv = new byte[16];
            SECURE_RANDOM.nextBytes(iv);
        }
        ParametersWithIV keyWithIv = new ParametersWithIV(aesKey, iv);
        // Encrypt using AES.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(true, keyWithIv);
        byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
        final int length1 = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
        final int length2 = cipher.doFinal(encryptedBytes, length1);
        return new EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2));
    } catch (Exception e) {
        throw new NulsRuntimeException(e);
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Aggregations

CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)7 PaddedBufferedBlockCipher (org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher)7 ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)7 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)6 AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)6 KeyParameter (org.spongycastle.crypto.params.KeyParameter)3 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)2 IOException (java.io.IOException)2 DataLengthException (org.spongycastle.crypto.DataLengthException)2 InvalidCipherTextException (org.spongycastle.crypto.InvalidCipherTextException)2 AESEngine (org.spongycastle.crypto.engines.AESEngine)1 ISO7816d4Padding (org.spongycastle.crypto.paddings.ISO7816d4Padding)1