Example usage for org.bouncycastle.crypto RuntimeCryptoException RuntimeCryptoException

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

Introduction

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

Prototype

public RuntimeCryptoException(String message) 

Source Link

Document

create a RuntimeCryptoException with the given message.

Usage

From source file:com.bitbreeds.webrtc.dtls.DTLSUtils.java

License:Open Source License

public static org.bouncycastle.crypto.tls.Certificate loadCert(String keystore, String alias, String password) {
    try {/*from   w w  w.  ja v  a2 s  .  c  o  m*/
        byte[] data = getCert(keystore, alias, password).getKey().getEncoded();
        org.bouncycastle.asn1.x509.Certificate[] cert = {
                org.bouncycastle.asn1.x509.Certificate.getInstance(data) };
        return new org.bouncycastle.crypto.tls.Certificate(cert);
    } catch (Exception e) {
        logger.error("Error loading certificate: ", e);
        throw new RuntimeCryptoException("Problem loading certificate");
    }
}

From source file:com.bitbreeds.webrtc.dtls.DTLSUtils.java

License:Open Source License

public static Pair<Certificate, KeyPair> getCert(String keystore, String alias, String password) {
    logger.info("Loading cert from {} with alias {}", keystore, alias);
    KeyStore ks = null;// ww w .j  a  v  a2 s .  c  o  m
    try {
        ks = KeyStore.getInstance("JKS");
        File fl = new File(keystore);
        FileInputStream stream = new FileInputStream(fl);
        ks.load(stream, password.toCharArray());
        final Key key = ks.getKey(alias, password.toCharArray());
        Certificate cert = ks.getCertificate(alias);
        KeyPair kp = new KeyPair(cert.getPublicKey(), (PrivateKey) key);
        return new Pair<>(ks.getCertificate(alias), kp);
    } catch (Exception e) {
        logger.error("Error loading certificate: ", e);
        throw new RuntimeCryptoException("Problem loading certificate");
    }
}

From source file:com.msopentech.thali.CouchDBListener.ThaliListener.java

License:Open Source License

/**
 * Waits until the local CouchDB server is up and running
 * @throws InterruptedException/*from  w w  w. ja va  2  s .  co  m*/
 */
public void waitTillListenerStarts() throws InterruptedException {
    while (cblListener == null && serverStarted) {
        Log.v(CblLogTags.TAG_THALI_LISTENER, "Waiting for Listener to start");
        Thread.sleep(100);
    }

    if (serverStarted == false) {
        throw new RuntimeCryptoException("server wasn't started or was stopped.");
    }
}

From source file:com.rapidminer.cryptography.hashing.DigesterProvider.java

License:Open Source License

private String digest(byte[] bytes, DigesterConfig config) throws JEPFunctionException {

    // digest input array
    byte[] digested;
    try {//  w ww .jav a  2s  .  c om
        digested = getDigester(config).digest(bytes);
    } catch (EncryptionOperationNotPossibleException e) {
        throw new JEPFunctionException(
                "Error while calculating hash. Check if your system does support the selected hash algorithm.");
    }

    // base64 encode digested array and return result
    try {
        return new String(BASE64.encode(digested), HASH_CHARSET);
    } catch (UnsupportedEncodingException e) {
        // cannot happen
        throw new RuntimeCryptoException(e.getLocalizedMessage());
    }
}

From source file:com.rapidminer.cryptography.hashing.DigesterProvider.java

License:Open Source License

/**
 * Checks wether the provided value matches the base64 encoded hash value.
 * This has to be done with the same settings that have been used to
 * calculcate the base64 encoded value./*from   www . j a  v  a 2  s  .co  m*/
 */
protected boolean matches(Object value, String base64Hash, DigesterConfig config) throws JEPFunctionException {

    try {
        // convert input to byte array
        byte[] bytes = getBytes(value);

        // base64 sanity check
        byte[] hashBytes = base64Hash.getBytes(HASH_CHARSET);
        if (!Base64.isArrayByteBase64(hashBytes)) {
            throw new JEPFunctionException(
                    "Hash value is not base64 encoded. Value and hash arguments switched? (First value than hash)");
        }

        // decode hash value to bytes
        hashBytes = BASE64.decode(hashBytes);

        PooledByteDigester digester = getDigester(config);
        return digester.matches(bytes, hashBytes);
    } catch (EncryptionOperationNotPossibleException e) {
        throw new JEPFunctionException(
                "Error while hash is being matched. Check if second argument truely is a base64 encoded hash "
                        + " and if your system does support the selected hash algorithm.");
    } catch (UnsupportedEncodingException e) {
        // cannot happen
        throw new RuntimeCryptoException(e.getLocalizedMessage());
    }
}

From source file:esteidhacker.FakeEstEID.java

License:Open Source License

