import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.codec.binary.Base64;

public class Base64Helper {
private static final Logger logger = LoggerFactory.getLogger(Base64Helper.class);

/**
* BASE64解密
*
* @param key
* @return
* @throws Exception
*/
public static String decryptBase64(String key) throws Exception{
key = key.replace("-", "/"); //解决URL参数带/问题。
byte[] byteArray=Base64.decodeBase64(key); // decryptBASE64(key);
String code = new String(byteArray);
logger.debug(" decrypt code:{} ",code);
return code;

}
//

/**
* BASE64加密
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBase64(String key) throws Exception {
byte[] binaryData = key.getBytes();
String code = Base64.encodeBase64String(binaryData); //encryptBASE64(byteArray);
logger.debug(" encrypt code:{} ",code);
return code.replace("/", "-");//解决URL参数带/问题。
}
public static void main(String[] args) throws Exception {
Date startTime = new Date();
//BASE64加密
String code = encryptBase64("MP3/Mp4");
logger.debug("BASE64加密:{}",code);
String key = decryptBase64(code);
logger.debug("BASE64解密:{}",key);
Date endTime = new Date();
long diff = endTime.getTime() - startTime.getTime();
long days = diff;// / (1000 * 60 * 60 * 24);
logger.debug("time:{}",days);
}
}
  URL

創作者介紹

資訊園

shadow 發表在 痞客邦 PIXNET 留言(0) 人氣()