Search in sources :

Example 16 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project tmdm-studio-se by Talend.

the class PasswordUtil method main.

public static void main(String[] args) {
    Base64 base64 = new Base64();
    // $NON-NLS-1$
    String str = "qwe";
    byte[] enbytes = null;
    String encodeStr = null;
    byte[] debytes = null;
    String decodeStr = null;
    enbytes = base64.encode(str.getBytes());
    encodeStr = new String(enbytes);
    debytes = base64.decode(enbytes);
    decodeStr = new String(debytes);
    // $NON-NLS-1$
    System.out.println("plain password:" + str);
    // $NON-NLS-1$
    System.out.println("encrypted password:" + encodeStr);
    // $NON-NLS-1$
    System.out.println("decrypted password:" + decodeStr);
}
Also used : Base64(org.apache.commons.codec.binary.Base64)

Example 17 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project knox by apache.

the class AbstractJWTFilterTest method generateKeys.

@BeforeClass
public static void generateKeys() throws Exception, NoSuchAlgorithmException {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(2048);
    KeyPair KPair = kpg.generateKeyPair();
    String dn = buildDistinguishedName(InetAddress.getLocalHost().getHostName());
    Certificate cert = X509CertificateUtil.generateCertificate(dn, KPair, 365, "SHA1withRSA");
    byte[] data = cert.getEncoded();
    Base64 encoder = new Base64(76, "\n".getBytes("ASCII"));
    pem = new String(encoder.encodeToString(data).getBytes("ASCII")).trim();
    publicKey = (RSAPublicKey) KPair.getPublic();
    privateKey = (RSAPrivateKey) KPair.getPrivate();
}
Also used : KeyPair(java.security.KeyPair) Base64(org.apache.commons.codec.binary.Base64) KeyPairGenerator(java.security.KeyPairGenerator) Certificate(java.security.cert.Certificate) BeforeClass(org.junit.BeforeClass)

Example 18 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project knox by apache.

the class BaseKeystoreService method writeCertificateToFile.

protected void writeCertificateToFile(Certificate cert, final File file) throws CertificateEncodingException, IOException {
    byte[] bytes = cert.getEncoded();
    Base64 encoder = new Base64(76, "\n".getBytes("ASCII"));
    try (final FileOutputStream out = new FileOutputStream(file)) {
        out.write("-----BEGIN CERTIFICATE-----\n".getBytes("ASCII"));
        out.write(encoder.encodeToString(bytes).getBytes("ASCII"));
        out.write("-----END CERTIFICATE-----\n".getBytes("ASCII"));
    }
}
Also used : Base64(org.apache.commons.codec.binary.Base64) FileOutputStream(java.io.FileOutputStream)

Example 19 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project knox by apache.

the class X509CertificateUtil method writeCertificateToFile.

public static void writeCertificateToFile(Certificate cert, final File file) throws CertificateEncodingException, IOException {
    byte[] bytes = cert.getEncoded();
    Base64 encoder = new Base64(76, "\n".getBytes("ASCII"));
    try (final FileOutputStream out = new FileOutputStream(file)) {
        out.write("-----BEGIN CERTIFICATE-----\n".getBytes("ASCII"));
        out.write(encoder.encodeToString(bytes).getBytes("ASCII"));
        out.write("-----END CERTIFICATE-----\n".getBytes("ASCII"));
    }
}
Also used : Base64(org.apache.commons.codec.binary.Base64) FileOutputStream(java.io.FileOutputStream)

Example 20 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project coprhd-controller by CoprHD.

the class VcenterHostCertificateGetter method getSSLThumbprint.

