Example usage for java.util Base64 getMimeDecoder

List of usage examples for java.util Base64 getMimeDecoder

Introduction

In this page you can find the example usage for java.util Base64 getMimeDecoder.

Prototype

public static Decoder getMimeDecoder() 

Source Link

Document

Returns a Decoder that decodes using the MIME type base64 decoding scheme.

Usage

From source file:org.codice.ddf.security.idp.client.AssertionConsumerService.java

private String decodeBase64(String encoded) {
    return new String(Base64.getMimeDecoder().decode(encoded.getBytes(StandardCharsets.UTF_8)),
            StandardCharsets.UTF_8);
}

From source file:org.openhab.binding.km200.internal.KM200Cryption.java

/**
 * This function does the decoding for a new message from the device
 *
 *//*from w ww. j  a v  a  2s .co  m*/
public String decodeMessage(byte[] encoded) {
    String retString = null;
    byte[] decodedB64 = null;

    // MimeDecoder was the only working decoder.
    decodedB64 = Base64.getMimeDecoder().decode(encoded);

    try {
        /* Check whether the length of the decryptData is NOT multiplies of 16 */
        if ((decodedB64.length & 0xF) != 0) {
            /* Return the data */
            retString = new String(decodedB64, remoteDevice.getCharSet());
            logger.debug("Did NOT decrypt message");
            return retString;
        }
        // --- create cipher
        final Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(remoteDevice.getCryptKeyPriv(), "AES"));
        byte[] decryptedData = cipher.doFinal(decodedB64);
        byte[] decryptedDataWOZP = removeZeroPadding(decryptedData);
        return (new String(decryptedDataWOZP, remoteDevice.getCharSet()));
    } catch (UnsupportedEncodingException | GeneralSecurityException e) {
        logger.debug("Exception on encoding: {}", e);
        return null;
    }
}