public static Work decodeWork(String data) { try { byte[] binary = Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8)); try (ObjectInputStream objectStream = new ObjectInputStream(new ByteArrayInputStream(binary))) { return (Work) objectStream.readObject(); } } catch (ClassNotFoundException | IOException e) { throw bomb(e); } }
byte[] der = Base64.getMimeDecoder().decode(base64); try { certs.add((X509Certificate) cf.generateCertificate(new ByteArrayInputStream(der))); } catch (Exception e) { throw new PkiException("Failed to read certificate from bytes", e);
alg = json.getString("alg", "RS256"); final BigInteger n = new BigInteger(1, Base64.getUrlDecoder().decode(json.getString("n"))); final BigInteger e = new BigInteger(1, Base64.getUrlDecoder().decode(json.getString("e"))); publicKey = KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(n, e)); final BigInteger n = new BigInteger(1, Base64.getUrlDecoder().decode(json.getString("n"))); final BigInteger e = new BigInteger(1, Base64.getUrlDecoder().decode(json.getString("e"))); final BigInteger d = new BigInteger(1, Base64.getUrlDecoder().decode(json.getString("d"))); final BigInteger p = new BigInteger(1, Base64.getUrlDecoder().decode(json.getString("p"))); final BigInteger q = new BigInteger(1, Base64.getUrlDecoder().decode(json.getString("q"))); final BigInteger dp = new BigInteger(1, Base64.getUrlDecoder().decode(json.getString("dp"))); final BigInteger dq = new BigInteger(1, Base64.getUrlDecoder().decode(json.getString("dq"))); final BigInteger qi = new BigInteger(1, Base64.getUrlDecoder().decode(json.getString("qi"))); JsonArray x5c = json.getJsonArray("x5c"); if (x5c.size() > 1) { certificate = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(addBoundaries(x5c.getString(0)).getBytes(UTF8)));
byte [] data = Base64.getDecoder().decode( s ); ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( data ) ); Object o = ois.readObject(); ois.close(); oos.writeObject( o ); oos.close(); return Base64.getEncoder().encodeToString(baos.toByteArray());
private static Object decodeObject(String expectedBase64Form) { try { byte[] bytes = DECODER.decode(expectedBase64Form); return new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject(); } catch (IOException | ClassNotFoundException e) { throw new AssertionError(e); } }
@Override public ServletInputStream getInputStream() throws IOException { if (request.getBody() == null) { return new AwsServletInputStream(new NullInputStream(0, false, false)); } byte[] bodyBytes = null; if (request.isBase64Encoded()) { bodyBytes = Base64.getMimeDecoder().decode(request.getBody()); } else { String encoding = getCharacterEncoding(); if (encoding == null) { encoding = StandardCharsets.ISO_8859_1.name(); } try { bodyBytes = request.getBody().getBytes(encoding); } catch (Exception e) { log.error("Could not read request with character encoding: " + SecurityUtils.crlf(encoding), e); bodyBytes = request.getBody().getBytes(StandardCharsets.ISO_8859_1.name()); } } ByteArrayInputStream requestBodyStream = new ByteArrayInputStream(bodyBytes); return new AwsServletInputStream(requestBodyStream); }
ByteArrayInputStream inputBytes = new ByteArrayInputStream(dataBytes); try (DataInputStream input = new DataInputStream(inputBytes)) { ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(CharBuffer.wrap(chars)); bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); Arrays.fill(byteBuffer.array(), (byte)0); tmpBytes[i] = (byte)chars[i]; // PBE only stores the lower 8 bits, so this narrowing is ok bytes = Base64.getDecoder().decode(tmpBytes); Arrays.fill(tmpBytes, (byte)0);
final String authorization = httpRequest.getHeader("Authorization"); if (authorization != null && authorization.startsWith("Basic")) { // Authorization: Basic base64credentials String base64Credentials = authorization.substring("Basic".length()).trim(); String credentials = new String(Base64.getDecoder().decode(base64Credentials), Charset.forName("UTF-8")); // credentials = username:password final String[] values = credentials.split(":",2);
byte[] message = "hello world".getBytes(StandardCharsets.UTF_8); String encoded = Base64.getEncoder().encodeToString(message); byte[] decoded = Base64.getDecoder().decode(encoded); System.out.println(encoded); System.out.println(new String(decoded, StandardCharsets.UTF_8));
private static final java.util.Base64.Decoder BASE_64_DECODER= new java.util.Base64.Decoder(); private static final String ENCODING = "UTF-8";// Use the correct encoding here. private byte[] decodePassword(char[] password) { CharBuffer charBuffer = CharBuffer.wrap(password); ByteBuffer byteBuffer = Charset.forName(ENCODING).encode(charBuffer); return BASE_64_DECODER.decode(byteBuffer); }
private static String decompress(String s) throws UnsupportedEncodingException { return new String( decompressAlgorithm( Base64.getUrlDecoder().decode( URLDecoder.decode(s, ENCODING.name()) .getBytes(ENCODING) ) ), ENCODING); }
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { String origin = request.getParameter("state"); URL raw = new URL( new String(Base64.getUrlDecoder().decode(origin.getBytes(Charset.forName("UTF-8"))), Charset.forName("UTF-8"))); response.sendRedirect(raw.toString()); } }
public PlistPartition[] getPartitions() throws IOException { LinkedList<PlistPartition> partitionList = new LinkedList<PlistPartition>(); PlistNode current = getRootNode(); current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); // Variables to keep track of the pointers of the previous partition long previousOutOffset = 0; long previousInOffset = 0; // Iterate over the partitions and gather data for (PlistNode pn : current.getChildren()) { String partitionName = io.takari.jdkget.osx.util.Util.readFully(pn.getKeyValue("Name")); String partitionID = io.takari.jdkget.osx.util.Util.readFully(pn.getKeyValue("ID")); String partitionAttributes = io.takari.jdkget.osx.util.Util.readFully(pn.getKeyValue("Attributes")); Reader base64Data = pn.getKeyValue("Data"); InputStream base64DataInputStream = Base64.getMimeDecoder().wrap(new ReaderInputStream(base64Data, Charset.forName("US-ASCII"))); PlistPartition dpp = new PlistPartition(partitionName, partitionID, partitionAttributes, base64DataInputStream, previousOutOffset, previousInOffset); previousOutOffset = dpp.getFinalOutOffset(); previousInOffset = dpp.getFinalInOffset(); partitionList.addLast(dpp); } return partitionList.toArray(new PlistPartition[partitionList.size()]); } }
try { final String encodedUserPass = authorizationHeader.substring(basic.length()); decodedUserPass = decoder.decode(encodedUserPass); } catch (IndexOutOfBoundsException iae) { sendError(ctx, msg); return; final String authorization = new String(decodedUserPass, Charset.forName("UTF-8")); final String[] split = authorization.split(":"); if (split.length != 2) {
@Test public void testGetBinary() { byte[] bytes = TestUtils.randomByteArray(100); jsonObject.put("foo", bytes); assertArrayEquals(bytes, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo")); String val = jsonObject.getString("foo"); assertNotNull(val); byte[] retrieved = Base64.getDecoder().decode(val); assertTrue(TestUtils.byteArraysEqual(bytes, retrieved));
@Test public void testFromJsonObject() { JsonObject object = new JsonObject(); object.put("string", "the_string"); object.put("integer", 4); object.put("boolean", true); object.put("binary", "hello".getBytes()); object.put("object", new JsonObject().put("nested", 4)); object.put("array", new JsonArray().add(1).add(2).add(3)); Map<String, Object> map = ConversionHelper.fromObject(object); assertEquals(6, map.size()); assertEquals("the_string", map.get("string")); assertEquals(4, map.get("integer")); assertEquals(true, map.get("boolean")); assertEquals("hello", new String(Base64.getDecoder().decode((String)map.get("binary")))); assertEquals(Collections.singletonMap("nested", 4), map.get("object")); assertEquals(Arrays.asList(1, 2, 3), map.get("array")); }
/** * Base64-decode the given byte array. * @param src the encoded byte array * @return the original byte array */ public static byte[] decode(byte[] src) { if (src.length == 0) { return src; } return Base64.getDecoder().decode(src); }
/** * Scans the given file line-by-line (ignoring empty lines) and returns a list containing those * lines. If decode is set to true, every line is decoded using {@link Base64} from the UTF-8 * bytes of that line before inserting in the list. * * @param filename * Path to the file that needs to be scanned * @param decode * Whether to decode lines in the file * @return List of {@link Text} objects containing data in the given file * @throws FileNotFoundException * if the given file doesn't exist */ @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "app is run in same security context as user providing the filename") public static List<Text> scanFile(String filename, boolean decode) throws FileNotFoundException { String line; List<Text> result = new ArrayList<>(); try (Scanner file = new Scanner(new File(filename), UTF_8.name())) { while (file.hasNextLine()) { line = file.nextLine(); if (!line.isEmpty()) { result.add(decode ? new Text(Base64.getDecoder().decode(line)) : new Text(line)); } } } return result; }
@Test public void testGetValue() { jsonObject.put("foo", 123); assertEquals(123, jsonObject.getValue("foo")); jsonObject.put("foo", 123l); assertEquals(123l, jsonObject.getValue("foo")); jsonObject.put("foo", 123f); jsonObject.put("foo", obj); assertEquals(obj, jsonObject.getValue("foo")); JsonArray arr = new JsonArray().add("blah").add("wibble"); jsonObject.put("foo", arr); assertEquals(arr, jsonObject.getValue("foo")); byte[] bytes = TestUtils.randomByteArray(100); jsonObject.put("foo", bytes); assertTrue(TestUtils.byteArraysEqual(bytes, Base64.getDecoder().decode((String) jsonObject.getValue("foo")))); jsonObject.putNull("foo"); assertNull(jsonObject.getValue("foo"));
AggregatedDataObject aggregatedDataObject = new AggregatedDataObject().setValue(TestUtils.randomAlphaString(20)); Buffer buffer = TestUtils.randomBuffer(20); JsonObject jsonObject = new JsonObject().put("wibble", TestUtils.randomAlphaString(20)); JsonArray jsonArray = new JsonArray().add(TestUtils.randomAlphaString(20)); TimeUnit httpMethod = TimeUnit.values()[TestUtils.randomPositiveInt() % TimeUnit.values().length]; Map<String, Object> map = new HashMap<>(); assertEquals(Character.toString(boxedCharValue), json.get("boxedCharValue")); assertEquals(aggregatedDataObject.toJson(), json.get("aggregatedDataObject")); assertEquals(buffer, Buffer.buffer(Base64.getDecoder().decode((String)json.get("buffer")))); assertEquals(jsonObject, json.get("jsonObject")); assertEquals(jsonArray, json.get("jsonArray")); assertEquals(httpMethod.name(), json.get("httpMethod")); assertEquals(new JsonArray().add(stringValue), json.get("stringValues")); assertEquals(Base64.getEncoder().encodeToString(buffer.getBytes()), ((JsonArray)json.get("buffers")).getValue(0)); assertEquals(Base64.getEncoder().encodeToString(buffer.getBytes()), ((JsonArray)json.get("bufferSet")).getValue(0)); assertEquals(new JsonObject().put(key, stringValue), json.get("stringValueMap")); assertEquals(Base64.getEncoder().encodeToString(buffer.getBytes()), ((JsonObject)json.get("bufferMap")).getValue(key));