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);
}
}
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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations