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.itextpdf.text.pdf.PdfEncryptionTest.java

License:Open Source License

@Test
public void encryptWithCertificateAndSignTest()
        throws IOException, DocumentException, GeneralSecurityException {
    removeCryptographyRestrictions();/*from w ww .ja  v a2 s . com*/
    Security.addProvider(new BouncyCastleProvider());
    String inPdf = SOURCE_FOLDER + "in.pdf";
    String outPdf = DEST_FOLDER + "encrypt_cert_signed.pdf";
    String tmpPdf = DEST_FOLDER + "encrypt_cert.pdf";

    encryptPdfWithCertificate(inPdf, tmpPdf, SOURCE_FOLDER + "test.cer");

    Certificate cert = getPublicCertificate(SOURCE_FOLDER + "test.cer");
    PrivateKey privateKey = getPrivateKey(SOURCE_FOLDER + "test.p12");
    certSign(getPublicCertificate(SOURCE_FOLDER + "test.cer"), privateKey, outPdf,
            new PdfReader(tmpPdf, cert, privateKey, new BouncyCastleProvider().getName()), "reason",
            "location");
    restoreCryptographyRestrictions();
}

From source file:com.itextpdf.text.pdf.PdfEncryptionTest.java

License:Open Source License

private static void certSign(Certificate cert, PrivateKey privateKey, String destinationPath, PdfReader reader,
        String reason, String location) throws IOException, DocumentException, GeneralSecurityException {
    Certificate[] chain = new Certificate[] { cert };

    BouncyCastleProvider provider = new BouncyCastleProvider();
    ExternalSignature pks = new PrivateKeySignature(privateKey, DigestAlgorithms.SHA1, provider.getName());

    FileOutputStream fout = new FileOutputStream(destinationPath);
    PdfStamper stamper = PdfStamper.createSignature(reader, fout, '\0', null, true);
    PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
    appearance.setReason(reason);/*w w  w.  j a  v  a2s. co  m*/
    appearance.setLocation(location);
    ExternalDigest digest = new BouncyCastleDigest();
    MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0,
            MakeSignature.CryptoStandard.CADES);
    stamper.close();
}

From source file:com.jamesashepherd.sshproxyj.core.Start.java

License:LGPL

@Override
public void startup() throws StartException {
    logger.info("sshproxyj starting");
    Security.addProvider(new BouncyCastleProvider());
    context = new FileSystemXmlApplicationContext(getSpringConfigURL());
}

From source file:com.jamesashepherd.sshproxyj.core.StartTest.java

License:LGPL

@BeforeClass
static public void setUp() throws SshProxyJException, IOException {
    Security.addProvider(new BouncyCastleProvider());
    System.setProperty("org.slf4j.simpleLogger.log.com.jamesashepherd", "debug");
    keyPair = KeyUtils.makeKeyPair(UtilsTest.testPublicKey(), UtilsTest.testPrivateKey());
    keyPair2 = KeyUtils.makeKeyPair(UtilsTest.test2PublicKey(), UtilsTest.test2PrivateKey());
}

From source file:com.jamesashepherd.sshproxyj.Start.java

License:LGPL

