org.bouncycastle.util.encoders.DecoderException Java Examples

The following examples show how to use org.bouncycastle.util.encoders.DecoderException. 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: base64_url.java    From tls-sig-api-java with MIT License 6 votes vote down vote up
public static byte[] base64DecodeUrl(byte[] in_str) throws DecoderException {
    byte[] base64 = in_str.clone();
    for (int i = 0; i < base64.length; ++i)
        switch (base64[i]) {
            case '*':
                base64[i] = '+';
                break;
            case '-':
                base64[i] = '/';
                break;
            case '_':
                base64[i] = '=';
                break;
            default:
                break;
        }
    return Base64.decode(base64);
}
 
Example #2
Source File: SolidityUtil.java    From hedera-sdk-java with Apache License 2.0 5 votes vote down vote up
static byte[] decodeAddress(String address) {
    if (address.length() != ADDRESS_LEN_HEX) {
        throw new IllegalArgumentException(
            "Solidity addresses must be 20 bytes or 40 hex chars");
    }

    try {
        return Hex.decode(address);
    } catch (DecoderException e) {
        throw new IllegalArgumentException("failed to decode Solidity address as hex", e);
    }
}
 
Example #3
Source File: ContractFunctionParams.java    From hedera-sdk-java with Apache License 2.0 5 votes vote down vote up
private static byte[] decodeAddress(String address) {
    if (address.length() != ADDRESS_LEN_HEX) {
        throw new IllegalArgumentException(
            "Solidity addresses must be 40 hex chars");
    }

    try {
        return Hex.decode(address);
    } catch (DecoderException e) {
        throw new IllegalArgumentException("failed to decode Solidity address as hex", e);
    }
}
 
Example #4
Source File: AESSensitivePropertyProvider.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the decrypted plaintext.
 *
 * @param protectedValue the cipher text read from the {@code nifi.properties} file
 * @return the raw value to be used by the application
 * @throws SensitivePropertyProtectionException if there is an error decrypting the cipher text
 */
@Override
public String unprotect(String protectedValue) throws SensitivePropertyProtectionException {
    if (protectedValue == null || protectedValue.trim().length() < MIN_CIPHER_TEXT_LENGTH) {
        throw new IllegalArgumentException("Cannot decrypt a cipher text shorter than " + MIN_CIPHER_TEXT_LENGTH + " chars");
    }

    if (!protectedValue.contains(DELIMITER)) {
        throw new IllegalArgumentException("The cipher text does not contain the delimiter " + DELIMITER + " -- it should be of the form Base64(IV) || Base64(cipherText)");
    }

    protectedValue = protectedValue.trim();

    final String IV_B64 = protectedValue.substring(0, protectedValue.indexOf(DELIMITER));
    byte[] iv = Base64.decode(IV_B64);
    if (iv.length < IV_LENGTH) {
        throw new IllegalArgumentException("The IV (" + iv.length + " bytes) must be at least " + IV_LENGTH + " bytes");
    }

    String CIPHERTEXT_B64 = protectedValue.substring(protectedValue.indexOf(DELIMITER) + 2);

    // Restore the = padding if necessary to reconstitute the GCM MAC check
    if (CIPHERTEXT_B64.length() % 4 != 0) {
        final int paddedLength = CIPHERTEXT_B64.length() + 4 - (CIPHERTEXT_B64.length() % 4);
        CIPHERTEXT_B64 = StringUtils.rightPad(CIPHERTEXT_B64, paddedLength, '=');
    }

    try {
        byte[] cipherBytes = Base64.decode(CIPHERTEXT_B64);

        cipher.init(Cipher.DECRYPT_MODE, this.key, new IvParameterSpec(iv));
        byte[] plainBytes = cipher.doFinal(cipherBytes);
        logger.info(getName() + " decrypted a sensitive value successfully");
        return new String(plainBytes, StandardCharsets.UTF_8);
    } catch (BadPaddingException | IllegalBlockSizeException | DecoderException | InvalidAlgorithmParameterException | InvalidKeyException e) {
        final String msg = "Error decrypting a protected value";
        logger.error(msg, e);
        throw new SensitivePropertyProtectionException(msg, e);
    }
}
 
Example #5
Source File: Utils.java    From nuls-v2 with MIT License 5 votes vote down vote up
/**
 * Decodes a hex string to address bytes and checks validity
 *
 * @param hex - a hex string of the address, e.g., 6c386a4b26f73c802f34673f7248bb118f97424a
 * @return - decode and validated address byte[]
 */
public static byte[] addressStringToBytes(String hex) {
    final byte[] addr;
    try {
        addr = Hex.decode(hex);
    } catch (DecoderException addressIsNotValid) {
        return null;
    }

    if (isValidAddress(addr)) {
        return addr;
    }
    return null;
}
 
Example #6
Source File: TestBase64.java    From tls-sig-api-java with MIT License 5 votes vote down vote up
@Test
public void failed() {
    try {
        base64_url.base64DecodeUrl("123".getBytes());
        Assert.fail();
    } catch (Exception e) {
        Assert.assertEquals(DecoderException.class, e.getClass());
    }
}
 
