Java Code Examples for java.util.Base64.Decoder#decode()

The following examples show how to use java.util.Base64.Decoder#decode() . 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: TestBase64Golden.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void test1() throws Exception {
    byte[] src = new byte[] {
        46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
        40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
        -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
    Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
    byte[] encoded = encoder.encode(src);
    Decoder decoder = Base64.getMimeDecoder();
    byte[] decoded = decoder.decode(encoded);
    if (!Objects.deepEquals(src, decoded)) {
        throw new RuntimeException();
    }
}
 
Example 2
Source File: TestBase64Golden.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void test1() throws Exception {
    byte[] src = new byte[] {
        46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
        40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
        -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
    Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
    byte[] encoded = encoder.encode(src);
    Decoder decoder = Base64.getMimeDecoder();
    byte[] decoded = decoder.decode(encoded);
    if (!Objects.deepEquals(src, decoded)) {
        throw new RuntimeException();
    }
}
 
Example 3
Source File: TestBase64Golden.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void test1() throws Exception {
    byte[] src = new byte[] {
        46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
        40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
        -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
    Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
    byte[] encoded = encoder.encode(src);
    Decoder decoder = Base64.getMimeDecoder();
    byte[] decoded = decoder.decode(encoded);
    if (!Objects.deepEquals(src, decoded)) {
        throw new RuntimeException();
    }
}
 
Example 4
Source File: TestBase64Golden.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void test1() throws Exception {
    byte[] src = new byte[] {
        46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
        40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
        -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
    Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
    byte[] encoded = encoder.encode(src);
    Decoder decoder = Base64.getMimeDecoder();
    byte[] decoded = decoder.decode(encoded);
    if (!Objects.deepEquals(src, decoded)) {
        throw new RuntimeException();
    }
}
 
Example 5
Source File: SampleIDs.java    From browserprint with MIT License 5 votes vote down vote up
/**
 * Decrypt an integer from a String.
 * 
 * @param encrypted
 * @param context
 * @return
 * @throws ServletException
 */
private static Integer decryptInteger(String encrypted, ServletContext context) throws ServletException {
	String encryptedParts[] = encrypted.split("\\|");
	if (encryptedParts.length != 3) {
		throw new ServletException("Invalid encrypted string.");
	}
	/* Get password. */
	String password = context.getInitParameter("SampleSetIDEncryptionPassword");

	/* Extract the encrypted data, initialisation vector, and salt from the cookie. */
	Decoder decoder = Base64.getDecoder();
	byte ciphertext[] = decoder.decode(encryptedParts[0]);
	byte iv[] = decoder.decode(encryptedParts[1]);
	byte salt[] = decoder.decode(encryptedParts[2]);
	byte plainbytes[];
	try {
		/* Derive the key, given password and salt. */
		SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
		KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
		SecretKey tmp = factory.generateSecret(spec);
		SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

		/* Decrypt the message, given derived key and initialization vector. */
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
		plainbytes = cipher.doFinal(ciphertext);
	} catch (Exception ex) {
		throw new ServletException(ex);
	}
	return ByteBuffer.wrap(plainbytes).asIntBuffer().get();
}
 
Example 6
Source File: Encryption.java    From browserprint with MIT License 5 votes vote down vote up
/**
 * Decrypt an array of integers from a String.
 * 
 * @param encrypted
 * @param context
 * @return
 * @throws ServletException
 */
public static int[] decryptIntegers(String encrypted, String password) throws ServletException {
	String encryptedParts[] = encrypted.split("\\|");
	if (encryptedParts.length != 3) {
		throw new ServletException("Invalid encrypted string.");
	}

	/* Extract the encrypted data, initialisation vector, and salt from the cookie. */
	Decoder decoder = Base64.getDecoder();
	byte ciphertext[] = decoder.decode(encryptedParts[0]);
	byte iv[] = decoder.decode(encryptedParts[1]);
	byte salt[] = decoder.decode(encryptedParts[2]);
	byte plainbytes[];
	try {
		/* Derive the key, given password and salt. */
		SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
		KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
		SecretKey tmp = factory.generateSecret(spec);
		SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

		/* Decrypt the message, given derived key and initialization vector. */
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
		plainbytes = cipher.doFinal(ciphertext);
	} catch (Exception ex) {
		throw new ServletException(ex);
	}
	IntBuffer buff = ByteBuffer.wrap(plainbytes).asIntBuffer();
	int integers[] = new int[buff.remaining()];
	for (int i = 0; i < integers.length; ++i) {
		integers[i] = buff.get();
	}
	return integers;
}
 
Example 7
Source File: MDSJwtVerifier.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<Certificate> getCertificatesFromJsonArray(JsonArray x5c) throws CertificateException, NoSuchProviderException{
    List<Certificate> result = new ArrayList<>();
    if(x5c == null){
        return result;
    }
    Decoder decoder = Base64.getDecoder();
    for(int i = 0; i < x5c.size(); i++){
        byte[] certBytes = decoder.decode(x5c.getString(i));
        result.add(cryptoCommon.generateX509FromBytes(certBytes));
    }
    return result;
}
 
Example 8
Source File: TestBase64Golden.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void test1() throws Exception {
    byte[] src = new byte[] {
        46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
        40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
        -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
    Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
    byte[] encoded = encoder.encode(src);
    Decoder decoder = Base64.getMimeDecoder();
    byte[] decoded = decoder.decode(encoded);
    if (!Objects.deepEquals(src, decoded)) {
        throw new RuntimeException();
    }
}
 
Example 9
Source File: TestBase64Golden.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
private static void test1() throws Exception {
    byte[] src = new byte[] {
        46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
        40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
        -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
    Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
    byte[] encoded = encoder.encode(src);
    Decoder decoder = Base64.getMimeDecoder();
    byte[] decoded = decoder.decode(encoded);
    if (!Objects.deepEquals(src, decoded)) {
        throw new RuntimeException();
    }
}
 
Example 10
Source File: AESCipher.java    From AESCipher-Java with MIT License 5 votes vote down vote up
public static String aesDecryptString(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
	Decoder decoder = Base64.getDecoder();
    byte[] encryptedBytes = decoder.decode(content);
    byte[] keyBytes = key.getBytes(charset);
	byte[] decryptedBytes = aesDecryptBytes(encryptedBytes, keyBytes);
    return new String(decryptedBytes, charset);		
}
 
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: TestBase64Golden.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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);
        srcBuf.rewind(); // reset for next test

        // 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);
        encodedBuf.rewind(); // reset for next test

        // 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);
        }
    }
}
 
Example 13
Source File: TestBase64Golden.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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);
        srcBuf.rewind(); // reset for next test

        // 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);
        encodedBuf.rewind(); // reset for next test

        // 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);
        }
    }
}
 
