use of org.bouncycastle.crypto.DataLengthException in project XobotOS by xamarin.
the class CTSBlockCipher method doFinal.
/**
* Process the last block in the buffer.
*
* @param out the array the block currently being held is copied into.
* @param outOff the offset at which the copying starts.
* @return the number of output bytes copied to out.
* @exception DataLengthException if there is insufficient space in out for
* the output.
* @exception IllegalStateException if the underlying cipher is not
* initialised.
* @exception InvalidCipherTextException if cipher text decrypts wrongly (in
* case the exception will never get thrown).
*/
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException, InvalidCipherTextException {
if (bufOff + outOff > out.length) {
throw new DataLengthException("output buffer to small in doFinal");
}
int blockSize = cipher.getBlockSize();
int len = bufOff - blockSize;
byte[] block = new byte[blockSize];
if (forEncryption) {
cipher.processBlock(buf, 0, block, 0);
if (bufOff < blockSize) {
throw new DataLengthException("need at least one block of input for CTS");
}
for (int i = bufOff; i != buf.length; i++) {
buf[i] = block[i - blockSize];
}
for (int i = blockSize; i != bufOff; i++) {
buf[i] ^= block[i - blockSize];
}
if (cipher instanceof CBCBlockCipher) {
BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
c.processBlock(buf, blockSize, out, outOff);
} else {
cipher.processBlock(buf, blockSize, out, outOff);
}
System.arraycopy(block, 0, out, outOff + blockSize, len);
} else {
byte[] lastBlock = new byte[blockSize];
if (cipher instanceof CBCBlockCipher) {
BlockCipher c = ((CBCBlockCipher) cipher).getUnderlyingCipher();
c.processBlock(buf, 0, block, 0);
} else {
cipher.processBlock(buf, 0, block, 0);
}
for (int i = blockSize; i != bufOff; i++) {
lastBlock[i - blockSize] = (byte) (block[i - blockSize] ^ buf[i]);
}
System.arraycopy(buf, blockSize, block, 0, len);
cipher.processBlock(block, 0, out, outOff);
System.arraycopy(lastBlock, 0, out, outOff + blockSize, len);
}
int offset = bufOff;
reset();
return offset;
}
use of org.bouncycastle.crypto.DataLengthException in project 360-Engine-for-Android by 360.
the class RSACoreEngine 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.bouncycastle.crypto.DataLengthException in project robovm by robovm.
the class BaseBlockCipher method engineDoFinal.
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
int len = 0;
byte[] tmp = new byte[engineGetOutputSize(inputLen)];
if (inputLen != 0) {
len = cipher.processBytes(input, inputOffset, inputLen, tmp, 0);
}
try {
len += cipher.doFinal(tmp, len);
} catch (DataLengthException e) {
throw new IllegalBlockSizeException(e.getMessage());
} catch (InvalidCipherTextException e) {
throw new BadPaddingException(e.getMessage());
}
if (len == tmp.length) {
return tmp;
}
byte[] out = new byte[len];
System.arraycopy(tmp, 0, out, 0, len);
return out;
}
use of org.bouncycastle.crypto.DataLengthException in project gocd by gocd.
the class EnvironmentVariableConfigTest method shouldErrorOutOnValidateWhenEncryptedValueIsForceChanged.
@Test
public void shouldErrorOutOnValidateWhenEncryptedValueIsForceChanged() throws InvalidCipherTextException {
String plainText = "secure_value";
String cipherText = "cipherText";
when(goCipher.encrypt(plainText)).thenReturn(cipherText);
when(goCipher.decrypt(cipherText)).thenThrow(new DataLengthException("last block incomplete in decryption"));
EnvironmentVariableConfig environmentVariableConfig = new EnvironmentVariableConfig(goCipher, "secure_key", plainText, true);
environmentVariableConfig.validate(null);
ConfigErrors error = environmentVariableConfig.errors();
assertThat(error.isEmpty(), is(false));
assertThat(error.on(EnvironmentVariableConfig.VALUE), is("Encrypted value for variable named 'secure_key' is invalid. This usually happens when the cipher text is modified to have an invalid value."));
}
use of org.bouncycastle.crypto.DataLengthException in project inbot-utils by Inbot.
the class AESUtils method encryptBouncyCastle.
private static String encryptBouncyCastle(SecretKey secret, String plainText) {
try {
// prepending with md5 hash allows us to do an integrity check on decrypt to prevent returning garbage if the decrypt key is incorrect
String md5 = HashUtils.md5(plainText);
plainText = md5 + plainText;
// the iv acts as a per use salt, this ensures things encrypted with the same key always have a unique salt
// 128 bit iv because NIST AES is standardized with 128 bit blocks and iv needs to match block size, even when using 256 bit key
byte[] iv = new byte[16];
SECURE_RANDOM.nextBytes(iv);
// setup cipher parameters with key and IV
byte[] key = secret.getEncoded();
// setup AES cipher in CBC mode with PKCS7 padding
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
cipher.reset();
cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
byte[] plainTextBuf = plainText.getBytes(StandardCharsets.UTF_8);
byte[] buf = new byte[cipher.getOutputSize(plainTextBuf.length)];
int len = cipher.processBytes(plainTextBuf, 0, plainTextBuf.length, buf, 0);
len += cipher.doFinal(buf, len);
// copy the encrypted part of the buffer to out
byte[] out = new byte[len];
System.arraycopy(buf, 0, out, 0, len);
// iv$encrypted
return byteArrayToHexString(iv) + "$" + new String(Base64.encodeBase64URLSafe(out), StandardCharsets.UTF_8);
} catch (DataLengthException | InvalidCipherTextException e) {
throw new IllegalStateException("cannot encrypt", e);
}
}
Aggregations