Example usage for org.bouncycastle.jce.provider BouncyCastleProvider BouncyCastleProvider

List of usage examples for org.bouncycastle.jce.provider BouncyCastleProvider BouncyCastleProvider

Introduction

In this page you can find the example usage for org.bouncycastle.jce.provider BouncyCastleProvider BouncyCastleProvider.

Prototype

public BouncyCastleProvider() 

Source Link

Document

Construct a new provider.

Usage

From source file:com.premiumminds.billy.france.util.KeyGenerator.java

License:Open Source License

/**
 * Generates the {@link PrivateKey} and {@link PublicKey} based on the
 * {@link PrivateKey} location.//from ww w  .  j  ava 2 s.co  m
 *
 * @param privateKeyPath
 */
public KeyGenerator(String privateKeyPath) {
    if (Security.getProvider("BC") == null) {
        Security.addProvider(new BouncyCastleProvider());
    }
    this.privateKeyPath = privateKeyPath;
}

From source file:com.projectswg.launchpad.service.PswgScanService.java

License:Open Source License

public PswgScanService(Manager manager) {
    this.manager = manager;
    file = null;

    Security.addProvider(new BouncyCastleProvider());
}

From source file:com.qut.middleware.crypto.impl.CryptoProcessorImpl.java

License:Apache License