String getSSLThumbprint(String host, Integer port, String certificatePath, String username, String password, Integer timeout) throws Exception {
    String cert = null;
    try {
        cert = executeRequest(host, port, certificatePath, username, password, timeout);
        if (StringUtils.isEmpty(cert)) {
            _log.error("Empty or null response string from host - {} ", host);
            throw new RuntimeException("Empty or null response string from host - " + host);
        }
    } catch (Exception ex) {
        _log.error("Unexepected error executing request https://{}", host + ":" + port + certificatePath, ex);
        throw new Exception("Unexepected error executing request https://" + host + ":" + port + certificatePath, ex);
    }
    try {
        // extracted base64 encoded certificate from response
        _log.info("Extracted base64 encoded certification from response");
        _log.info("Content: {}", cert);
        // strip pem certificate of begin and end strings
        cert = cert.replaceAll("-----BEGIN CERTIFICATE-----", "").replaceAll("-----END CERTIFICATE-----", "");
        _log.info("Certificate extracted {}", cert);
    } catch (Exception ex) {
        _log.error("Unexepected error extracting content from response {} ", ex.getMessage(), ex);
        throw new Exception("Unexepected error extracting content from response " + ex.getMessage(), ex);
    }
    if (cert == null || cert.equals("")) {
        _log.error("Certificate base 64 encoded string " + cert);
        throw new Exception("Certificate base 64 encoded string " + cert);
    }
    /**
     * THEN SHA-1 HASH THE CERT
     */
    ByteArrayInputStream byteArrayInputStream = null;
    try {
        // decode
        _log.info("Decode base64 encoded certificate string");
        Base64 base64 = new Base64();
        byteArrayInputStream = new ByteArrayInputStream(base64.decode(cert));
        // DO NOT PRINT THE byte[] since it breaks the next step - Could not parse certificate:
        _log.info("Base64 decoded");
    // java.io.IOException: DerInputStream.getLength(): lengthTag=127, too big.
    } catch (Exception ex) {
        _log.error("Unexepected error decode base64 encoded certificate ", ex);
        throw new Exception("Unexepected error decode base64 encoded certificate");
    }
    if (byteArrayInputStream == null) {
        _log.error("Decoded base 64 byte[] null");
        throw new Exception("Decoded base 64 byte[] null");
    }
    X509Certificate certificate = null;
    try {
        // convert to x509 cert
        _log.info("Convert decoded base64 byte[] to X509 certificate");
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        certificate = (X509Certificate) cf.generateCertificate(byteArrayInputStream);
        _log.info("X509 certificate created " + certificate);
    } catch (Exception ex) {
        _log.error("Unexepected error creating x509 certificate ", ex);
        throw new Exception("Unexepected error creating x509 certificate");
    }
    if (certificate == null) {
        _log.error("X509 Certificate null");
        throw new Exception("X509 Certificate null");
    }
    byte[] digest;
    try {
        // hash to SHA1
        _log.info("Hash X509 certificate to SHA-1");
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] der = certificate.getEncoded();
        md.update(der);
        digest = md.digest();
        _log.info("SHA-1 hash created " + digest);
    } catch (Exception ex) {
        _log.error("Unexepected error SHA-1 hashing certificate ", ex);
        throw new Exception("Unexepected error SHA-1 hashing certificate");
    }
    if (digest == null) {
        _log.error("SHA-1 hash null");
        throw new Exception("SHA-1 hash null");
    }
    String thumbprint;
    try {
        // convert to hex
        _log.info("Convert hash to hex");
        // BigInteger bigInt = new BigInteger(1,digest)
        // thumbprint = bigInt.toString(16)
        thumbprint = getHex(digest);
        _log.info("Hex created " + thumbprint);
    } catch (Exception ex) {
        _log.error("Unexepected error converting SSL thumbprint to hex ", ex);
        throw new Exception("Unexepected error converting SSL thumbprint to hex");
    }
    if (thumbprint == null || thumbprint.equals("")) {
        _log.error("thumbprint " + thumbprint);
        throw new Exception("thumbprint " + thumbprint);
    }
    String hash;
    try {
        // splice in colons and bring to upper
        _log.info("Splice colons to hash and bring to upper case");
        thumbprint = thumbprint.toUpperCase();
        StringBuffer buf = new StringBuffer(thumbprint);
        int index = 2;
        while (index < buf.toString().length()) {
            buf.insert(index, ':');
            // 2 (for chars) + 1 (for spliced colon)
            index += 3;
        }
        hash = buf.toString();
        _log.info("Hash string formatted " + hash);
    } catch (Exception ex) {
        _log.error("Unexepected error splicing colons into SSL thumbprint ", ex);
        throw new Exception("Unexepected error splicing colons into SSL thumbprint");
    }
    if (hash == null || hash.equals("")) {
        _log.error("SSL thumbprint is empty");
        throw new Exception("SSL thumbprint is empty");
    }
    if (hash.length() != 59) {
        _log.error("SSL thumbprint does not appear to be a valid");
        throw new Exception("SSL thumbprint does not appear to be a valid");
    }
    return hash;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) ByteArrayInputStream(java.io.ByteArrayInputStream) MessageDigest(java.security.MessageDigest) CertificateFactory(java.security.cert.CertificateFactory) IOException(java.io.IOException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) X509Certificate(java.security.cert.X509Certificate)

Aggregations

Base64 (org.apache.commons.codec.binary.Base64)149 IOException (java.io.IOException)33 Test (org.junit.Test)29 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)14 ByteArrayInputStream (java.io.ByteArrayInputStream)11 InputStream (java.io.InputStream)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 SecretKeySpec (javax.crypto.spec.SecretKeySpec)9 URL (java.net.URL)8 HashMap (java.util.HashMap)8 Cipher (javax.crypto.Cipher)8 File (java.io.File)7 MessageDigest (java.security.MessageDigest)7 Mac (javax.crypto.Mac)7 ServletException (javax.servlet.ServletException)7 FileNotFoundException (java.io.FileNotFoundException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 URLConnection (java.net.URLConnection)5