Search in sources :

Example 1 with DataLengthException

use of org.bouncycastle.crypto.DataLengthException in project robovm by robovm.

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;
}
Also used : DataLengthException(org.bouncycastle.crypto.DataLengthException) BigInteger(java.math.BigInteger)

Example 2 with DataLengthException

use of org.bouncycastle.crypto.DataLengthException in project XobotOS by xamarin.

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;
}
Also used : DataLengthException(org.bouncycastle.crypto.DataLengthException) BigInteger(java.math.BigInteger)

Example 3 with DataLengthException

use of org.bouncycastle.crypto.DataLengthException in project XobotOS by xamarin.

the class JCEBlockCipher method engineDoFinal.

protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, BadPaddingException, ShortBufferException {
    // BEGIN android-note
    // added ShortBufferException to the throws statement
    // END android-note
    int len = 0;
    // BEGIN android-added
    int outputLen = cipher.getOutputSize(inputLen);
    if (outputLen + outputOffset > output.length) {
        throw new ShortBufferException("need at least " + outputLen + " bytes");
    }
    if (inputLen != 0) {
        len = cipher.processBytes(input, inputOffset, inputLen, output, outputOffset);
    }
    try {
        return (len + cipher.doFinal(output, outputOffset + len));
    } catch (DataLengthException e) {
        throw new IllegalBlockSizeException(e.getMessage());
    } catch (InvalidCipherTextException e) {
        throw new BadPaddingException(e.getMessage());
    }
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) DataLengthException(org.bouncycastle.crypto.DataLengthException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException)

Example 4 with DataLengthException

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."));
}
Also used : DataLengthException(org.bouncycastle.crypto.DataLengthException) ConfigErrors(com.thoughtworks.go.domain.ConfigErrors) Test(org.junit.Test)

Example 5 with DataLengthException

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;
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) DataLengthException(org.bouncycastle.crypto.DataLengthException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException)

Aggregations

DataLengthException (org.bouncycastle.crypto.DataLengthException)8 BigInteger (java.math.BigInteger)3 BadPaddingException (javax.crypto.BadPaddingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 BlockCipher (org.bouncycastle.crypto.BlockCipher)2 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)2 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)2 ConfigErrors (com.thoughtworks.go.domain.ConfigErrors)1 ShortBufferException (javax.crypto.ShortBufferException)1 Test (org.junit.Test)1