/** * Decodes data from Base64 notation, automatically * detecting gzip-compressed data and decompressing it. * * @param s the string to decode * @return the decoded data * @throws java.io.IOException If there is a problem * @since 1.4 */ public static byte[] decode( String s ) throws java.io.IOException { return decode( s, NO_OPTIONS ); }
/** * Low-level access to decoding ASCII characters in * the form of a byte array. <strong>Ignores GUNZIP option, if * it's set.</strong> This is not generally a recommended method, * although it is used internally as part of the decoding process. * Special case: if len = 0, an empty array is returned. Still, * if you need more speed and reduced memory footprint (and aren't * gzipping), consider this method. * * @param source The Base64 encoded data * @return decoded data * @since 2.3.1 */ public static byte[] decode( byte[] source ) throws java.io.IOException { byte[] decoded = null; // try { decoded = decode( source, 0, source.length, Base64.NO_OPTIONS ); // } catch( java.io.IOException ex ) { // assert false : "IOExceptions only come from GZipping, which is turned off: " + ex.getMessage(); // } return decoded; }
private static byte[] decodeBase64(String base64String) { base64String = base64String.replaceAll("\\s",""); if(!base64String.matches("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$")) { throw new IllegalArgumentException("Cannot convert string '"+ base64String+ "'to a byte[] - it does not appear to be base64 data"); } try { return Base64.decode(base64String); } catch (IOException e) { throw new IllegalArgumentException(e); } }
private static byte[] pemToDer(String pem) throws IOException { pem = removeBeginEnd(pem); return Base64.decode(pem); }
public static String[] parseHeader(String header) { if (header.length() < 6) return null; String type = header.substring(0, 5); type = type.toLowerCase(); if (!type.equalsIgnoreCase("Basic")) return null; String val = header.substring(6); try { val = new String(Base64.decode(val.getBytes())); } catch (IOException e) { throw new RuntimeException(e); } int seperatorIndex = val.indexOf(":"); if(seperatorIndex == -1) return null; String user = val.substring(0, seperatorIndex); String pw = val.substring(seperatorIndex + 1); return new String[]{user,pw}; } }
public static byte[] decode(String s) { s = s.replace('-', '+'); // 62nd char of encoding s = s.replace('_', '/'); // 63rd char of encoding switch (s.length() % 4) // Pad with trailing '='s { case 0: break; // No pad chars in this case case 2: s += "=="; break; // Two pad chars case 3: s += "="; break; // One pad char default: throw new RuntimeException( "Illegal base64url string!"); } try { // KEYCLOAK-2479 : Avoid to try gzip decoding as for some objects, it may display exception to STDERR. And we know that object wasn't encoded as GZIP return Base64.decode(s, Base64.DONT_GUNZIP); } catch (Exception e) { throw new RuntimeException(e); } }
private static Object deserialize(String serialized) throws ClassNotFoundException, IOException { byte[] bytes = Base64.decode(serialized); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = null; try { in = new ObjectInputStream(bis); return in.readObject(); } finally { try { if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } }
static StoredAndServerKey getStoredAndServerKeyFromScramHash(CredentialModel credentialModel, String hmacName, String digestName) { try { StoredAndServerKey keys; if (credentialModel == null) { return getStoredAndServerKeyFromPbkdf2Hash(credentialModel, hmacName, digestName); } String[] storedAndServerKeys = credentialModel.getValue().split("\\|", 2); byte[] storedKey = Base64.decode(storedAndServerKeys[0]); byte[] serverKey = Base64.decode(storedAndServerKeys[1]); keys = new StoredAndServerKey() { @Override public byte[] getStoredKey() { return storedKey; } @Override public byte[] getServerKey() { return serverKey; } }; return keys; } catch (IOException e) { throw new IllegalArgumentException(e.getMessage(), e); } }
public static byte[] decrypt(String password, String saltString, String encodedJwe) { try { byte[] salt = Base64.decode(saltString); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 100, 128); SecretKey tmp = factory.generateSecret(spec); SecretKey aesKey = new SecretKeySpec(tmp.getEncoded(), "AES"); JWE jwe = new JWE(); jwe.getKeyStorage() .setEncryptionKey(aesKey); jwe.verifyAndDecodeJwe(encodedJwe); return jwe.getContent(); } catch (Exception e) { throw new RuntimeException(e); } }
bytes = decode( bytes, 0, bytes.length, options );
protected GSSContext establishContext() throws GSSException, IOException { GSSManager manager = GSSManager.getInstance(); Oid[] supportedMechs = new Oid[] { KerberosConstants.KRB5_OID, KerberosConstants.SPNEGO_OID }; GSSCredential gssCredential = manager.createCredential(null, GSSCredential.INDEFINITE_LIFETIME, supportedMechs, GSSCredential.ACCEPT_ONLY); GSSContext gssContext = manager.createContext(gssCredential); byte[] inputToken = Base64.decode(spnegoToken); byte[] respToken = gssContext.acceptSecContext(inputToken, 0, inputToken.length); responseToken = Base64.encodeBytes(respToken); return gssContext; }
public static String encrypt(String password, String saltString, byte[] payload) { try { byte[] salt = Base64.decode(saltString); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 100, 128); SecretKey tmp = factory.generateSecret(spec); SecretKey aesKey = new SecretKeySpec(tmp.getEncoded(), "AES"); JWEHeader jweHeader = new JWEHeader(JWEConstants.A128KW, JWEConstants.A128CBC_HS256, null); JWE jwe = new JWE() .header(jweHeader) .content(payload); jwe.getKeyStorage() .setEncryptionKey(aesKey); return jwe.encodeJwe(); } catch (Exception e) { throw new RuntimeException(e); } }
String userpw=new String(Base64.decode(tokenString)); int seperatorIndex = userpw.indexOf(":"); String user = userpw.substring(0, seperatorIndex);
if (cred.getHashIterations() != null) hashedCred.setHashIterations(cred.getHashIterations()); try { if (cred.getSalt() != null) hashedCred.setSalt(Base64.decode(cred.getSalt())); } catch (IOException ioe) { throw new RuntimeException(ioe);