Search in sources :

Example 1 with DataLengthException

use of org.spongycastle.crypto.DataLengthException 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 2 with DataLengthException

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

the class AmCryptoProvider method decrypt.

/**
 * Entschlasselt das abergebene ByteArray mit den Parametern die beim @see
 * #init(byte[], long) eingestellt wurden.
 * @param in BytrArray mit den verschlasselten Daten
 * @return ByteArray mit den entschlasselten Daten
 * @throws AmCryptoException On any error.
 */
public final byte[] decrypt(final byte[] in) throws AmCryptoException {
    // number of bytes read from input
    int noBytesRead = 0;
    // number of bytes processed
    int noBytesProcessed = 0;
    try (final ByteArrayInputStream bin = new ByteArrayInputStream(in);
        final ByteArrayOutputStream bout = new ByteArrayOutputStream()) {
        try {
            while ((noBytesRead = bin.read(this.buf)) >= 0) {
                noBytesProcessed = this.decryptCipher.processBytes(this.buf, 0, noBytesRead, this.obuf, 0);
                bout.write(this.obuf, 0, noBytesProcessed);
            }
        } catch (final DataLengthException | IllegalStateException | IOException e) {
            throw new AmCryptoException(e);
        }
        try {
            noBytesProcessed = this.decryptCipher.doFinal(this.obuf, 0);
            bout.write(this.obuf, 0, noBytesProcessed);
            bout.flush();
            return bout.toByteArray();
        } catch (final DataLengthException | IllegalStateException | InvalidCipherTextException | IOException e) {
            throw new AmCryptoException(e);
        }
    } catch (final IOException ioe) {
        throw new AmCryptoException(ioe);
    }
}
Also used : InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) ByteArrayInputStream(java.io.ByteArrayInputStream) DataLengthException(org.spongycastle.crypto.DataLengthException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 3 with DataLengthException

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

the class AmCryptoProvider method encrypt.

/**
 * Verschlasselt das abergebene ByteArray mit den Parametern die beim @see
 * #init(byte[], long) eingestellt wurden.
 * @param in ByteArray mit den zu verschlasselnden Daten
 * @return ByteArray mit den entschlasselten Daten.
 * @throws AmCryptoException On any error.
 */
public final byte[] encrypt(final byte[] in) throws AmCryptoException {
    // number of bytes read from input
    int noBytesRead = 0;
    // number of bytes processed
    int noBytesProcessed = 0;
    try (final ByteArrayInputStream bin = new ByteArrayInputStream(in);
        final ByteArrayOutputStream bout = new ByteArrayOutputStream()) {
        try {
            while ((noBytesRead = bin.read(this.buf)) >= 0) {
                noBytesProcessed = this.encryptCipher.processBytes(this.buf, 0, noBytesRead, this.obuf, 0);
                bout.write(this.obuf, 0, noBytesProcessed);
            }
        } catch (final DataLengthException | IllegalStateException | IOException e) {
            throw new AmCryptoException(e);
        }
        try {
            noBytesProcessed = this.encryptCipher.doFinal(this.obuf, 0);
            bout.write(this.obuf, 0, noBytesProcessed);
            bout.flush();
            return bout.toByteArray();
        } catch (final DataLengthException | IllegalStateException | InvalidCipherTextException | IOException e) {
            throw new AmCryptoException(e);
        }
    } catch (final IOException ioe) {
        throw new AmCryptoException(ioe);
    }
}
Also used : InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) ByteArrayInputStream(java.io.ByteArrayInputStream) DataLengthException(org.spongycastle.crypto.DataLengthException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 4 with DataLengthException

use of org.spongycastle.crypto.DataLengthException in project universa by UniversaBlockchain.

the class NativeRSACoreEngine method convertInput.

public BigInteger convertInput(byte[] in, int inOff, int inLen) {
    if (inLen > (getInputBlockSize() + 1)) {
        throw new DataLengthException("input too large for RSA cipher.");
    } else if (inLen == (getInputBlockSize() + 1) && !forEncryption) {
        throw new DataLengthException("input too large for RSA cipher.");
    }
    byte[] block;
    if (inOff != 0 || inLen != in.length) {
        block = new byte[inLen];
        System.arraycopy(in, inOff, block, 0, inLen);
    } else {
        block = in;
    }
    BigInteger res = new BigInteger(1, block);
    if (res.compareTo(key.getModulus()) >= 0) {
        throw new DataLengthException("input too large for RSA cipher.");
    }
    return res;
}
Also used : DataLengthException(org.spongycastle.crypto.DataLengthException) BigInteger(java.math.BigInteger)

Example 5 with DataLengthException

use of org.spongycastle.crypto.DataLengthException 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)

Aggregations

DataLengthException (org.spongycastle.crypto.DataLengthException)5 IOException (java.io.IOException)4 InvalidCipherTextException (org.spongycastle.crypto.InvalidCipherTextException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)2 AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)2 CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)2 PaddedBufferedBlockCipher (org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher)2 ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)2 BigInteger (java.math.BigInteger)1