Example usage for org.bouncycastle.crypto InvalidCipherTextException InvalidCipherTextException

List of usage examples for org.bouncycastle.crypto InvalidCipherTextException InvalidCipherTextException

Introduction

In this page you can find the example usage for org.bouncycastle.crypto InvalidCipherTextException InvalidCipherTextException.

Prototype

public InvalidCipherTextException(String message) 

Source Link

Document

create a InvalidCipherTextException with the given message.

Usage

From source file:com.DSC.crypto.Cipher.java

License:Open Source License

/**
 * /*  w  w w. j  av a2 s  .co m*/
 * @param passphrase
 * @param HMAC
 * @param data
 * @return
 * @throws InvalidCipherTextException
 */
public static boolean verifyHMAC(String passphrase, BigInteger[] HMAC, byte[] data)
        throws InvalidCipherTextException {
    HMac hmac = new HMac(new MD5Digest());
    byte[] expHMAC = new byte[hmac.getMacSize()];
    byte[] recHMAC = new byte[hmac.getMacSize()];

    /* Initializes and generate the expected HMAC for message */
    hmac.init(new KeyParameter(passphrase.getBytes()));
    hmac.update(data, 0, data.length);
    hmac.doFinal(expHMAC, 0);

    /* Convert the received HMAC to a byte representation */
    recHMAC = HMAC[0].toByteArray();

    /* Compare the HMAC received to the expected HMAC */
    if (Arrays.equals(expHMAC, recHMAC)) {
        return true;
    } else {
        throw new InvalidCipherTextException("Message HMAC failed!");
    }
}

From source file:com.thoughtworks.go.config.materials.tfs.TfsMaterialConfigUpdateTest.java

License:Apache License

@Test
public void shouldErrorOutIfDecryptionFails() throws InvalidCipherTextException {
    GoCipher mockGoCipher = mock(GoCipher.class);
    String fakeCipherText = "fake cipher text";
    when(mockGoCipher.decrypt(fakeCipherText)).thenThrow(new InvalidCipherTextException("exception"));
    TfsMaterialConfig materialConfig = new TfsMaterialConfig(mockGoCipher,
            new UrlArgument("http://10.4.4.101:8080/tfs/Sample"), "loser", "CORPORATE", "passwd",
            "walk_this_path");
    ReflectionUtil.setField(materialConfig, "encryptedPassword", fakeCipherText);
    try {//from   w w w .  ja  v  a 2 s.  c o m
        materialConfig.getPassword();
        fail("Should have thrown up");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("Could not decrypt the password to get the real password"));
    }
}

From source file:com.thoughtworks.go.config.materials.tfs.TfsMaterialConfigUpdateTest.java

License:Apache License

@Test
public void shouldErrorOutIfEncryptionFails() throws Exception {
    GoCipher mockGoCipher = mock(GoCipher.class);
    when(mockGoCipher.encrypt("password")).thenThrow(new InvalidCipherTextException("exception"));
    try {/*from w w w  .  ja v  a  2 s . c  o m*/
        new TfsMaterialConfig(mockGoCipher, new UrlArgument("http://10.4.4.101:8080/tfs/Sample"), "loser",
                "CORPORATE", "password", "walk_this_path");
        fail("Should have thrown up");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("Password encryption failed. Please verify your cipher key."));
    }
}

From source file:com.thoughtworks.go.config.materials.tfs.TfsMaterialTest.java

License:Apache License

@Test
public void shouldErrorOutIfDecryptionFails() throws InvalidCipherTextException {
    GoCipher mockGoCipher = mock(GoCipher.class);
    String fakeCipherText = "fake cipher text";
    when(mockGoCipher.decrypt(fakeCipherText)).thenThrow(new InvalidCipherTextException("exception"));
    TfsMaterial material = new TfsMaterial(mockGoCipher, new UrlArgument("/foo"), "username", DOMAIN,
            "password", "");
    ReflectionUtil.setField(material, "encryptedPassword", fakeCipherText);
    try {//from  w w  w  . j a va  2s.  co  m
        material.getPassword();
        fail("Should have thrown up");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("Could not decrypt the password to get the real password"));
    }
}

From source file:com.thoughtworks.go.config.materials.tfs.TfsMaterialTest.java

License:Apache License

@Test
public void shouldErrorOutIfEncryptionFails() throws Exception {
    GoCipher mockGoCipher = mock(GoCipher.class);
    when(mockGoCipher.encrypt("password")).thenThrow(new InvalidCipherTextException("exception"));
    try {//ww  w.  ja v a 2  s.  c  o  m
        new TfsMaterial(mockGoCipher, new UrlArgument("/foo"), "username", DOMAIN, "password", "");
        fail("Should have thrown up");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("Password encryption failed. Please verify your cipher key."));
    }
}

