Search in sources :

Example 21 with Base64.encodeBase64String

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

the class RESTClient method setResourceHeaders.

/**
 * Sets required headers into the passed WebResource.
 *
 * @param resource The resource to which headers are added.
 */
Builder setResourceHeaders(WebResource resource) {
    StringBuffer credentials = new StringBuffer(_username).append(HDSConstants.COLON).append(_password);
    Base64 base64 = new Base64();
    String encodedStr = base64.encodeToString(credentials.toString().getBytes());
    return resource.header("Authorization", "Basic " + encodedStr);
}
Also used : Base64(org.apache.commons.codec.binary.Base64)

Example 22 with Base64.encodeBase64String

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

the class AESEncryptDecrypt method decode.

/**
 * 解密需要解密的密文
 *
 * @param code 密文
 * @param key  the key
 * @return the string
 * @key 密钥
 */
public static String decode(String code, String key) {
    if (null == code || null == key) {
        return null;
    }
    String plainText = null;
    try {
        // BASE64 DECODE
        // BASE64Decoder b64 = new BASE64Decoder();
        // byte[] inputStream = b64.decodeBuffer(code);
        // BASE64 DECODE
        byte[] inputStream = new Base64().decode(code);
        // DESj解密
        byte[] key_ = getKey(key);
        SecretKey desKey = getDesKey(key_);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, desKey);
        byte[] desText = cipher.doFinal(inputStream);
        plainText = new String(desText, "UTF-8");
        // 因为MD5是固定的32byte的长度,这里截取32byte之后的内容即为明文内容
        plainText = plainText.substring(32, plainText.length());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return plainText;
}
Also used : SecretKey(javax.crypto.SecretKey) Base64(org.apache.commons.codec.binary.Base64) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 23 with Base64.encodeBase64String

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

the class SecretKeyhepler method getPublicKey.

// 获取公钥
public static PublicKey getPublicKey(String key) throws Exception {
    byte[] keyBytes;
    Base64 base64 = new Base64();
    keyBytes = base64.decode(key);
    /* keyBytes = (new BASE64Decoder()).decodeBuffer(key);*/
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    PublicKey publicKey = keyFactory.generatePublic(keySpec);
    return publicKey;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 24 with Base64.encodeBase64String

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

the class SecretKeyhepler method getPrivateKey.

// 获取私钥
public static PrivateKey getPrivateKey(String key) throws Exception {
    byte[] keyBytes;
    /*keyBytes = (new BASE64Decoder()).decodeBuffer(key);*/
    Base64 base64 = new Base64();
    keyBytes = base64.decode(key);
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
    return privateKey;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec)

Example 25 with Base64.encodeBase64String

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

the class KeyCertificatePairGenerator method getCertificateChainAsString.

/**
 * @param certChain - the certificate chain to parse to PEM format
 * @return the specified certificate chain in PEM format
 * @throws IOException
 * @throws CertificateEncodingException
 */
public static String getCertificateChainAsString(Certificate[] certChain) throws CertificateEncodingException {
    StringBuilder builder = new StringBuilder();
    Base64 encoder = new Base64(PEM_OUTPUT_LINE_SIZE);
    boolean isFirst = true;
    for (Certificate certificate : certChain) {
        if (!isFirst) {
            builder.append(System.lineSeparator());
        }
        builder.append(PEM_BEGIN_CERT);
        builder.append(System.lineSeparator());
        builder.append(encoder.encodeAsString(certificate.getEncoded()));
        builder.append(PEM_END_CERT);
        isFirst = false;
    }
    return builder.toString();
}
Also used : Base64(org.apache.commons.codec.binary.Base64) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

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