/** * returns length of decoded data given an * array containing encoded data. * WhiteSpace removing is done if data array not * valid. * * @param base64Data an array containing encoded data * @return length of decoded data, -1 if not decoded */ public static synchronized int getDecodedDataLength(byte[] base64Data) { if (base64Data == null) { return -1; } if (base64Data.length == 0) { return 0; } //byte[] normalizedBase64Data = removeWhiteSpace( base64Data );//Remove any whiteSpace byte[] decodedData = null; if ((decodedData = decode(base64Data)) == null) { //decode could return a null byte array return -1; } return decodedData.length; } }
public static synchronized boolean isArrayByteBase64(byte[] arrayOctect) { return (getDecodedDataLength(arrayOctect) >= 0); }
public static boolean isBase64(byte octect) { return (isWhiteSpace(octect) || isPad(octect) || isData(octect)); }
return null; byte[] normalizedBase64Data = removeWhiteSpace(base64Data); if (normalizedBase64Data.length % FOURBYTE != 0) { decodedData = new byte[(numberQuadruple) * 3]; for (; i < numberQuadruple - 1; i++) { if (!isData((d1 = normalizedBase64Data[dataIndex++])) || !isData((d2 = normalizedBase64Data[dataIndex++])) || !isData((d3 = normalizedBase64Data[dataIndex++])) || !isData((d4 = normalizedBase64Data[dataIndex++]))) { decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); if (!isData((d1 = normalizedBase64Data[dataIndex++])) || !isData((d2 = normalizedBase64Data[dataIndex++]))) { d3 = normalizedBase64Data[dataIndex++]; d4 = normalizedBase64Data[dataIndex++]; if (!isData((d3)) || !isData((d4))) { if (isPad(d3) && isPad(d4)) { tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4); return tmp; } else if (!isPad(d3) && isPad(d4)) {
/** * Encrypts given string with key generated from {@link #getPassword()} with given iteration count and return the masked * value (to be used as the variable). * * @param secretStr sensitive string to be protected by encryption * @param iterations iteration count * @return Encrypted value. */ protected String encrypt(String secretStr, int iterations) throws Exception { SecureRandom secureRandom = new SecureRandom(); byte[] salt = new byte[saltLengthBytes]; secureRandom.nextBytes(salt); byte[] encryptedVal = transform(Cipher.ENCRYPT_MODE, secretStr.getBytes(UTF8_CHARSET), salt, iterations); return new String(Base64.encode(salt), UTF8_CHARSET) + ":" + iterations + ":" + new String(Base64.encode(encryptedVal), UTF8_CHARSET); }
/** * Tests a string to see if it is valid Base64 * * @return true if string is valid Base64, false otherwise */ public static boolean isBase64(String isValidString) { if (isValidString == null) { return false; } return isArrayByteBase64(stringToBytes(isValidString)); }
int i = 0; for (; i < len; i++) { if (!isWhiteSpace(data[i])) { newSize++; if (isWhiteSpace(data[i])) { continue; } else {
return null; byte[] normalizedBase64Data = removeWhiteSpace(base64Data); if (normalizedBase64Data.length % FOURBYTE != 0) { decodedData = new byte[(numberQuadruple) * 3]; for (; i < numberQuadruple - 1; i++) { if (!isData((d1 = normalizedBase64Data[dataIndex++])) || !isData((d2 = normalizedBase64Data[dataIndex++])) || !isData((d3 = normalizedBase64Data[dataIndex++])) || !isData((d4 = normalizedBase64Data[dataIndex++]))) { decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); if (!isData((d1 = normalizedBase64Data[dataIndex++])) || !isData((d2 = normalizedBase64Data[dataIndex++]))) { d3 = normalizedBase64Data[dataIndex++]; d4 = normalizedBase64Data[dataIndex++]; if (!isData((d3)) || !isData((d4))) { if (isPad(d3) && isPad(d4)) { tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4); return tmp; } else if (!isPad(d3) && isPad(d4)) {
/** * Encrypts given string with key generated from {@link #getPassword()} with given iteration count and return the masked * value (to be used as the variable). * * @param secretStr sensitive string to be protected by encryption * @param iterations iteration count * @return Encrypted value. */ protected String encrypt(String secretStr, int iterations) throws Exception { SecureRandom secureRandom = new SecureRandom(); byte[] salt = new byte[saltLengthBytes]; secureRandom.nextBytes(salt); byte[] encryptedVal = transform(Cipher.ENCRYPT_MODE, secretStr.getBytes(UTF8_CHARSET), salt, iterations); return new String(Base64.encode(salt), UTF8_CHARSET) + ":" + iterations + ":" + new String(Base64.encode(encryptedVal), UTF8_CHARSET); }
/** * Tests a string to see if it is valid Base64 * * @return true if string is valid Base64, false otherwise */ public static boolean isBase64(String isValidString) { if (isValidString == null) { return false; } return isArrayByteBase64(stringToBytes(isValidString)); }
int i = 0; for (; i < len; i++) { if (!isWhiteSpace(data[i])) { newSize++; if (isWhiteSpace(data[i])) { continue; } else {
public static boolean isBase64(byte octect) { return (isWhiteSpace(octect) || isPad(octect) || isData(octect)); }
/** * returns length of decoded data given an * array containing encoded data. * WhiteSpace removing is done if data array not * valid. * * @param base64Data an array containing encoded data * @return length of decoded data, -1 if not decoded */ public static synchronized int getDecodedDataLength(byte[] base64Data) { if (base64Data == null) { return -1; } if (base64Data.length == 0) { return 0; } //byte[] normalizedBase64Data = removeWhiteSpace( base64Data );//Remove any whiteSpace byte[] decodedData = null; if ((decodedData = decode(base64Data)) == null) { //decode could return a null byte array return -1; } return decodedData.length; } }
@Override protected char[] getPassword() { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (passwordFile != null) { FileInputStream fis = new FileInputStream(passwordFile); try { baos.write(IOUtil.toByteArray(fis)); } finally { IOUtil.closeResource(fis); } } if (passwordUserProperties) { baos.write(System.getProperty("user.home").getBytes(UTF8_CHARSET)); baos.write(System.getProperty("user.name").getBytes(UTF8_CHARSET)); } if (passwordNetworkInterface != null) { try { NetworkInterface iface = NetworkInterface.getByName(passwordNetworkInterface); baos.write(iface.getHardwareAddress()); } catch (SocketException e) { throw rethrow(e); } } return new String(Base64.encode(baos.toByteArray()), UTF8_CHARSET).toCharArray(); } catch (Exception e) { throw rethrow(e); } }
public static synchronized boolean isArrayByteBase64(byte[] arrayOctect) { return (getDecodedDataLength(arrayOctect) >= 0); }
/** * Decrypts given encrypted variable. * * @param encryptedStr * @return */ protected String decrypt(String encryptedStr) throws Exception { String[] split = encryptedStr.split(":"); checkTrue(split.length == 3, "Wrong format of the encrypted variable (" + encryptedStr + ")"); byte[] salt = Base64.decode(split[0].getBytes(UTF8_CHARSET)); checkTrue(salt.length == saltLengthBytes, "Salt length doesn't match."); int iterations = Integer.parseInt(split[1]); byte[] encryptedVal = Base64.decode(split[2].getBytes(UTF8_CHARSET)); return new String(transform(Cipher.DECRYPT_MODE, encryptedVal, salt, iterations), UTF8_CHARSET); }
@Override protected char[] getPassword() { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (passwordFile != null) { FileInputStream fis = new FileInputStream(passwordFile); try { baos.write(IOUtil.toByteArray(fis)); } finally { IOUtil.closeResource(fis); } } if (passwordUserProperties) { baos.write(System.getProperty("user.home").getBytes(UTF8_CHARSET)); baos.write(System.getProperty("user.name").getBytes(UTF8_CHARSET)); } if (passwordNetworkInterface != null) { try { NetworkInterface iface = NetworkInterface.getByName(passwordNetworkInterface); baos.write(iface.getHardwareAddress()); } catch (SocketException e) { throw rethrow(e); } } return new String(Base64.encode(baos.toByteArray()), UTF8_CHARSET).toCharArray(); } catch (Exception e) { throw rethrow(e); } }
/** * Decrypts given encrypted variable. * * @param encryptedStr * @return */ protected String decrypt(String encryptedStr) throws Exception { String[] split = encryptedStr.split(":"); checkTrue(split.length == 3, "Wrong format of the encrypted variable (" + encryptedStr + ")"); byte[] salt = Base64.decode(split[0].getBytes(UTF8_CHARSET)); checkTrue(salt.length == saltLengthBytes, "Salt length doesn't match."); int iterations = Integer.parseInt(split[1]); byte[] encryptedVal = Base64.decode(split[2].getBytes(UTF8_CHARSET)); return new String(transform(Cipher.DECRYPT_MODE, encryptedVal, salt, iterations), UTF8_CHARSET); }