base64加解密 des加解密

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);
  }
}


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值