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.globus.cog.security.cert.request.GridCertRenewalRequest.java

License:Open Source License

/**
 * Converts to PEM encoding./*from   w  ww .  ja v  a2  s  .  c o  m*/
 */
static private String toCertPEM(byte[] data) {
    byte[] enc_data = Base64.encode(data);
    String header = "-----BEGIN CERTIFICATE-----";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        PEMUtils.writeBase64(out, header, enc_data, "-----END CERTIFICATE-----");
    } catch (IOException e) {
    }
    return new String(out.toByteArray());
}

From source file:org.globus.cog.security.cert.request.GridCertRenewalRequest.java

License:Open Source License

/**
 * Converts to PEM encoding.//w w w .j  a va2 s.  c  o  m
 */
static private String toCertReqPEM(byte[] data) {
    byte[] enc_data = Base64.encode(data);
    String header = "-----BEGIN CERTIFICATE REQUEST-----";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        PEMUtils.writeBase64(out, header, enc_data, "-----END CERTIFICATE REQUEST-----");
    } catch (IOException e) {
    }
    return new String(out.toByteArray());
}

From source file:org.globus.cog.security.cert.request.GridCertRequest.java

License:Open Source License

/**
 * Converts to PEM encoding.//from w w w .j a va  2 s.  c o  m
 */
static public String toPEM(byte[] data) {
    byte[] enc_data = Base64.encode(data);
    String header = "-----BEGIN CERTIFICATE REQUEST-----";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        PEMUtils.writeBase64(out, header, enc_data, "-----END CERTIFICATE REQUEST-----");
    } catch (IOException e) {
        throw new RuntimeException("Unexpected error: " + e.getMessage());
    }
    return new String(out.toByteArray());
}

From source file:org.globus.cog.security.certrequest.GridCertRequest.java

License:Open Source License

/**
 * Converts to PEM encoding.//from  w  w  w  .j  a va2 s. c o  m
 * @param data cert bytes
 * @return pem enoded CSR
 */
static private String toPEM(byte[] data) {
    byte[] enc_data = Base64.encode(data);
    String header = "-----BEGIN CERTIFICATE REQUEST-----";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        PEMUtils.writeBase64(out, header, enc_data, "-----END CERTIFICATE REQUEST-----");
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Unexpected error: " + e.getMessage());
    }
    return new String(out.toByteArray());
}

From source file:org.globus.ftp.extended.GridFTPOutputStream.java

License:Apache License

private void writeToken(byte[] header, byte[] token) throws IOException {
    this.out.write(header);
    this.out.write(Base64.encode(token));
    this.out.write(CRLF);
    this.out.flush();
}

From source file:org.globus.gsi.OpenSSLKey.java

License:Apache License

