加密2

package com.lot.common.util;

import java.security.*;
import java.security.interfaces.RSAPublicKey;

/**
 *
 * <p>Title: </p>
 * <p>Description: 加密类</p>
 * <p>Copyright: Copyright (c) 2011 版权</p>
 * <p>Company: </p>
 * @author kevin
 * @version V1.0
 * @date 2011-6-10下午02:26:39
 */
public class EncryptUtil {
 /**
  *
  * @author: kevin
  * @Title getEncrypt
  * @Time: 2011-6-10下午02:26:59
  * @Description: SHA256位加密
  * @return: String
  * @throws:
  * @param strSrc
  * @return
  */
    public String getSHA256Encrypt(String strSrc) {
        MessageDigest md = null;
        String strDes = null;

        byte[] bt = strSrc.getBytes();
        try {
            md = MessageDigest.getInstance("SHA-256");
            md.update(bt);
            strDes = bytes2Hex(md.digest());
        }
        catch (NoSuchAlgorithmException e) {
            return null;
        }
        return strDes;
    }

    /**
     *
     * @author: kevin
     * @Title getMD5Encrypt
     * @Time: 2011-6-10下午02:29:14
     * @Description: MD5加密
     * @return: String
     * @throws:
     * @param strSrc
     * @return
     */
    public String getMD5Encrypt(String strSrc) {
        MessageDigest md = null;
        String strDes = null;

        byte[] bt = strSrc.getBytes();
        try {
            md = MessageDigest.getInstance("MD5");
            md.update(bt);
            strDes = bytes2Hex(md.digest());
        }
        catch (NoSuchAlgorithmException e) {
            return null;
        }
        return strDes;
    }
   
    /**
     *
     * @author: kevin
     * @Title bytes2Hex
     * @Time: 2011-6-10下午02:27:13
     * @Description:
     * @return: String
     * @throws:
     * @param bts
     * @return
     */
    public String bytes2Hex(byte[]bts) {
        String des = "";
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }

    /**
  *
  * @author: Yolanda.qin
  * @Title 加密卡号
  * @Time: 2011-6-18上午10:13:39
  * @Description:
  * @return: 加密后的卡号
  * @throws: Exception, EncryptException
  * @param cardno 卡号
  */
    public String getCardNoRSA(String cardno)throws Exception, EncryptException {
        /**
         * 在web容器中,baseURL路径获取是 "*.war/WEB-INF/classes/"
         */
//        String keyUrl = baseURL.getPath() + "../rsa/";

        //调用,getClass().getResource("/")= "classes/"
        String keyUrl = getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "../rsa/";//

        System.out.println("keyUrl = " + keyUrl);
        RSAPublicKey Public = (RSAPublicKey) RSAUtil.ReadObject(keyUrl + "RSAPublicKey_20091204.dat");


        long startTime = System.currentTimeMillis();
         //加密
        String srcRSA_encrypt = RSAUtil.encrypt(Public, cardno);

        long endTime1 = System.currentTimeMillis();


        return srcRSA_encrypt;

    }
   
    public static void main(String[]args) {
        EncryptUtil te = new EncryptUtil();
        String strSrc = "admin123";
        System.out.println("Source String:" + strSrc);
        /*System.out.println("Use MD5:(32个字符)" + te.getEncrypt(strSrc, "MD5"));
        System.out.println("Use SHA1:(40个字符)" + te.getEncrypt(strSrc, "SHA-1"));
        System.out.println("Use SHA-256:(64个字符)" + te.getEncrypt(strSrc, "SHA-256"));
        System.out.println("Use SHA-384:(96个字符)" + te.getEncrypt(strSrc, "SHA-384"));
        System.out.println("Use SHA-512:(128个字符)" + te.getEncrypt(strSrc, "SHA-512"));*/
       
        System.out.println("Use SHA-256:(64个字符)" + te.getSHA256Encrypt(strSrc));
    }
}

 

 

----------------------------------------------------例子-------------------------------------------------------------------------

1.0

   EncryptUtil sha = new EncryptUtil();
   loginInfo.setLoginPass(sha.getSHA256Encrypt(loginInfo
     .getLoginPass()));
   param.add(loginInfo.getLoginPass().trim());
   param.add(loginInfo.getLoginName().trim());
   param.add(loginInfo.getMerNo().trim());

 

2.0

    //成功后,保存虚拟帐户信息
    String virtualAccount = user.getAdminInfo().getMchInfo().getVirtualAccount();
    user.setVaNO(virtualAccount);
    user.setPid(PID);
    //平台ID+平台KEY+虚拟账号
    user.setKey(new EncryptUtil().getSHA256Encrypt(PID+KEY+user.getVaNO()));

 

你可能感兴趣的:(加密)