Example 14
Source File: TestBase64Golden.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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);
        srcBuf.rewind(); // reset for next test

        // 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);
        encodedBuf.rewind(); // reset for next test

        // 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);
        }
    }
}
 
Example 15
Source File: TestBase64Golden.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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);
        srcBuf.rewind(); // reset for next test

        // 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);
        encodedBuf.rewind(); // reset for next test

        // 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);
        }
    }
}
 
Example 16
Source File: TokenFactory.java    From multiapps-controller with Apache License 2.0 4 votes vote down vote up
private String decode(String string) {
    Decoder decoder = Base64.getUrlDecoder();
    return new String(decoder.decode(string), StandardCharsets.UTF_8);
}
 
Example 17
Source File: TestBase64Golden.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
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);
        srcBuf.rewind(); // reset for next test

        // 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);
        encodedBuf.rewind(); // reset for next test

        // 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);
        }
    }
}
 
Example 18
Source File: TestBase64Golden.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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);
        srcBuf.rewind(); // reset for next test

        // 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);
        encodedBuf.rewind(); // reset for next test

        // 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);
        }
    }
}
 
Example 19
Source File: TestBase64Golden.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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);
        srcBuf.rewind(); // reset for next test

        // 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);
        encodedBuf.rewind(); // reset for next test

        // test byte[] decode(String)
        resArr = decoder.decode(encodedStr);
        assertEqual(resArr, srcArr);

    }
}
 
Example 20
Source File: TestBase64Golden.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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);
        srcBuf.rewind(); // reset for next test

        // 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);
        encodedBuf.rewind(); // reset for next test

        // 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);
        }
    }
}