public KeyStore generateKeyStore() throws CryptoException {
    try {/*  ww w.  ja  va  2  s. c  o m*/
        logger.debug("Generating a new key store.");

        /* Add BC to the jdk security manager to be able to use it as a provider */
        Security.addProvider(new BouncyCastleProvider());

        /* Create and init an empty key store */
        KeyStore keyStore = KeyStore.getInstance("JKS");
        keyStore.load(null, null);

        /*
         * Populate all new key stores with the key data of the local resolver, generally this is for metadata
         * purposes to ensure that all systems in the authentication network can correctly validate the signed
         * metadata document
         */
        X509Certificate localCertificate = (X509Certificate) localResolver.getLocalCertificate();
        Calendar before = new GregorianCalendar();
        Calendar expiry = new GregorianCalendar();
        before.setTime(localCertificate.getNotBefore());
        expiry.setTime(localCertificate.getNotAfter());

        addPublicKey(keyStore,
                new KeyPair(this.localResolver.getLocalPublicKey(), this.localResolver.getLocalPrivateKey()),
                this.localResolver.getLocalKeyAlias(), this.certIssuerDN, before, expiry);

        return keyStore;
    } catch (KeyStoreException e) {
        this.logger.error("KeyStoreException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        this.logger.error("NoSuchAlgorithmException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    } catch (CertificateException e) {
        this.logger.error("CertificateException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    } catch (IOException e) {
        this.logger.error("IOException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    }
}

From source file:com.qut.middleware.deployer.logic.CryptoProcessorESOEImpl.java

License:Apache License

@Override
public KeyStore generateKeyStore() throws CryptoException {
    try {//from www  .j  a  v  a2 s.c  o m
        logger.debug("Generating a new key store.");

        // Add BC to the jdk security manager to be able to use it as a provider
        Security.addProvider(new BouncyCastleProvider());

        // Create and init an empty key store
        KeyStore keyStore = KeyStore.getInstance("JKS");
        keyStore.load(null, null);

        return keyStore;
    } catch (KeyStoreException e) {
        this.logger.error("KeyStoreException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        this.logger.error("NoSuchAlgorithmException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    } catch (CertificateException e) {
        this.logger.error("CertificateException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    } catch (IOException e) {
        this.logger.error("IOException thrown, " + e.getLocalizedMessage());
        this.logger.debug(e.toString());
        throw new CryptoException(e.getLocalizedMessage(), e);
    }
}

From source file:com.raphfrk.bitcoin.bcnode.BCNode.java

License:Open Source License

public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException,
        UnknownHostException, IOException, InterruptedException {
    Security.addProvider(new BouncyCastleProvider());
    LogManager.init();/*from  w  w w  . j a  v a  2s  .co m*/

    BitcoinP2PManager manager = new BitcoinP2PManager(16);

    //manager.connect(new InetSocketAddress("bitseed.xf2.org", 8333), false);
    manager.connect(new InetSocketAddress("seed.bitcoin.sipa.be", 8333));
    //manager.connect(new InetSocketAddress("localhost", 8333), false);

    manager.start();

    System.in.read();

    manager.interrupt();

    manager.join();
}

From source file:com.raphfrk.bukkit.eventlink.SSLUtils.java

License:Open Source License

static boolean generateCertificateFile(File file, int keySize, String password, String algorithm,
        String certificateAlgorithm, String certificateName, boolean forceWrite) {

    KeyPair keyPair;// w ww  .j  a  v  a 2  s  .c  om
    X509Certificate cert;
    X509V3CertificateGenerator certGen = null;

    String providerName = "BC";

    if (Security.getProvider(providerName) == null) {
        Security.addProvider(new BouncyCastleProvider());
        if (Security.getProvider(providerName) == null) {
            EventLink.logger.log("Crypt libray (" + providerName + ") provider not installed");
            return false;
        }
    }

    try {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);

        synchronized (randomLock) {
            keyPairGenerator.initialize(keySize, random);
        }

        keyPair = KeyPairGenerator.getInstance(algorithm).generateKeyPair();

        certGen = new X509V3CertificateGenerator();

        certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
        certGen.setIssuerDN(new X500Principal(certificateName));
        certGen.setNotBefore(new Date(System.currentTimeMillis() - 10000));
        certGen.setNotAfter(new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000));
        certGen.setSubjectDN(new X500Principal(certificateName));
        certGen.setPublicKey(keyPair.getPublic());
        certGen.setSignatureAlgorithm(certificateAlgorithm);

        cert = certGen.generate(keyPair.getPrivate(), "BC");
    } catch (IllegalArgumentException iae) {
        EventLink.logger.log("Unable to find provider (BC)");
        iae.printStackTrace();
        if (certGen != null) {
            Iterator itr = certGen.getSignatureAlgNames();
            while (itr.hasNext()) {
                System.out.println("Algorithm: " + itr.next());
            }
        }
        return false;
    } catch (NoSuchProviderException nspe) {
        EventLink.logger.log("Unable to find provider (BC)");
        nspe.printStackTrace();
        return false;
    } catch (NoSuchAlgorithmException nsa) {
        EventLink.logger.log("Unable to implement algorithm (" + certificateAlgorithm + ")");
        if (certGen != null) {
            Iterator<String> itr = certGen.getSignatureAlgNames();
            while (itr.hasNext()) {
                String algName = itr.next();
                System.out.println("Algorithm: " + algName + " " + (algName.equals(certificateAlgorithm)));
            }
        }
        nsa.printStackTrace();
        return false;
    } catch (InvalidKeyException ike) {
        EventLink.logger.log("Unable to generate key");
        ike.printStackTrace();
        return false;
    } catch (SignatureException se) {
        EventLink.logger.log("Signature error");
        se.printStackTrace();
        return false;
    } catch (CertificateEncodingException cee) {
        EventLink.logger.log("Encoding error");
        cee.printStackTrace();
        return false;
    }

    return createKeyFile(file, password, keyPair, cert, forceWrite);

}

From source file:com.silverpeas.bootstrap.SilverpeasContextBootStrapper.java

License:Open Source License

/**
 * Initialise the System.properties according to Silverpeas needs and configuration.
 * @param sce//w w w  .ja v  a 2  s  . c  om
 */
@Override
public void contextInitialized(ServletContextEvent sce) {
    ResourceBundle silverpeasInitialisationSettings = FileUtil.loadBundle(
            "com.stratelia.silverpeas._silverpeasinitialize.settings._silverpeasinitializeSettings",
            new Locale("fr", ""));
    Security.addProvider(new BouncyCastleProvider());
    File pathInitialize = new File(silverpeasInitialisationSettings.getString("pathInitialize"));
    if (pathInitialize == null) {
        Logger.getLogger("bootstrap").log(Level.SEVERE,
                "Repository Initialize for systemSettings.properties file is not defined in Settings.");
    } else {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(pathInitialize, "systemSettings.properties"));
            Properties systemFileProperties = new Properties(System.getProperties());
            systemFileProperties.load(fis);

            // Fix - empty proxy port and proxy host not supported by Spring Social
            if (!StringUtil.isDefined(systemFileProperties.getProperty("http.proxyPort"))) {
                systemFileProperties.remove("http.proxyPort");
            }
            if (!StringUtil.isDefined(systemFileProperties.getProperty("http.proxyHost"))) {
                systemFileProperties.remove("http.proxyHost");
            }

            System.setProperties(systemFileProperties);
            if (isTrustoreConfigured()) {
                registerSSLSocketFactory();
            }
        } catch (FileNotFoundException e) {
            Logger.getLogger("bootstrap").log(Level.SEVERE,
                    "File systemSettings.properties in directory {0} not found.", pathInitialize);
        } catch (IOException e) {
            Logger.getLogger("bootstrap").log(Level.SEVERE, "Unable to read systemSettings.properties.");
        } catch (GeneralSecurityException e) {
            Logger.getLogger("bootstrap").log(Level.SEVERE, "Unable to configure the trustore.");
        } finally {
            IOUtils.closeQuietly(fis);
        }

    }
    springContextListener.contextInitialized(sce);
}

From source file:com.silverpeas.util.cryptage.SilverCryptKeysAsymetric.java

License:Open Source License

public SilverCryptKeysAsymetric(FileInputStream filep12, String pwd) {
    // CHARGEMENT DU FICHIER PKCS#12
    KeyStore ks = null;/* ww  w. j  av a2 s  .  c  o m*/
    char[] password = null;
    Security.addProvider(new BouncyCastleProvider());
    try {
        ks = KeyStore.getInstance("PKCS12");
        // Password pour le fichier filep12
        password = pwd.toCharArray();
        if (filep12 != null) {
            ks.load(filep12, password);
        } else {
            System.out.println("Erreur: fichier .p12" + "introuvable");
        }

    } catch (Exception e) {
        System.out.println(
                "Erreur: fichier .p12" + " n'est pas un fichier pkcs#12 valide ou passphrase incorrect");
        return;
    }

    // RECUPERATION DU COUPLE CLE PRIVEE/PUBLIQUE ET DU CERTIFICAT PUBLIQUE
    try {
        Enumeration<String> en = ks.aliases();
        String alias = "";
        List<String> vectaliases = new ArrayList<String>();

        while (en.hasMoreElements()) {
            vectaliases.add(en.nextElement());
        }
        String[] aliases = (vectaliases.toArray(new String[vectaliases.size()]));
        for (String aliase : aliases) {
            if (ks.isKeyEntry(aliase)) {
                alias = aliase;
                break;
            }
        }
        privatekey = (PrivateKey) ks.getKey(alias, password);
        cert = (X509Certificate) ks.getCertificate(alias);
        publickey = ks.getCertificate(alias).getPublicKey();
    } catch (Exception e) {
        SilverTrace.error("util", "SilverCryptKeysAsymetric.Error", "root.MSG_GEN_PARAM_VALUE", "In init", e);
        return;
    }
}

From source file:com.skplanet.jose.jwa.crypto.CryptoUtils.java

License:Open Source License

public static byte[] rsaDecrypt(Transformation transformation, byte[] encrypted, PrivateKey key)
        throws Exception {
    Cipher cipher = null;//from w  w  w  .j  a va 2 s  .c o m

    try {
        cipher = Cipher.getInstance(transformation.getValue());
    } catch (NoSuchAlgorithmException e) {
        if (Security.getProvider("BC") == null) {
            Security.addProvider(new BouncyCastleProvider());
        }

        cipher = Cipher.getInstance(transformation.getValue());
    }

    cipher.init(Cipher.DECRYPT_MODE, key);
    return cipher.doFinal(encrypted);
}

From source file:com.skplanet.jose.jwa.crypto.CryptoUtils.java

License:Open Source License

public static ECPrivateKey generateEcPrivateKey(byte[] d) throws Exception {
    ECPrivateKey privateKey = null;

    try {/*from w  w w. ja v  a2  s .c  o  m*/
        KeyFactory keyFactory = KeyFactory.getInstance("EC");
        privateKey = (ECPrivateKey) keyFactory.generatePrivate(new ECPrivateKeySpec(new BigInteger(d), P256));
    } catch (NoSuchAlgorithmException e) {
        if (Security.getProvider("BC") == null) {
            Security.addProvider(new BouncyCastleProvider());
        }

        KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
        privateKey = (ECPrivateKey) keyFactory.generatePrivate(new ECPrivateKeySpec(new BigInteger(d), P256));
    }

    return privateKey;
}