Search in sources :

Example 16 with BASE64Decoder

use of sun.misc.BASE64Decoder in project j2objc by google.

the class X509Factory method readOneBlock.

/**
 * Returns an ASN.1 SEQUENCE from a stream, which might be a BER-encoded
 * binary block or a PEM-style BASE64-encoded ASCII data. In the latter
 * case, it's de-BASE64'ed before return.
 *
 * After the reading, the input stream pointer is after the BER block, or
 * after the newline character after the -----END SOMETHING----- line.
 *
 * @param is the InputStream
 * @returns byte block or null if end of stream
 * @throws IOException If any parsing error
 */
private static byte[] readOneBlock(InputStream is) throws IOException {
    // The first character of a BLOCK.
    int c = is.read();
    if (c == -1) {
        return null;
    }
    if (c == DerValue.tag_Sequence) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream(2048);
        bout.write(c);
        readBERInternal(is, bout, c);
        return bout.toByteArray();
    } else {
        // Read BASE64 encoded data, might skip info at the beginning
        char[] data = new char[2048];
        int pos = 0;
        // Step 1: Read until header is found
        // count of consequent hyphens
        int hyphen = (c == '-') ? 1 : 0;
        // the char before hyphen
        int last = (c == '-') ? -1 : c;
        while (true) {
            int next = is.read();
            if (next == -1) {
                // say, empty lines.
                return null;
            }
            if (next == '-') {
                hyphen++;
            } else {
                hyphen = 0;
                last = next;
            }
            if (hyphen == 5 && (last == -1 || last == '\r' || last == '\n')) {
                break;
            }
        }
        // Step 2: Read the rest of header, determine the line end
        int end;
        StringBuffer header = new StringBuffer("-----");
        while (true) {
            int next = is.read();
            if (next == -1) {
                throw new IOException("Incomplete data");
            }
            if (next == '\n') {
                end = '\n';
                break;
            }
            if (next == '\r') {
                next = is.read();
                if (next == -1) {
                    throw new IOException("Incomplete data");
                }
                if (next == '\n') {
                    end = '\n';
                } else {
                    end = '\r';
                    data[pos++] = (char) next;
                }
                break;
            }
            header.append((char) next);
        }
        // Step 3: Read the data
        while (true) {
            int next = is.read();
            if (next == -1) {
                throw new IOException("Incomplete data");
            }
            if (next != '-') {
                data[pos++] = (char) next;
                if (pos >= data.length) {
                    data = Arrays.copyOf(data, data.length + 1024);
                }
            } else {
                break;
            }
        }
        // Step 4: Consume the footer
        StringBuffer footer = new StringBuffer("-");
        while (true) {
            int next = is.read();
            // is not consistent.
            if (next == -1 || next == end || next == '\n') {
                break;
            }
            if (next != '\r')
                footer.append((char) next);
        }
        checkHeaderFooter(header.toString(), footer.toString());
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(new String(data, 0, pos));
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BASE64Decoder(sun.misc.BASE64Decoder)

Example 17 with BASE64Decoder

use of sun.misc.BASE64Decoder in project sakuli by ConSol.

the class AesCbcCipher method decryptString.

public static String decryptString(String encryptedStringBase64, SecretKey aesKey) throws SakuliCipherException {
    if (StringUtils.isEmpty(encryptedStringBase64)) {
        throw new SakuliCipherException("Empty secret can not en-/decrypted!");
    }
    final byte[] ciphertext;
    try {
        ciphertext = new BASE64Decoder().decodeBuffer(encryptedStringBase64);
    } catch (IOException e) {
        throw new SakuliCipherException("Can not decrypt invalid Base64 secret: " + encryptedStringBase64);
    }
    final byte[] decrypted = decryptBytes(aesKey, ciphertext);
    return convertBytesToString(decrypted);
}
Also used : SakuliCipherException(org.sakuli.exceptions.SakuliCipherException) IOException(java.io.IOException) BASE64Decoder(sun.misc.BASE64Decoder)

Example 18 with BASE64Decoder

use of sun.misc.BASE64Decoder in project blade by biezhi.

the class Tools method deAes.

public static String deAes(String data, String key) throws Exception {
    Cipher cipher = Cipher.getInstance("AES");
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    byte[] cipherTextBytes = new BASE64Decoder().decodeBuffer(data);
    byte[] decValue = cipher.doFinal(cipherTextBytes);
    return new String(decValue);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) BASE64Decoder(sun.misc.BASE64Decoder)

Example 19 with BASE64Decoder

use of sun.misc.BASE64Decoder in project intellij-community by JetBrains.

the class IpnbImagePanel method createViewPanel.

@Override
protected JComponent createViewPanel() {
    final String png = myCell.getBase64String();
    final JBLabel label = new JBLabel();
    if (!StringUtil.isEmptyOrSpaces(png)) {
        try {
            byte[] btDataFile = new BASE64Decoder().decodeBuffer(png);
            BufferedImage image = ImageIO.read(new ByteArrayInputStream(btDataFile));
            label.setIcon(new ImageIcon(image));
        } catch (Exception e) {
            LOG.error("Couldn't parse image. " + e.getMessage());
        }
    }
    label.setBackground(IpnbEditorUtil.getBackground());
    label.setOpaque(true);
    return label;
}
Also used : JBLabel(com.intellij.ui.components.JBLabel) ByteArrayInputStream(java.io.ByteArrayInputStream) BASE64Decoder(sun.misc.BASE64Decoder) BufferedImage(java.awt.image.BufferedImage)

Example 20 with BASE64Decoder

use of sun.misc.BASE64Decoder in project jdk8u_jdk by JetBrains.

the class TestBase64Golden method test0.

public static void test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile) throws Exception {
    String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET).toArray(new String[0]);
    String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET).toArray(new String[0]);
    int lns = 0;
    for (String srcStr : srcLns) {
        String encodedStr = null;
        if (type != Base64Type.MIME) {
            encodedStr = encodedLns[lns++];
        } else {
            while (lns < encodedLns.length) {
                String s = encodedLns[lns++];
                if (s.length() == 0)
                    break;
                if (encodedStr != null) {
                    encodedStr += DEFAULT_CRLF + s;
                } else {
                    encodedStr = s;
                }
            }
            if (encodedStr == null && srcStr.length() == 0) {
                encodedStr = "";
            }
        }
        System.out.printf("%n    src[%d]: %s%n", srcStr.length(), srcStr);
        System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr);
        byte[] srcArr = srcStr.getBytes(DEF_CHARSET);
        byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET);
        ByteBuffer srcBuf = ByteBuffer.wrap(srcArr);
        ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr);
        byte[] resArr = new byte[encodedArr.length];
        // test int encode(byte[], byte[])
        int len = encoder.encode(srcArr, resArr);
        assertEqual(len, encodedArr.length);
        assertEqual(resArr, encodedArr);
        // test byte[] encode(byte[])
        resArr = encoder.encode(srcArr);
        assertEqual(resArr, encodedArr);
        // test ByteBuffer encode(ByteBuffer)
        int limit = srcBuf.limit();
        ByteBuffer resBuf = encoder.encode(srcBuf);
        assertEqual(srcBuf.position(), limit);
        assertEqual(srcBuf.limit(), limit);
        assertEqual(resBuf, encodedBuf);
        // reset for next test
        srcBuf.rewind();
        // test String encodeToString(byte[])
        String resEncodeStr = encoder.encodeToString(srcArr);
        assertEqual(resEncodeStr, encodedStr);
        // test int decode(byte[], byte[])
        resArr = new byte[srcArr.length];
        len = decoder.decode(encodedArr, resArr);
        assertEqual(len, srcArr.length);
        assertEqual(resArr, srcArr);
        // test byte[] decode(byte[])
        resArr = decoder.decode(encodedArr);
        assertEqual(resArr, srcArr);
        // test ByteBuffer decode(ByteBuffer)
        limit = encodedBuf.limit();
        resBuf = decoder.decode(encodedBuf);
        assertEqual(encodedBuf.position(), limit);
        assertEqual(encodedBuf.limit(), limit);
        assertEqual(resBuf, srcBuf);
        // reset for next test
        encodedBuf.rewind();
        // test byte[] decode(String)
        resArr = decoder.decode(encodedStr);
        assertEqual(resArr, srcArr);
        // test compatible with sun.misc.Base64Encoder
        if (type == Base64Type.MIME) {
            sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder();
            sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder();
            resArr = decoder.decode(miscEncoder.encode(srcArr));
            assertEqual(resArr, srcArr);
            resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr));
            assertEqual(new String(resArr, DEF_CHARSET), encodedStr);
        }
    }
}
Also used : BASE64Encoder(sun.misc.BASE64Encoder) BASE64Decoder(sun.misc.BASE64Decoder) BASE64Encoder(sun.misc.BASE64Encoder) ByteBuffer(java.nio.ByteBuffer) BASE64Decoder(sun.misc.BASE64Decoder)

Aggregations

BASE64Decoder (sun.misc.BASE64Decoder)44 IOException (java.io.IOException)23 ByteArrayInputStream (java.io.ByteArrayInputStream)6 FileOutputStream (java.io.FileOutputStream)6 HashMap (java.util.HashMap)6 OutputStream (java.io.OutputStream)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 JSONObject (com.alibaba.fastjson.JSONObject)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 InvalidKeyException (java.security.InvalidKeyException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 BASE64Encoder (sun.misc.BASE64Encoder)4 UserInfoServiceException (com.itrus.portal.exception.UserInfoServiceException)3 FileNotFoundException (java.io.FileNotFoundException)3 URL (java.net.URL)3 KeyFactory (java.security.KeyFactory)3 CertificateException (java.security.cert.CertificateException)3 Map (java.util.Map)3 Cipher (javax.crypto.Cipher)3