Example usage for org.bouncycastle.util.encoders Base64 encode

List of usage examples for org.bouncycastle.util.encoders Base64 encode

Introduction

In this page you can find the example usage for org.bouncycastle.util.encoders Base64 encode.

Prototype

public static byte[] encode(byte[] data) 

Source Link

Document

encode the input data producing a base 64 encoded byte array.

Usage

From source file:org.brownsocks.payments.gateways.enets.pgp.BCPGPProvider.java

@Override
public String signAndEncrypt(String message) throws IOException {

    try {//from  w w  w  . j av a 2  s .  c o m
        /* Final < Armored < Crypted < Clear PGP */
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ArmoredOutputStream armoredOutput = new ArmoredOutputStream(out);
        PGPEncryptedDataGenerator crypter = new PGPEncryptedDataGenerator(PGPEncryptedDataGenerator.S2K_SHA1,
                new SecureRandom(), _provider);
        crypter.addMethod(getRemotePublicKey());
        BCPGOutputStream pgpOut = new BCPGOutputStream(crypter.open(armoredOutput, new byte[512]));

        /* Prepare for signing */
        PGPSignatureGenerator signer = new PGPSignatureGenerator(getSigningPublicKey().getAlgorithm(),
                PGPUtil.SHA1, _provider);
        signer.initSign(PGPSignature.BINARY_DOCUMENT, getSigningPrivateKey());

        /* Output the standard header */
        signer.generateOnePassVersion(false).encode(pgpOut);

        /* Output the literal data */
        PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator(true);
        literalDataGenerator.open(pgpOut, 'b', "bar", message.getBytes().length, new Date())
                .write(message.getBytes());

        /* Calculate signature and output it */
        signer.update(message.getBytes());
        signer.generate().encode(pgpOut);

        pgpOut.close();
        armoredOutput.close();
        out.close();

        byte[] result = out.toByteArray();

        // brain dead UMAPI adds an extra base64 encoding on top of the ASCII armored string. Go figure.
        return new String(Base64.encode(result));

    } catch (PGPException pgpException) {
        throw new IOException("PGP subsystem problem.", pgpException);

    } catch (NoSuchAlgorithmException noSuchAlgorithmException) {
        throw new IOException("Missing algorithm. Are you running a compatible JVM/Bouncycastle version?",
                noSuchAlgorithmException);

    } catch (SignatureException signatureException) {
        throw new IOException("PGP subsystem problem.", signatureException);

    } catch (NoSuchProviderException noSuchProviderException) {
        throw new IOException("Missing provider. Are you running a compatible JVM/Bouncycastle version?",
                noSuchProviderException);

    }

}

From source file:org.ccnx.ccn.impl.support.DataUtils.java

License:Open Source License

public static byte[] base64Encode(byte[] input) {
    return Base64.encode(input);
}

From source file:org.cesecore.certificates.certificate.HashID.java

License:Open Source License

private HashID(byte hash[]) {
    final String b64padded = new String(Base64.encode(hash));
    if (b64padded.length() != 28 || b64padded.charAt(27) != '=') {
        this.isOK = false;
        this.b64 = b64padded;
    } else {/*from  www. j  ava  2 s.c  om*/
        this.isOK = true;
        this.b64 = b64padded.substring(0, 27);
    }
    this.b64url = this.b64.replaceAll("\\+", "%2B");
    this.key = Integer.valueOf(new BigInteger(hash).hashCode());
}

From source file:org.cesecore.certificates.endentity.ExtendedInformation.java

License:Open Source License

/**
 * @param sn//  w w w.j  a v  a 2s.c  o m
 *            the serial number to be used for the certificate
 */
