Search in sources :

Example 36 with BASE64Encoder

use of sun.misc.BASE64Encoder in project atlas by alibaba.

the class SignedJarBuilder method writeSignatureFile.

/**
 * Writes a .SF file with a digest to the manifest.
 */
private void writeSignatureFile(SignatureOutputStream out) throws IOException, GeneralSecurityException {
    Manifest sf = new Manifest();
    Attributes main = sf.getMainAttributes();
    main.putValue("Signature-Version", "1.0");
    main.putValue("Created-By", "1.0 (Android)");
    BASE64Encoder base64 = new BASE64Encoder();
    MessageDigest md = MessageDigest.getInstance(DIGEST_ALGORITHM);
    PrintStream print = new PrintStream(new DigestOutputStream(new ByteArrayOutputStream(), md), true, "utf-8");
    // Digest of the entire manifest
    mManifest.write(print);
    print.flush();
    main.putValue(DIGEST_MANIFEST_ATTR, base64.encode(md.digest()));
    Map<String, Attributes> entries = mManifest.getEntries();
    for (Map.Entry<String, Attributes> entry : entries.entrySet()) {
        // Digest of the manifest stanza for this entry.
        print.print("Name: " + entry.getKey() + "\r\n");
        for (Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
            print.print(att.getKey() + ": " + att.getValue() + "\r\n");
        }
        print.print("\r\n");
        print.flush();
        Attributes sfAttr = new Attributes();
        sfAttr.putValue(DIGEST_ATTR, base64.encode(md.digest()));
        sf.getEntries().put(entry.getKey(), sfAttr);
    }
    sf.write(out);
    // As a workaround, add an extra CRLF in this case.
    if ((out.size() % 1024) == 0) {
        out.write('\r');
        out.write('\n');
    }
}
Also used : BASE64Encoder(sun.misc.BASE64Encoder) Map(java.util.Map)

Example 37 with BASE64Encoder

use of sun.misc.BASE64Encoder in project orientdb by orientechnologies.

the class OCommandExecutorSQLSelectTest method testBinaryField.

@Test
public void testBinaryField() {
    // issue #6379
    byte[] array = new byte[] { 1, 4, 5, 74, 3, 45, 6, 127, -120, 2 };
    db.command(new OCommandSQL("create class TestBinaryField")).execute();
    ODocument doc = db.newInstance("TestBinaryField");
    doc.field("binaryField", array);
    doc.save();
    doc = db.newInstance("TestBinaryField");
    doc.field("binaryField", "foobar");
    doc.save();
    String base64Value = new BASE64Encoder().encode(array);
    List<ODocument> results = db.query(new OSQLSynchQuery<ODocument>("select from TestBinaryField where binaryField = decode(?, 'base64')"), base64Value);
    assertEquals(results.size(), 1);
    Object value = results.get(0).field("binaryField");
    assertEquals(value, array);
}
Also used : BASE64Encoder(sun.misc.BASE64Encoder) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 38 with BASE64Encoder

use of sun.misc.BASE64Encoder in project paascloud-master by paascloud.

the class HttpAesUtil method encrypt.

/**
 * 加密
 *
 * @param contentParam 需要加密的内容
 * @param keyParam     加密密码
 * @param md5Key       是否对key进行md5加密
 * @param ivParam      加密向量
 *
 * @return 加密后的字节数据 string
 */
public static String encrypt(String contentParam, String keyParam, boolean md5Key, String ivParam) {
    try {
        byte[] content = contentParam.getBytes(CHAR_SET);
        byte[] key = keyParam.getBytes(CHAR_SET);
        byte[] iv = ivParam.getBytes(CHAR_SET);
        if (md5Key) {
            MessageDigest md = MessageDigest.getInstance("MD5");
            key = md.digest(key);
        }
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        // "算法/模式/补码方式"
        Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding");
        // 使用CBC模式, 需要一个向量iv, 可增加加密算法的强度
        IvParameterSpec ivps = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivps);
        byte[] bytes = cipher.doFinal(content);
        return new BASE64Encoder().encode(bytes);
    } catch (Exception ex) {
        log.error("加密密码失败", ex);
        throw new HttpAesException("加密失败");
    }
}
Also used : HttpAesException(com.paascloud.exception.HttpAesException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) BASE64Encoder(sun.misc.BASE64Encoder) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest) HttpAesException(com.paascloud.exception.HttpAesException)

Example 39 with BASE64Encoder

use of sun.misc.BASE64Encoder in project pancm_project by xuwujing.

the class CopyOfMyTools method GetImageStr.

/**
 * Get image str string.
 *
 * @param fileurl the fileurl
 * @return the string
 */
// 图片转化成base64字符串
public static String GetImageStr(String fileurl) {
    // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
    // 待处理的图片
    String imgFile = fileurl;
    InputStream in = null;
    byte[] data = null;
    // 读取图片字节数组
    try {
        in = new FileInputStream(imgFile);
        data = new byte[in.available()];
        in.read(data);
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 对字节数组Base64编码
    BASE64Encoder encoder = new BASE64Encoder();
    // 返回Base64编码过的字节数组字符串
    return encoder.encode(data);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BASE64Encoder(sun.misc.BASE64Encoder) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 40 with BASE64Encoder

use of sun.misc.BASE64Encoder in project pmph by BCSquad.

the class SyncUtils method encrypt.

/**
 * 根据密钥对指定的明文plainText进行加密.
 *
 * @param plainText 明文
 * @return 加密后的密文.
 */
public static final String encrypt(String plainText) {
    Key secretKey = getKey("medu");
    try {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] p = plainText.getBytes("UTF-8");
        byte[] result = cipher.doFinal(p);
        BASE64Encoder encoder = new BASE64Encoder();
        String encoded = encoder.encode(result);
        return encoded;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : BASE64Encoder(sun.misc.BASE64Encoder) Cipher(javax.crypto.Cipher) Key(java.security.Key) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

BASE64Encoder (sun.misc.BASE64Encoder)45 IOException (java.io.IOException)17 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 MessageDigest (java.security.MessageDigest)8 FileInputStream (java.io.FileInputStream)7 BufferedInputStream (java.io.BufferedInputStream)6 BufferedOutputStream (java.io.BufferedOutputStream)6 FileNotFoundException (java.io.FileNotFoundException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Map (java.util.Map)4 HostnameVerifier (javax.net.ssl.HostnameVerifier)4 SSLSession (javax.net.ssl.SSLSession)4 Call (org.apache.axis.client.Call)4 BASE64Decoder (sun.misc.BASE64Decoder)4 File (java.io.File)3 URL (java.net.URL)3 Signature (java.security.Signature)3 X509Certificate (java.security.cert.X509Certificate)3 Date (java.util.Date)3