import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EncryptUtils {
protected static final Logger logger = LoggerFactory.getLogger(EncryptUtils.class);
public static final String MD5Encode(String s) {
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
try {
byte[] btInput = s.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char[] str = new char[j * 2];
int k = 0;
for (int i = 0; i < j; ++i) {
byte byte0 = md[i];
str[(k++)] = hexDigits[(byte0 >>> 4 & 0xF)];
str[(k++)] = hexDigits[(byte0 & 0xF)];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String encodingFileName(String fileName) {
String returnFileName = "";
try {
returnFileName = URLEncoder.encode(fileName, "UTF-8");
returnFileName = StringUtils.replace(returnFileName, "+", "%20");
returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
returnFileName = StringUtils.replace(returnFileName, " ", "%20");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
logger.error("Don't support this encoding ...");
}
logger.info("encoded file name is {}", returnFileName);
logger.info("encoded file name is {}", Integer.valueOf(returnFileName.length()));
return returnFileName;
}
public static void main(String[] args) {
logger.info(EncryptUtils.MD5Encode("666666"));
}
}
//可以解密的加密算法
package com.ebiz.pxxx.web.util;
import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import com.sun.crypto.provider.SunJCE;
public class DESPlus
{
private static String strDefaultKey = "ebiz";
private Cipher encryptCipher;
private Cipher decryptCipher;
public static String byteArr2HexStr(byte[] arrB)
throws Exception
{
int iLen = arrB.length;
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; ++i) {
int intTmp = arrB[i];
while (intTmp < 0) {
intTmp += 256;
}
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
public static byte[] hexStr2ByteArr(String strIn)
throws Exception
{
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i += 2) {
String strTmp = new String(arrB, i, 2);
arrOut[(i / 2)] = (byte)Integer.parseInt(strTmp, 16);
}
return arrOut;
}
public DESPlus()
throws Exception
{
this(strDefaultKey);
}
public DESPlus(String strKey)
throws Exception
{
this.encryptCipher = null;
this.decryptCipher = null;
Security.addProvider(new SunJCE());
Key key = getKey(strKey.getBytes());
this.encryptCipher = Cipher.getInstance("DES");
this.encryptCipher.init(1, key);
this.decryptCipher = Cipher.getInstance("DES");
this.decryptCipher.init(2, key);
}
public byte[] encrypt(byte[] arrB)
throws Exception
{
return this.encryptCipher.doFinal(arrB);
}
public String encrypt(String strIn)
throws Exception
{
return byteArr2HexStr(encrypt(strIn.getBytes()));
}
public byte[] decrypt(byte[] arrB)
throws Exception
{
return this.decryptCipher.doFinal(arrB);
}
public String decrypt(String strIn)
throws Exception
{
return new String(decrypt(hexStr2ByteArr(strIn)));
}
private Key getKey(byte[] arrBTmp)
throws Exception
{
byte[] arrB = new byte[8];
for (int i = 0; (i < arrBTmp.length) && (i < arrB.length); ++i) {
arrB[i] = arrBTmp[i];
}
Key key = new SecretKeySpec(arrB, "DES");
return key;
}
public static void main(String[] args) {
try {
String test = "0";
DESPlus des = new DESPlus();
System.out.println("加密前的字符:" + test);
System.out.println("加密后的字符:" + des.encrypt(test));
System.out.println("解密后的字符:" + des.decrypt(des.encrypt(test)));
} catch (Exception e) {
e.printStackTrace();
}
}
}