public void setCertificateSerialNumber(BigInteger sn) {
    if (sn == null) {
        this.data.remove(CERTIFICATESERIALNUMBER);
        return;
    }
    final String s = new String(Base64.encode(sn.toByteArray()));
    this.data.put(CERTIFICATESERIALNUMBER, s);
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

private void addPrivKey(KeystoreEditor keystoreEditor, File keyFile, String type)
        throws KeystoreEditor.KeystoreEditorException, IOException {
    FileInputStream fileInputStream = new FileInputStream(keyFile);
    byte[] keyBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("asdf", "changeit", "", new String(Base64.encode(keyBytes)), type,
            keyFile.toString());//from w w w  .jav a2 s  .c  o  m
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

private void addCertChain(KeystoreEditor keystoreEditor)
        throws KeystoreEditor.KeystoreEditorException, IOException {
    FileInputStream fileInputStream = new FileInputStream(chainFile);
    byte[] crtBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("asdf", "changeit", "", new String(Base64.encode(crtBytes)),
            KeystoreEditor.PEM_TYPE, chainFile.toString());
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test
public void testAddKeyJks() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(jksFile);
    byte[] keyBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("asdf", "changeit", "changeit", new String(Base64.encode(keyBytes)), "",
            jksFile.toString());//from   ww w. ja va 2s .  com
    List<Map<String, Object>> keystore = keystoreEditor.getKeystore();
    Assert.assertThat(keystore.size(), Is.is(1));
    Assert.assertThat((String) keystore.get(0).get("alias"), Is.is("asdf"));

    List<Map<String, Object>> truststore = keystoreEditor.getTruststore();
    Assert.assertThat(truststore.size(), Is.is(0));
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test
public void testAddKeyP12() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(pkcs12StoreFile);
    byte[] keyBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("asdf", "changeit", "changeit", new String(Base64.encode(keyBytes)),
            KeystoreEditor.PKCS12_TYPE, pkcs12StoreFile.toString());
    List<Map<String, Object>> keystore = keystoreEditor.getKeystore();
    Assert.assertThat(keystore.size(), Is.is(1));
    Assert.assertThat((String) keystore.get(0).get("alias"), Is.is("asdf"));

    List<Map<String, Object>> truststore = keystoreEditor.getTruststore();
    Assert.assertThat(truststore.size(), Is.is(0));
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test
public void testAddKeyLocal() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(localhostCrtFile);
    byte[] crtBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("localhost", "changeit", "", new String(Base64.encode(crtBytes)),
            KeystoreEditor.PEM_TYPE, localhostCrtFile.toString());
    List<Map<String, Object>> keystore = keystoreEditor.getKeystore();
    Assert.assertThat(keystore.size(), Is.is(1));

    List<Map<String, Object>> truststore = keystoreEditor.getTruststore();
    Assert.assertThat(truststore.size(), Is.is(0));

    keystoreEditor = new KeystoreEditor();
    fileInputStream = new FileInputStream(localhostKeyFile);
    byte[] keyBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addPrivateKey("localhost", "changeit", "changeit", new String(Base64.encode(keyBytes)),
            KeystoreEditor.PEM_TYPE, localhostKeyFile.toString());
    keystore = keystoreEditor.getKeystore();
    Assert.assertThat(keystore.size(), Is.is(1));
    Assert.assertThat((String) keystore.get(0).get("alias"), Is.is("localhost"));

    truststore = keystoreEditor.getTruststore();
    Assert.assertThat(truststore.size(), Is.is(0));
}

From source file:org.codice.ddf.security.certificate.keystore.editor.KeystoreEditorTest.java

License:Open Source License

@Test
public void testAddCert() throws KeystoreEditor.KeystoreEditorException, IOException {
    KeystoreEditor keystoreEditor = new KeystoreEditor();
    FileInputStream fileInputStream = new FileInputStream(crtFile);
    byte[] crtBytes = IOUtils.toByteArray(fileInputStream);
    IOUtils.closeQuietly(fileInputStream);
    keystoreEditor.addTrustedCertificate("asdf", "changeit", "", new String(Base64.encode(crtBytes)),
            KeystoreEditor.PEM_TYPE, crtFile.toString());
    List<Map<String, Object>> truststore = keystoreEditor.getTruststore();
    Assert.assertThat(truststore.size(), Is.is(1));
    Assert.assertThat((String) truststore.get(0).get("alias"), Is.is("asdf"));

    List<Map<String, Object>> keystore = keystoreEditor.getKeystore();
    Assert.assertThat(keystore.size(), Is.is(0));
}