public void startup() throws StartException {

    clientSessions = new LinkedList<ClientSession>();

    // set up client
    client = SshClient.setUpDefaultClient();
    PublicKey publicKey = null;//  ww  w .java2 s  .co m
    KeyPair kp = null;
    try {
        InputStream is = new FileInputStream(new File("/tmp/id_rsa.pub"));
        publicKey = decodePublicKey(IOUtil.toString(is));

        BufferedReader br = new BufferedReader(new FileReader("/tmp/id_rsa"));
        Security.addProvider(new BouncyCastleProvider());
        PEMReader pr = new PEMReader(br);
        kp = (KeyPair) pr.readObject();
        pr.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    final KeyPair keyPair = new KeyPair(publicKey, kp.getPrivate());

    client.start();

    // set up server
    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(6667);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("/tmp/host.key"));
    // sshd.setShellFactory(new ProcessShellFactory(new String[] {
    // "/bin/bash", "-i", "-l" }));
    sshd.setShellFactory(new Factory<Command>() {

        ExitCallback exitCallBack;

        public Command create() {
            try {
                final ClientSession session = client.connect("localhost", 22).await().getSession();
                clientSessions.add(session);
                System.out.println("Started Client Session");
                session.authPublicKey("jas", keyPair);

                int ret = session.waitFor(ClientSession.CLOSED | ClientSession.AUTHED, 10 * 1000); // milliseconds
                System.out.println("Waited for auth: " + ret);
                if ((ret & ClientSession.CLOSED) != 0) {
                    System.err.println("error session closed");
                    System.exit(-1);
                }

                System.out.println("Still open");

                final ClientChannel channel = session.createChannel("shell");
                System.out.println("Returning Command");

                return new Command() {

                    public void setInputStream(InputStream in) {
                        channel.setIn(in);
                    }

                    public void setOutputStream(OutputStream out) {
                        channel.setOut(out);
                    }

                    public void setErrorStream(OutputStream err) {
                        channel.setErr(err);
                    }

                    public void setExitCallback(ExitCallback callback) {
                        exitCallBack = callback;
                    }

                    public void start(Environment env) throws IOException {
                        try {
                            channel.open();
                            new Thread(new Runnable() {

                                public void run() {
                                    channel.waitFor(ClientChannel.CLOSED, 0);
                                    exitCallBack.onExit(
                                            channel.getExitStatus() == null ? 1 : channel.getExitStatus());
                                    session.close(false);
                                }

                            }).start();
                        } catch (Exception e) {
                            throw new IOException(e);
                        }
                    }

                    public void destroy() {
                        try {
                            session.close(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    });

    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {

        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            PublicKey publicKey;
            try {
                InputStream is = new FileInputStream(new File("/tmp/id_rsa.pub"));
                publicKey = decodePublicKey(IOUtil.toString(is));
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

            return ((RSAPublicKey) publicKey).getModulus().equals(((RSAPublicKey) key).getModulus());
        }
    });

    try {
        sshd.start();
    } catch (IOException e) {
        throw new StartException("Failed to start SshServer", e);
    }
}

From source file:com.jaspersoft.jasperserver.api.security.encryption.EncryptionRSA.java

License:Open Source License

/**
 * {@inheritDoc}//w  ww  .j a  v  a  2 s  .c o m
 */
public String decrypt(String encrypted, PrivateKey privateKey) {
    Cipher dec;
    try {
        dec = Cipher.getInstance("RSA/NONE/NoPadding", new BouncyCastleProvider());
        dec.init(Cipher.DECRYPT_MODE, privateKey);
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("RSA algorithm not supported", e);
    }
    String[] blocks = SPLIT_BY_WHITESPACE.split(encrypted);
    StringBuilder result = new StringBuilder();
    try {
        for (int i = blocks.length - 1; i >= 0; i--) {
            byte[] data = StringUtil.hexStringToByteArray(blocks[i]);
            byte[] decryptedBlock = dec.doFinal(data);
            result.append(new String(decryptedBlock));
        }
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Decrypt error", e);
    }
    return result.toString();
}

From source file:com.jaspersoft.jasperserver.jaxrs.client.core.EncryptionUtils.java

License:Open Source License

private static String getEncryptedPassword(PublicKey publicKey, String pwd) throws Exception {
    byte[] encryptedUtfPass;
    Cipher enc = Cipher.getInstance("RSA/NONE/NoPadding", new BouncyCastleProvider());
    enc.init(Cipher.ENCRYPT_MODE, publicKey);
    String utfPass = URLEncoder.encode(pwd, CharEncoding.UTF_8);
    encryptedUtfPass = enc.doFinal(utfPass.getBytes());

    return byteArrayToHexString(encryptedUtfPass);
}

From source file:com.jlocksmith.MainFrame.java

License:Open Source License

/**
 * Main/*from  ww  w. ja  va2s. c  o  m*/
 * 
 * @param args
 */
public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception err) {
        err.printStackTrace();
    }

    try {
        SecurityUtil util = SecurityUtil.getInstance();
        util.addProvider(new BouncyCastleProvider());
    } catch (Exception err) {
        err.printStackTrace();
    }

    @SuppressWarnings("unused")
    MainFrame main = new MainFrame();
}

From source file:com.jtdowney.chloride.ChlorideTest.java

License:Open Source License

@BeforeClass
public static void InstallBouncyCastle() {
    Security.addProvider(new BouncyCastleProvider());
}

From source file:com.keepassdroid.crypto.CipherFactory.java

License:Open Source License

public static Cipher getInstance(String transformation, boolean androidOverride)
        throws NoSuchAlgorithmException, NoSuchPaddingException {
    try {//from   ww  w  . ja va 2 s  .c  om
        return Cipher.getInstance(transformation, new BouncyCastleProvider());
    } catch (NoSuchAlgorithmException e) {
        // Do nothing, fall through
    } catch (NoSuchPaddingException e) {
        // Do nothing, fall through
    }
    return Cipher.getInstance(transformation);
}