加密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()));

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值