Refine search
/** * 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); }
/** * Like {@link #getBinary(String)} but specifying a default value to return if there is no entry. * * @param key the key to lookup * @param def the default value to use if the entry is not present * @return the value or {@code def} if no entry present */ public byte[] getBinary(String key, byte[] def) { Objects.requireNonNull(key); Object val = map.get(key); return val != null || map.containsKey(key) ? (val == null ? null : Base64.getDecoder().decode((String)val)) : def; }
@Override public Buffer binaryValue() { return value != null ? Buffer.buffer(Base64.getDecoder().decode((String) value)) : null; }
/** * @return the decoded base64 of the cookie or {@code null} if the value was not correctly encoded */ private @CheckForNull String decodeCookieBase64(@Nonnull String base64EncodedValue){ for (int j = 0; j < base64EncodedValue.length() % 4; j++) { base64EncodedValue = base64EncodedValue + "="; } try { // any charset should be fine but better safe than sorry byte[] decodedPlainValue = java.util.Base64.getDecoder().decode(base64EncodedValue.getBytes(StandardCharsets.UTF_8)); return new String(decodedPlainValue, StandardCharsets.UTF_8); } catch (IllegalArgumentException e) { return null; } }
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));
public static ScramCredential credentialFromString(String str) { Properties props = toProps(str); if (props.size() != 4 || !props.containsKey(SALT) || !props.containsKey(STORED_KEY) || !props.containsKey(SERVER_KEY) || !props.containsKey(ITERATIONS)) { throw new IllegalArgumentException("Credentials not valid: " + str); } byte[] salt = Base64.getDecoder().decode(props.getProperty(SALT)); byte[] storedKey = Base64.getDecoder().decode(props.getProperty(STORED_KEY)); byte[] serverKey = Base64.getDecoder().decode(props.getProperty(SERVER_KEY)); int iterations = Integer.parseInt(props.getProperty(ITERATIONS)); return new ScramCredential(salt, storedKey, serverKey, iterations); }
import java.util.Base64; byte[] bytes = "Hello, World!".getBytes("UTF-8"); String encoded = Base64.getEncoder().encodeToString(bytes); byte[] decoded = Base64.getDecoder().decode(encoded);
// decode the base64 encoded string byte[] decodedKey = Base64.getDecoder().decode(encodedKey); // rebuild key using SecretKeySpec SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
public static String decodeBase64ToString(String base64) { return new String(java.util.Base64.getDecoder().decode(base64.getBytes(UTF_8)), UTF_8); }
private String decodeBase64(String s) { try { return new String(Base64.getDecoder().decode(s), StandardCharsets.US_ASCII); } catch (IllegalArgumentException e) { return ""; } }
public static String decryptUsingAES(SecretKey secretKey, String dataToDecrypt) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { Cipher aesCipher = Cipher.getInstance("AES"); aesCipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] bytePlainText = aesCipher.doFinal(Base64.getDecoder().decode(dataToDecrypt)); return new String(bytePlainText); } }
public byte[] getDataAsBytes() { if (dataAsBytes == null) { dataAsBytes = Base64.getDecoder().decode(data); } return dataAsBytes; }
public Image(String contentType, String data, String hash) { this.contentType = contentType; this.data = data; this.hash = hash; this.dataAsBytes = Base64.getDecoder().decode(data); }
/** * 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); }
public ClientFinalMessage(byte[] messageBytes) throws SaslException { String message = toMessage(messageBytes); Matcher matcher = PATTERN.matcher(message); if (!matcher.matches()) throw new SaslException("Invalid SCRAM client final message format: " + message); this.channelBinding = Base64.getDecoder().decode(matcher.group("channel")); this.nonce = matcher.group("nonce"); this.proof = Base64.getDecoder().decode(matcher.group("proof")); } public ClientFinalMessage(byte[] channelBinding, String nonce) {
public static String decodeViewData(String data) { checkCondition(data.startsWith(VIEW_PREFIX), HIVE_INVALID_VIEW_DATA, "View data missing prefix: %s", data); checkCondition(data.endsWith(VIEW_SUFFIX), HIVE_INVALID_VIEW_DATA, "View data missing suffix: %s", data); data = data.substring(VIEW_PREFIX.length()); data = data.substring(0, data.length() - VIEW_SUFFIX.length()); return new String(Base64.getDecoder().decode(data), UTF_8); }
public static boolean verifyRSASignature(String subordinatePublicKeyContent, String signatureContent, String masterPublicKeyContent) throws NoSuchProviderException, NoSuchAlgorithmException, IOException, InvalidKeySpecException, InvalidKeyException, SignatureException { PublicKey masterPublicKey = getRSAPublicKeyFrom(masterPublicKeyContent); signatureContent = signatureContent.replace("\n", ""); Signature signature = Signature.getInstance("SHA512withRSA"); signature.initVerify(masterPublicKey); signature.update(subordinatePublicKeyContent.getBytes()); return signature.verify(Base64.getDecoder().decode(signatureContent.getBytes())); }
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); } }
public static String decryptUsingRSA(String cipherText, String privateKeyContent) throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, InvalidKeySpecException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException { Cipher decryptCipher = Cipher.getInstance("RSA"); decryptCipher.init(Cipher.DECRYPT_MODE, getRSAPrivateKeyFrom(privateKeyContent)); return new String(decryptCipher.doFinal(Base64.getDecoder().decode(cipherText)), UTF_8); }
@Override public byte[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { String text = p.getText(); try { return Base64.getDecoder().decode(text); } catch (IllegalArgumentException e) { throw new InvalidFormatException(p, "Expected a base64 encoded byte array", text, Instant.class); } } }