Example #7
Source File: AESSensitivePropertyProvider.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the decrypted plaintext.
 *
 * @param protectedValue the cipher text read from the {@code nifi.properties} file
 * @return the raw value to be used by the application
 * @throws SensitivePropertyProtectionException if there is an error decrypting the cipher text
 */
@Override
public String unprotect(String protectedValue) throws SensitivePropertyProtectionException {
    if (protectedValue == null || protectedValue.trim().length() < MIN_CIPHER_TEXT_LENGTH) {
        throw new IllegalArgumentException("Cannot decrypt a cipher text shorter than " + MIN_CIPHER_TEXT_LENGTH + " chars");
    }

    if (!protectedValue.contains(DELIMITER)) {
        throw new IllegalArgumentException("The cipher text does not contain the delimiter " + DELIMITER + " -- it should be of the form Base64(IV) || Base64(cipherText)");
    }

    protectedValue = protectedValue.trim();

    final String IV_B64 = protectedValue.substring(0, protectedValue.indexOf(DELIMITER));
    byte[] iv = Base64.decode(IV_B64);
    if (iv.length < IV_LENGTH) {
        throw new IllegalArgumentException("The IV (" + iv.length + " bytes) must be at least " + IV_LENGTH + " bytes");
    }

    String CIPHERTEXT_B64 = protectedValue.substring(protectedValue.indexOf(DELIMITER) + 2);

    // Restore the = padding if necessary to reconstitute the GCM MAC check
    if (CIPHERTEXT_B64.length() % 4 != 0) {
        final int paddedLength = CIPHERTEXT_B64.length() + 4 - (CIPHERTEXT_B64.length() % 4);
        CIPHERTEXT_B64 = StringUtils.rightPad(CIPHERTEXT_B64, paddedLength, '=');
    }

    try {
        byte[] cipherBytes = Base64.decode(CIPHERTEXT_B64);

        cipher.init(Cipher.DECRYPT_MODE, this.key, new IvParameterSpec(iv));
        byte[] plainBytes = cipher.doFinal(cipherBytes);
        logger.debug(getName() + " decrypted a sensitive value successfully");
        return new String(plainBytes, StandardCharsets.UTF_8);
    } catch (BadPaddingException | IllegalBlockSizeException | DecoderException | InvalidAlgorithmParameterException | InvalidKeyException e) {
        final String msg = "Error decrypting a protected value";
        logger.error(msg, e);
        throw new SensitivePropertyProtectionException(msg, e);
    }
}
 
Example #8
Source File: AESSensitivePropertyProvider.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the decrypted plaintext.
 *
 * @param protectedValue the cipher text read from the {@code nifi.properties} file
 * @return the raw value to be used by the application
 * @throws SensitivePropertyProtectionException if there is an error decrypting the cipher text
 */
@Override
public String unprotect(String protectedValue) throws SensitivePropertyProtectionException {
    if (protectedValue == null || protectedValue.trim().length() < MIN_CIPHER_TEXT_LENGTH) {
        throw new IllegalArgumentException("Cannot decrypt a cipher text shorter than " + MIN_CIPHER_TEXT_LENGTH + " chars");
    }

    if (!protectedValue.contains(DELIMITER)) {
        throw new IllegalArgumentException("The cipher text does not contain the delimiter " + DELIMITER + " -- it should be of the form Base64(IV) || Base64(cipherText)");
    }

    protectedValue = protectedValue.trim();

    final String IV_B64 = protectedValue.substring(0, protectedValue.indexOf(DELIMITER));
    byte[] iv = Base64.decode(IV_B64);
    if (iv.length < IV_LENGTH) {
        throw new IllegalArgumentException("The IV (" + iv.length + " bytes) must be at least " + IV_LENGTH + " bytes");
    }

    String CIPHERTEXT_B64 = protectedValue.substring(protectedValue.indexOf(DELIMITER) + 2);

    // Restore the = padding if necessary to reconstitute the GCM MAC check
    if (CIPHERTEXT_B64.length() % 4 != 0) {
        final int paddedLength = CIPHERTEXT_B64.length() + 4 - (CIPHERTEXT_B64.length() % 4);
        CIPHERTEXT_B64 = StringUtils.rightPad(CIPHERTEXT_B64, paddedLength, '=');
    }

    try {
        byte[] cipherBytes = Base64.decode(CIPHERTEXT_B64);

        cipher.init(Cipher.DECRYPT_MODE, this.key, new IvParameterSpec(iv));
        byte[] plainBytes = cipher.doFinal(cipherBytes);
        logger.debug(getName() + " decrypted a sensitive value successfully");
        return new String(plainBytes, StandardCharsets.UTF_8);
    } catch (BadPaddingException | IllegalBlockSizeException | DecoderException | InvalidAlgorithmParameterException | InvalidKeyException e) {
        final String msg = "Error decrypting a protected value";
        logger.error(msg, e);
        throw new SensitivePropertyProtectionException(msg, e);
    }
}