com.beginner.core.utils.Base64Util.java Source code

Java tutorial

Introduction

Here is the source code for com.beginner.core.utils.Base64Util.java

Source

/*
 * Copyright 2015-9999 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.beginner.core.utils;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

/**
 * <b>??</b>Base64Util<br/>
 * <b>??</b>Base64<br/>
 * <b></b>Hsiao Lin Studio<br/>
 * <b></b><br/>
 * <b></b>20150521 ?6:18:18<br/>
 * <b></b><br/>
 * @version 1.0.0<br/>
 */
public class Base64Util extends Base64 {

    private static final String DEFAULT_ENCODING = "UTF-8";

    /**
     * Base64(UTF-8)
     * @param plaintext    
     * @return             
     */
    public static String encrypt(String plaintext) {
        return encrypt(plaintext, DEFAULT_ENCODING);
    }

    /**
     * Base64(UTF-8)
     * @param ciphertext    
     * @return             
     */
    public static String decrypt(String ciphertext) {
        return decrypt(ciphertext, DEFAULT_ENCODING);
    }

    /**
     * Base64
     * @param plaintext    
     * @param encoding       ??
     * @return             
     */
    public static String encrypt(String plaintext, String encoding) {
        if (StringUtils.isBlank(plaintext))
            return plaintext;
        try {
            return new String(encodeBase64(plaintext.getBytes(encoding)));
        } catch (UnsupportedEncodingException e) {
            return plaintext;
        }
    }

    /**
     * Base64
     * @param ciphertext    
     * @param encoding       ??
     * @return             
     */
    public static String decrypt(String ciphertext, String encoding) {
        if (StringUtils.isBlank(ciphertext))
            return ciphertext;
        try {
            return new String(decodeBase64(ciphertext.getBytes(encoding)));
        } catch (UnsupportedEncodingException e) {
            return ciphertext;
        }
    }
}