From source file:com.thoughtworks.go.domain.materials.svn.SvnMaterialTest.java

License:Apache License

@Test
public void shouldErrorOutIfDecryptionFails() throws InvalidCipherTextException {
    GoCipher mockGoCipher = mock(GoCipher.class);
    String fakeCipherText = "fake cipher text";
    when(mockGoCipher.decrypt(fakeCipherText)).thenThrow(new InvalidCipherTextException("exception"));
    SvnMaterial material = new SvnMaterial("/foo", "username", null, false, mockGoCipher);
    ReflectionUtil.setField(material, "encryptedPassword", fakeCipherText);
    try {/*from  w w w.j  a  v a2  s.  c o m*/
        material.getPassword();
        fail("Should have thrown up");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("Could not decrypt the password to get the real password"));
    }
}

From source file:com.thoughtworks.go.domain.materials.svn.SvnMaterialTest.java

License:Apache License

@Test
public void shouldErrorOutIfEncryptionFails() throws Exception {
    GoCipher mockGoCipher = mock(GoCipher.class);
    when(mockGoCipher.encrypt("password")).thenThrow(new InvalidCipherTextException("exception"));
    try {// ww  w.j  a v a2 s. c  o m
        new SvnMaterial("/foo", "username", "password", false, mockGoCipher);
        fail("Should have thrown up");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("Password encryption failed. Please verify your cipher key."));
    }
}

From source file:dbn.crypto.Crypto.java

public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] key) {
    try {//www  .  j a v  a 2  s .c  o  m
        if (ivCiphertext.length < 16 || ivCiphertext.length % 16 != 0) {
            throw new InvalidCipherTextException("invalid ivCiphertext length");
        }
        byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16);
        byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(false, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(ciphertext.length)];
        int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0);
        plaintextLength += aes.doFinal(output, plaintextLength);
        byte[] result = new byte[plaintextLength];
        System.arraycopy(output, 0, result, 0, result.length);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:dbn.crypto.Crypto.java

public static byte[] aesGCMDecrypt(byte[] ivCiphertext, byte[] key) {
    try {//w ww .  j a  va 2 s . c om
        if (ivCiphertext.length < 16) {
            throw new InvalidCipherTextException("invalid ivCiphertext length");
        }
        byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16);
        byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length);
        GCMBlockCipher aes = new GCMBlockCipher(new AESEngine());
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(false, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(ciphertext.length)];
        int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0);
        plaintextLength += aes.doFinal(output, plaintextLength);
        byte[] result = new byte[plaintextLength];
        System.arraycopy(output, 0, result, 0, result.length);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:freemail.RTSFetcher.java

License:Open Source License

private byte[] decrypt_rts(File rtsmessage) throws IOException, InvalidCipherTextException {
    // initialise our ciphers
    RSAKeyParameters ourprivkey = AccountManager.getPrivateKey(account.getProps());
    AsymmetricBlockCipher deccipher = new RSAEngine();
    deccipher.init(false, ourprivkey);//from   w  w  w  .  j av  a  2s .c  om

    PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new PKCS7Padding());

    // first n bytes will be an encrypted RSA block containting the
    // AES IV and Key. Read that.
    byte[] encrypted_params = new byte[deccipher.getInputBlockSize()];
    FileInputStream fis = new FileInputStream(rtsmessage);
    int read = 0;

    while (read < encrypted_params.length) {
        read += fis.read(encrypted_params, read, encrypted_params.length - read);
        if (read < 0)
            break;
    }

    if (read < 0) {
        throw new InvalidCipherTextException("RTS Message too short");
    }

    byte[] aes_iv_and_key = deccipher.processBlock(encrypted_params, 0, encrypted_params.length);

    KeyParameter kp = new KeyParameter(aes_iv_and_key, aescipher.getBlockSize(),
            aes_iv_and_key.length - aescipher.getBlockSize());
    ParametersWithIV kpiv = new ParametersWithIV(kp, aes_iv_and_key, 0, aescipher.getBlockSize());
    try {
        aescipher.init(false, kpiv);
    } catch (IllegalArgumentException iae) {
        throw new InvalidCipherTextException(iae.getMessage());
    }

    byte[] plaintext = new byte[aescipher.getOutputSize((int) rtsmessage.length() - read)];

    int ptbytes = 0;
    while (read < rtsmessage.length()) {
        byte[] buf = new byte[(int) rtsmessage.length() - read];

        int thisread = fis.read(buf, 0, (int) rtsmessage.length() - read);
        ptbytes += aescipher.processBytes(buf, 0, thisread, plaintext, ptbytes);
        read += thisread;
    }

    fis.close();

    try {
        aescipher.doFinal(plaintext, ptbytes);
    } catch (DataLengthException dle) {
        throw new InvalidCipherTextException(dle.getMessage());
    }

    return plaintext;
}