base64 加解密
package com.city.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import net.sourceforge.jtds.util.MD5Digest;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class BaseUtil {
/**
* BASE64 加密
* @author wangxinying
* @param src
* @return
* @throws Exception
*/
public String base64Encoder(String src) throws Exception {
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(src.getBytes("UTF8"));
}
/**
* BASE64解密
* @author wangxinying
* @param dest
* @return
* @throws Exception
*/
public String base64Decoder(String dest) throws Exception {
BASE64Decoder decoder = new BASE64Decoder();
return new String(decoder.decodeBuffer(dest), "UTF8");
}
public String md5Encode(){
String str="";
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
str=new String (md5.digest());
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
public static void main(String[] args) throws Exception {
BaseUtil u=new BaseUtil();
String sd="1234567890000000"+","+"1234";
String ud=u.base64Encoder(sd);
String ue=u.base64Decoder("MTM1MTE1MDYwOTkwNix1c2VyaTEy");
System.out.println(ue);
Date da=new Date();
String s= da.getTime()+"";
String p=s+","+"useri12";
System.out.println(p);
String ud1=u.base64Encoder(p);
System.out.println(s);
System.out.println(ue);
System.out.println(ud1);
}
}
des加解密
package com.city.util;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.log4j.Logger;
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
public class DESedeUtil {
private static final Logger log = Logger.getLogger(DESedeUtil.class);
private static final String b="McityPortalDd";
private static final String ENCRYALGORITHM="DES/ECB/PKCS5Padding";
// private static byte[] b =new byte[]{-22, 87, -45, 67, -82, -62, -5, 25};
private static SecretKey key;
public static SecretKey getKey(){
SecretKeyFactory keyFactory;
try {
DESKeySpec desKeySpec=new DESKeySpec(b.getBytes());
keyFactory = SecretKeyFactory.getInstance("DES");
key= keyFactory.generateSecret(desKeySpec);
return key;
} catch (NoSuchAlgorithmException e1) {
log.error(e1.getMessage(), e1);
} catch (InvalidKeyException e) {
log.error(e.getMessage(), e);
} catch (InvalidKeySpecException e) {
log.error(e.getMessage(), e);
}
return null;
}
public static byte[] encry(byte[] message){
try {
if(key==null)
key=getKey();
Cipher c=Cipher.getInstance(ENCRYALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encry=c.doFinal(message);
return encry;
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage(), e);
} catch (NoSuchPaddingException e) {
log.error(e.getMessage(), e);
} catch (InvalidKeyException e) {
log.error(e.getMessage(), e);
} catch (IllegalBlockSizeException e) {
log.error(e.getMessage(), e);
} catch (BadPaddingException e) {
log.error(e.getMessage(), e);
}
return null;
}
public static byte[] decry(byte[] message){
try {
if(key==null)
key=getKey();
Cipher c=Cipher.getInstance(ENCRYALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte [] decry=c.doFinal(message);
return decry;
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage(), e);
} catch (NoSuchPaddingException e) {
log.error(e.getMessage(), e);
} catch (InvalidKeyException e) {
log.error(e.getMessage(), e);
} catch (IllegalBlockSizeException e) {
log.error(e.getMessage(), e);
} catch (BadPaddingException e) {
log.error(e.getMessage(), e);
}
return null;
}
public static String encrymessage(String message){
byte [] msg=message.getBytes();
byte [] encrybyte=encry(msg);
return Base64.encode(encrybyte);
}
public static String decrymessage(String message) throws Exception{
byte [] msg=Base64.decode(message);
byte [] decrybyte=decry(msg);
String decryMessage=new String(decrybyte);
return decryMessage;
}
public static void main(String[] args) throws Exception {
String encrymessage= encrymessage("123456");
System.out.println("密文="+encrymessage);
String decrymessage=decrymessage(encrymessage);
System.out.println("原文="+decrymessage);
}
}