public void make_sample_card(FakeEstEIDCA ca, boolean check) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME);
    keyGen.initialize(2048);/*from   w w  w .ja v  a 2  s. c o  m*/
    // Generate keys
    KeyPair auth = keyGen.generateKeyPair();
    KeyPair sign = keyGen.generateKeyPair();
    X509Certificate authcert = ca.generateUserCertificate((RSAPublicKey) auth.getPublic(), false, "SIILIPOISS",
            "UDUS", "10101010005", "kalevipoeg@soome.fi");
    X509Certificate signcert = ca.generateUserCertificate((RSAPublicKey) sign.getPublic(), true, "SIILIPOISS",
            "UDUS", "10101010005", "kalevipoeg@soome.fi");
    if (check) {
        // Verify softkeys
        if (!verifyKeypairIntegrity((RSAPrivateCrtKey) auth.getPrivate(),
                (RSAPublicKey) authcert.getPublicKey())) {
            throw new RuntimeCryptoException("Cert and key mismatch");
        }
        if (!verifyKeypairIntegrity((RSAPrivateCrtKey) sign.getPrivate(),
                (RSAPublicKey) signcert.getPublicKey())) {
            throw new RuntimeCryptoException("Cert and key mismatch");
        }
    }
    send_key((RSAPrivateCrtKey) auth.getPrivate(), 1);
    send_key((RSAPrivateCrtKey) sign.getPrivate(), 2);
    send_cert(authcert.getEncoded(), 1);
    send_cert(signcert.getEncoded(), 2);

    CommandAPDU cmd = null;
    ResponseAPDU resp = null;
    if (check) {
        // Verify on-card keys.
        Cipher verify_cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        SecureRandom r = SecureRandom.getInstance("SHA1PRNG");
        byte[] rnd = new byte[8];

        r.nextBytes(rnd);
        cmd = new CommandAPDU(0x00, 0x88, 0x00, 0x00, rnd, 256);
        resp = channel.transmit(cmd);
        check(resp);
        verify_cipher.init(Cipher.DECRYPT_MODE, authcert.getPublicKey());
        byte[] result = verify_cipher.doFinal(resp.getData());
        if (!java.util.Arrays.equals(rnd, result)) {
            throw new RuntimeCryptoException("Card and auth key don't match!");
        }

        r.nextBytes(rnd);
        cmd = new CommandAPDU(0x00, 0x2A, 0x9E, 0x9A, rnd, 256);
        resp = channel.transmit(cmd);
        check(resp);
        verify_cipher.init(Cipher.DECRYPT_MODE, signcert.getPublicKey());
        result = verify_cipher.doFinal(resp.getData());
        if (!java.util.Arrays.equals(rnd, result)) {
            throw new RuntimeCryptoException("Card and sign key don't match!");
        }
    }
    // Dump default data file
    for (int i = 0; i < defaultDataFile.length; i++) {
        cmd = new CommandAPDU(0x80, 0x04, i + 1, 0x00, defaultDataFile[i].toUpperCase().getBytes("ISO8859-15"));
        resp = channel.transmit(cmd);
        check(resp);
    }
}

From source file:org.pwsafe.lib.file.AbstractPwsPasswdField.java

License:Open Source License

@Override
public String toString() {
    SealedObject sealValue = (SealedObject) getValue();
    try {//  www .j  a va 2 s . com
        if (sealValue == null) {
            return "";
        } else {
            return (String) sealValue.getObject(itsReadCipher);
        }
    } catch (IllegalBlockSizeException | BadPaddingException | ClassNotFoundException e) {
        throw new RuntimeCryptoException(e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.pwsafe.lib.file.AbstractPwsPasswdField.java

License:Open Source License

private static SealedObject sealValue(String value, Cipher cipher) {
    try {/*from   www . j a va  2  s.c o m*/
        return new SealedObject(value, cipher);
    } catch (IllegalBlockSizeException | IOException e) {
        throw new RuntimeCryptoException(e.getMessage());
    }
}

From source file:org.pwsafe.lib.file.PwsFile.java

License:Open Source License

/**
 * Returns the passphrase used to open the file.
 *
 * @return The file's passphrase./*from  ww  w.j  a  va2  s  . c o m*/
 */
public Owner<PwsPassword> getPassphrase() {
    try {
        return new Owner<>(PwsPassword.unseal(passphrase, getReadCipher()));
    } catch (IllegalBlockSizeException | BadPaddingException | ClassNotFoundException | IOException e) {
        throw new RuntimeCryptoException(e.getMessage());
    }
}

From source file:org.pwsafe.lib.file.PwsFile.java

License:Open Source License

/**
 * Sets the passphrase that will be used to encrypt the file when it is
 * saved.//  w w w .j a  va  2 s.co  m
 */
public void setPassphrase(Owner<PwsPassword>.Param passwdParam) {
    Owner<PwsPassword> passwd = passwdParam.use();
    try {
        passphrase = passwd.get().seal(getWriteCipher());
    } catch (IllegalBlockSizeException | IOException e) {
        throw new RuntimeCryptoException(e.getMessage());
    } finally {
        passwd.close();
    }
}