private String toPEM() {

    byte[] data = (this.keyData == null) ? this.encodedKey : Base64.encode(this.keyData);

    String header = HEADER;//from   w  ww .j av  a2  s . c  om

    if (isEncrypted()) {
        StringBuffer buf = new StringBuffer(header);
        buf.append(PEMUtil.LINE_SEP);
        buf.append("Proc-Type: 4,ENCRYPTED");
        buf.append(PEMUtil.LINE_SEP);
        buf.append("DEK-Info: ").append(this.encAlgStr);
        buf.append(",").append(PEMUtil.toHex(initializationVector.getIV()));
        buf.append(PEMUtil.LINE_SEP);
        header = buf.toString();
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {
        PEMUtil.writeBase64(out, header, data, "-----END RSA PRIVATE KEY-----");
    } catch (IOException e) {
        // JGLOBUS-91
        throw new RuntimeException("Unexpected error", e);
    }

    return new String(out.toByteArray());
}

From source file:org.globus.gsi.util.CertificateIOUtil.java

License:Apache License

/**
 * Writes certificate to the specified output stream in PEM format.
 *///from  w  w w  .  j  a  va  2  s  .  co  m
public static void writeCertificate(OutputStream out, X509Certificate cert)
        throws IOException, CertificateEncodingException {
    PEMUtil.writeBase64(out, "-----BEGIN CERTIFICATE-----", Base64.encode(cert.getEncoded()),
            "-----END CERTIFICATE-----");
}

From source file:org.globus.security.OpenSSLKey.java

License:Apache License

private String toPEM() {

    byte[] data = (this.keyData == null) ? this.encodedKey : Base64.encode(this.keyData);

    String header = HEADER;// ww  w.  ja  va2  s  .  c  om

    if (isEncrypted()) {
        StringBuffer buf = new StringBuffer(header);
        buf.append(PEMUtil.LINE_SEP);
        buf.append("Proc-Type: 4,ENCRYPTED");
        buf.append(PEMUtil.LINE_SEP);
        buf.append("DEK-Info: ").append(this.encAlgStr);
        buf.append(",").append(PEMUtil.toHex(initializationVector.getIV()));
        buf.append(PEMUtil.LINE_SEP);
        header = buf.toString();
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {
        PEMUtil.writeBase64(out, header, data, "-----END RSA PRIVATE KEY-----");
    } catch (IOException e) {
        // FIXME !!
        throw new RuntimeException("Unexpected error", e);
    }

    return new String(out.toByteArray());
}

From source file:org.globus.tools.GridCertRequest.java

License:Open Source License

/**
 * Generates a encrypted private key and certificate request.
 *//*from   w w w.  ja  v  a  2s.  c om*/
static public void genCertificateRequest(String dname, String emailAddressOfCA, String password, File keyFile,
        File certFile, File certReqFile) throws Exception {

    String sigAlgName = "MD5WithRSA";
    String keyAlgName = "RSA";

    CertUtil.init();

    X509Name name = new X509Name(dname);

    String certSubject = X509NameHelper.toString(name);

    System.out.println("Generating a 1024 bit RSA private key");

    // Generate a new key pair.
    KeyPairGenerator keygen = KeyPairGenerator.getInstance(keyAlgName);
    keygen.initialize(1024);
    KeyPair keyPair = keygen.genKeyPair();
    PrivateKey privKey = keyPair.getPrivate();
    PublicKey pubKey = keyPair.getPublic();

    // Generate the certificate request.
    DERSet derSet = new DERSet();
    PKCS10CertificationRequest request = new PKCS10CertificationRequest(sigAlgName, name, pubKey, derSet,
            privKey);

    // Save the certificate request to a .pem file.
    byte[] data = request.getEncoded();
    byte[] encodedData = Base64.encode(data);

    PrintStream ps = null;

    try {
        ps = new PrintStream(new FileOutputStream(certReqFile));

        boolean caEmail = false;

        if ((emailAddressOfCA != null) && (emailAddressOfCA.length() > 0)) {
            caEmail = true;
            ps.print("\n\n" + "Please mail the following certificate request to " + emailAddressOfCA);
        } else {
            ps.print("\n\n"
                    + "Please send the following certificate request to the Certificate Authority (CA). Refer to CA instructions for details on to send the request.");
        }
        ps.print("\n\n" + "==================================================================\n" + "\n"
                + "Certificate Subject:\n" + "\n" + certSubject + "\n" + "\n"
                + "The above string is known as your user certificate subject, and it \n"
                + "uniquely identifies this user.\n" + "\n"
                + "To install this user certificate, please save this e-mail message\n"
                + "into the following file.\n" + "\n" + "\n" + certReqFile.getAbsolutePath() + "\n" + "\n"
                + "\n" + "      You need not edit this message in any way. Simply \n"
                + "      save this e-mail message to the file.\n" + "\n" + "\n"
                + "If you have any questions about the certificate contact\n" + "the Certificate Authority");
        if (caEmail) {
            ps.print("at " + emailAddressOfCA);
        }
        ps.print("\n\n");
        PEMUtils.writeBase64(ps, "-----BEGIN CERTIFICATE REQUEST-----", encodedData,
                "-----END CERTIFICATE REQUEST-----");
    } finally {
        if (ps != null) {
            ps.close();
        }
    }

    // Save private key to a .pem file.
    OpenSSLKey key = new BouncyCastleOpenSSLKey(privKey);
    if (password != null) {
        key.encrypt(password);
    }
    // this will set the permissions correctly already
    key.writeTo(keyFile.getAbsolutePath());

    // Create an empty cert file.
    certFile.createNewFile();

    System.out.println("A private key and a certificate request has been generated with the subject:");
    System.out.println();
    System.out.println(certSubject);
    System.out.println();

    System.out.println("The private key is stored in " + keyFile.getAbsolutePath());
    System.out.println("The request is stored in " + certReqFile.getAbsolutePath());

}

From source file:org.gluu.oxtrust.action.ManageCertificateAction.java

License:MIT License

@Restrict("#{s:hasPermission('configuration', 'access')}")
public String generateCSR(String fileName) {
    if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        Security.addProvider(new BouncyCastleProvider());
    }//from w ww  .  jav a  2s .c  om

    KeyPair pair = getKeyPair(fileName);
    boolean result = false;
    if (pair != null) {
        String url = applicationConfiguration.getIdpUrl().replaceFirst(".*//", "");
        String csrPrincipal = String.format("CN=%s", url);
        X500Principal principal = new X500Principal(csrPrincipal);

        PKCS10CertificationRequest csr = null;
        try {
            csr = new PKCS10CertificationRequest("SHA1withRSA", principal, pair.getPublic(), null,
                    pair.getPrivate());
        } catch (GeneralSecurityException e) {
            log.error(e.getMessage(), e);
            return OxTrustConstants.RESULT_FAILURE;
        }

        // Form download responce
        StringBuilder response = new StringBuilder();

        response.append(BEGIN_CERT_REQ + "\n");
        response.append(WordUtils.wrap(new String(Base64.encode(csr.getDEREncoded())), 64, "\n", true) + "\n");
        response.append(END_CERT_REQ + "\n");

        result = ResponseHelper.downloadFile("csr.pem", OxTrustConstants.CONTENT_TYPE_TEXT_PLAIN,
                response.toString().getBytes(), facesContext);
    }

    return result ? OxTrustConstants.RESULT_SUCCESS : OxTrustConstants.RESULT_FAILURE;
}