Java Code Examples for java.util.Base64#getDecoder()

The following examples show how to use java.util.Base64#getDecoder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBase64.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void  testDecodeUnpadded() throws Throwable {
    byte[] srcA = new byte[] { 'Q', 'Q' };
    byte[] srcAA = new byte[] { 'Q', 'Q', 'E'};
    Base64.Decoder dec = Base64.getDecoder();
    byte[] ret = dec.decode(srcA);
    if (ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A failed");
    ret = dec.decode(srcAA);
    if (ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA failed");
    ret = new byte[10];
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 &&
        ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A from stream failed");
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 &&
        ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA from stream failed");
}
 
Example 2
Source File: TestBase64.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static void  testDecodeUnpadded() throws Throwable {
    byte[] srcA = new byte[] { 'Q', 'Q' };
    byte[] srcAA = new byte[] { 'Q', 'Q', 'E'};
    Base64.Decoder dec = Base64.getDecoder();
    byte[] ret = dec.decode(srcA);
    if (ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A failed");
    ret = dec.decode(srcAA);
    if (ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA failed");
    ret = new byte[10];
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 &&
        ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A from stream failed");
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 &&
        ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA from stream failed");
}
 
Example 3
Source File: TestBase64.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static void  testDecodeUnpadded() throws Throwable {
    byte[] srcA = new byte[] { 'Q', 'Q' };
    byte[] srcAA = new byte[] { 'Q', 'Q', 'E'};
    Base64.Decoder dec = Base64.getDecoder();
    byte[] ret = dec.decode(srcA);
    if (ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A failed");
    ret = dec.decode(srcAA);
    if (ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA failed");
    ret = new byte[10];
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 &&
        ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A from stream failed");
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 &&
        ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA from stream failed");
}
 
Example 4
Source File: JwtUtil.java    From cubeai with Apache License 2.0 6 votes vote down vote up
public static String getUserRoles(HttpServletRequest httpServletRequest) {
    String authorization = httpServletRequest.getHeader("authorization");
    if (null == authorization) {
        // 请求头中没有携带JWT,表示非登录用户
        return null;
    }

    String jwt = authorization.substring(7); // 去除前缀“bearer ”
    String payloadBase64 = jwt.substring(jwt.indexOf(".") + 1, jwt.lastIndexOf(".")); // 取出JWT中第二部分
    Base64.Decoder decoder = Base64.getDecoder();
    String payloadString;
    try {
        payloadString = new String(decoder.decode(payloadBase64), "UTF-8");
    } catch (Exception e) {
        return null;
    }
    JSONObject payloadJson = JSONObject.parseObject(payloadString);

    return payloadJson.getString("authorities");
}
 
Example 5
Source File: APIExportConfigAdapter.java    From apimanager-swagger-promote with Apache License 2.0 6 votes vote down vote up
private void storeCaCerts(File localFolder, List<CaCert> caCerts) throws AppException {
	for(CaCert caCert : caCerts) {
		if (caCert.getCertBlob() == null) {
			LOG.warn("- Ignoring cert export for null certBlob for alias: {}", caCert.getAlias());
		} else {
			String filename = caCert.getCertFile();
			Base64.Encoder encoder = Base64.getMimeEncoder(64, System.getProperty("line.separator").getBytes());
			Base64.Decoder decoder = Base64.getDecoder();
			final String encodedCertText = new String(encoder.encode(decoder.decode(caCert.getCertBlob())));
			byte[] certContent = ("-----BEGIN CERTIFICATE-----\n" + encodedCertText + "\n-----END CERTIFICATE-----").getBytes();
			try {
				writeBytesToFile(certContent, localFolder + "/" + filename);
			} catch (AppException e) {
				throw new AppException("Can't write certificate to disc", ErrorCode.UNXPECTED_ERROR, e);
			}
		}
	}
}
 
Example 6
Source File: Base64Util.java    From gpmall with Apache License 2.0 6 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
    try {
        final Base64.Decoder decoder = Base64.getDecoder();
        final Base64.Encoder encoder = Base64.getEncoder();
        final String text = "字串文字";
        final byte[] textByte = text.getBytes("UTF-8");
        //编码
        final String encodedText = encoder.encodeToString(textByte);
        System.out.println(encodedText);
        //解码
        System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example 7
Source File: TestBase64.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void  testDecodeUnpadded() throws Throwable {
    byte[] srcA = new byte[] { 'Q', 'Q' };
    byte[] srcAA = new byte[] { 'Q', 'Q', 'E'};
    Base64.Decoder dec = Base64.getDecoder();
    byte[] ret = dec.decode(srcA);
    if (ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A failed");
    ret = dec.decode(srcAA);
    if (ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA failed");
    ret = new byte[10];
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 &&
        ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A from stream failed");
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 &&
        ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA from stream failed");
}
 
Example 8
Source File: TestBase64.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void  testDecodeUnpadded() throws Throwable {
    byte[] srcA = new byte[] { 'Q', 'Q' };
    byte[] srcAA = new byte[] { 'Q', 'Q', 'E'};
    Base64.Decoder dec = Base64.getDecoder();
    byte[] ret = dec.decode(srcA);
    if (ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A failed");
    ret = dec.decode(srcAA);
    if (ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA failed");
    ret = new byte[10];
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 &&
        ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A from stream failed");
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 &&
        ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA from stream failed");
}
 
Example 9
Source File: TestBase64.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void  testDecodeUnpadded() throws Throwable {
    byte[] srcA = new byte[] { 'Q', 'Q' };
    byte[] srcAA = new byte[] { 'Q', 'Q', 'E'};
    Base64.Decoder dec = Base64.getDecoder();
    byte[] ret = dec.decode(srcA);
    if (ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A failed");
    ret = dec.decode(srcAA);
    if (ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA failed");
    ret = new byte[10];
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 &&
        ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A from stream failed");
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 &&
        ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA from stream failed");
}
 
Example 10
Source File: TestBase64.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static void  testDecodeUnpadded() throws Throwable {
    byte[] srcA = new byte[] { 'Q', 'Q' };
    byte[] srcAA = new byte[] { 'Q', 'Q', 'E'};
    Base64.Decoder dec = Base64.getDecoder();
    byte[] ret = dec.decode(srcA);
    if (ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A failed");
    ret = dec.decode(srcAA);
    if (ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA failed");
    ret = new byte[10];
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 &&
        ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A from stream failed");
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 &&
        ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA from stream failed");
}
 
Example 11
Source File: Base64DecodeMultipartFile.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
public static MultipartFile base64Convert(String base64) {

        String[] baseStrs = base64.split(",");
        Decoder decoder = Base64.getDecoder();
        byte[] b = decoder.decode(baseStrs[1]);

        for (int i = 0; i < b.length; ++i) {
            if (b[i] < 0) {
                b[i] += 256;
            }
        }
        return new Base64DecodeMultipartFile(b, baseStrs[0]);
    }
 
Example 12
Source File: TestRabbitMQPasswordConverter.java    From rabbitmq-operator with Apache License 2.0 5 votes vote down vote up
@BeforeEach
private void setup() throws NoSuchAlgorithmException {
    final Random myRandom = new Random() {
        @Override
        public int nextInt() {
            return SALT;
        }
    };

    converter = new RabbitMQPasswordConverter(myRandom, MessageDigest.getInstance("SHA-256"), Base64.getEncoder(), Base64.getDecoder());
}
 
Example 13
Source File: TestBase64.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testDecodeIgnoredAfterPadding() throws Throwable {
    for (byte nonBase64 : new byte[] {'#', '(', '!', '\\', '-', '_', '\n', '\r'}) {
        byte[][] src = new byte[][] {
            "A".getBytes("ascii"),
            "AB".getBytes("ascii"),
            "ABC".getBytes("ascii"),
            "ABCD".getBytes("ascii"),
            "ABCDE".getBytes("ascii")
        };
        Base64.Encoder encM = Base64.getMimeEncoder();
        Base64.Decoder decM = Base64.getMimeDecoder();
        Base64.Encoder enc = Base64.getEncoder();
        Base64.Decoder dec = Base64.getDecoder();
        for (int i = 0; i < src.length; i++) {
            // decode(byte[])
            byte[] encoded = encM.encode(src[i]);
            encoded = Arrays.copyOf(encoded, encoded.length + 1);
            encoded[encoded.length - 1] = nonBase64;
            checkEqual(decM.decode(encoded), src[i], "Non-base64 char is not ignored");
            byte[] decoded = new byte[src[i].length];
            decM.decode(encoded, decoded);
            checkEqual(decoded, src[i], "Non-base64 char is not ignored");

            try {
                dec.decode(encoded);
                throw new RuntimeException("No IAE for non-base64 char");
            } catch (IllegalArgumentException iae) {}
        }
    }
}
 
Example 14
Source File: EncryptionTool.java    From TAC with MIT License 5 votes vote down vote up
/**
 * BASE64解密
 *
 * @param key
 * @return
 * @throws Exception
 */
public static String decryptBASE64(String key) {
    Base64.Decoder decoder = Base64.getDecoder();
    String result=null;
    try {
        result = new String(decoder.decode(key), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 15
Source File: TestBase64.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static void testMalformedPadding() throws Throwable {
    Object[] data = new Object[] {
        "$=#",       "",      0,      // illegal ending unit
        "A",         "",      0,      // dangling single byte
        "A=",        "",      0,
        "A==",       "",      0,
        "QUJDA",     "ABC",   4,
        "QUJDA=",    "ABC",   4,
        "QUJDA==",   "ABC",   4,

        "=",         "",      0,      // unnecessary padding
        "QUJD=",     "ABC",   4,      //"ABC".encode() -> "QUJD"

        "AA=",       "",      0,      // incomplete padding
        "QQ=",       "",      0,
        "QQ=N",      "",      0,      // incorrect padding
        "QQ=?",      "",      0,
        "QUJDQQ=",   "ABC",   4,
        "QUJDQQ=N",  "ABC",   4,
        "QUJDQQ=?",  "ABC",   4,
    };

    Base64.Decoder[] decs = new Base64.Decoder[] {
        Base64.getDecoder(),
        Base64.getUrlDecoder(),
        Base64.getMimeDecoder()
    };

    for (Base64.Decoder dec : decs) {
        for (int i = 0; i < data.length; i += 3) {
            final String srcStr = (String)data[i];
            final byte[] srcBytes = srcStr.getBytes("ASCII");
            final ByteBuffer srcBB = ByteBuffer.wrap(srcBytes);
            byte[] expected = ((String)data[i + 1]).getBytes("ASCII");
            int pos = (Integer)data[i + 2];

            // decode(byte[])
            checkIAE(new Runnable() { public void run() { dec.decode(srcBytes); }});

            // decode(String)
            checkIAE(new Runnable() { public void run() { dec.decode(srcStr); }});

            // decode(ByteBuffer)
            checkIAE(new Runnable() { public void run() { dec.decode(srcBB); }});

            // wrap stream
            checkIOE(new Testable() {
                public void test() throws IOException {
                    try (InputStream is = dec.wrap(new ByteArrayInputStream(srcBytes))) {
                        while (is.read() != -1);
                    }
            }});
        }
    }
}
 
Example 16
Source File: TestBase64.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void testMalformedPadding() throws Throwable {
    Object[] data = new Object[] {
        "$=#",       "",      0,      // illegal ending unit
        "A",         "",      0,      // dangling single byte
        "A=",        "",      0,
        "A==",       "",      0,
        "QUJDA",     "ABC",   4,
        "QUJDA=",    "ABC",   4,
        "QUJDA==",   "ABC",   4,

        "=",         "",      0,      // unnecessary padding
        "QUJD=",     "ABC",   4,      //"ABC".encode() -> "QUJD"

        "AA=",       "",      0,      // incomplete padding
        "QQ=",       "",      0,
        "QQ=N",      "",      0,      // incorrect padding
        "QQ=?",      "",      0,
        "QUJDQQ=",   "ABC",   4,
        "QUJDQQ=N",  "ABC",   4,
        "QUJDQQ=?",  "ABC",   4,
    };

    Base64.Decoder[] decs = new Base64.Decoder[] {
        Base64.getDecoder(),
        Base64.getUrlDecoder(),
        Base64.getMimeDecoder()
    };

    for (Base64.Decoder dec : decs) {
        for (int i = 0; i < data.length; i += 3) {
            final String srcStr = (String)data[i];
            final byte[] srcBytes = srcStr.getBytes("ASCII");
            final ByteBuffer srcBB = ByteBuffer.wrap(srcBytes);
            byte[] expected = ((String)data[i + 1]).getBytes("ASCII");
            int pos = (Integer)data[i + 2];

            // decode(byte[])
            checkIAE(() -> dec.decode(srcBytes));

            // decode(String)
            checkIAE(() -> dec.decode(srcStr));

            // decode(ByteBuffer)
            checkIAE(() -> dec.decode(srcBB));

            // wrap stream
            checkIOE(new Testable() {
                public void test() throws IOException {
                    try (InputStream is = dec.wrap(new ByteArrayInputStream(srcBytes))) {
                        while (is.read() != -1);
                    }
            }});
        }
    }
}
 
Example 17
Source File: Base64Util.java    From gpmall with Apache License 2.0 4 votes vote down vote up
public static byte[] decode(String encodedText){
    final Base64.Decoder decoder = Base64.getDecoder();
    return decoder.decode(encodedText);
}
 
Example 18
Source File: RabbitConfig.java    From rabbitmq-operator with Apache License 2.0 4 votes vote down vote up
@Bean
public RabbitMQPasswordConverter passwordConverter() throws NoSuchAlgorithmException {
    return new RabbitMQPasswordConverter(new Random(), MessageDigest.getInstance("SHA-256"), Base64.getEncoder(), Base64.getDecoder());
}
 
Example 19
Source File: HFCAClientIT.java    From fabric-sdk-java with Apache License 2.0 3 votes vote down vote up
TBSCertList.CRLEntry[] parseCRL(String crl) throws Exception {

        Base64.Decoder b64dec = Base64.getDecoder();
        final byte[] decode = b64dec.decode(crl.getBytes(UTF_8));

        PEMParser pem = new PEMParser(new StringReader(new String(decode)));
        X509CRLHolder holder = (X509CRLHolder) pem.readObject();

        return holder.toASN1Structure().getRevokedCertificates();
    }
 
Example 20
Source File: Base64Bytes.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Ctor uses a RFC4648 {@link java.util.Base64.Decoder}.
 *
 * @param origin Origin bytes
 */
public Base64Bytes(final Bytes origin) {
    this(origin, Base64.getDecoder());
}