一个登录密码MD5加密的封装类代码共享

/*
 * @(#)Encrypter.java  
 *       
 * 
 *  
 * 
 * All rights reserved.
 * 
 *  * 
 * 
 * 功能描述:
 * 公用方法描述:
 * 
 * 修改人:
 * 修改日期:
 * 修改原因:
 * 
 
*/




import  java.security.MessageDigest;
import  java.security.Security;
import  java.security.NoSuchAlgorithmException;
import  java.security.Provider;

import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;

/**
 * <p>
 * 加密类,里面封装的是MD5的处理机制 <br>
 * 
 * 
@author 
 * 
@since  */

public   class  Encrypter
{

    
private static String DEFAULT_JCE = "com.sun.crypto.provider.SunJCE";
    
private static String IBM_JCE = "com.ibm.crypto.provider.IBMJCE";
    
protected static final Log log = LogFactory.getLog(Encrypter.class);
    
/**
     * 初始化系统加密算法提供者
     
*/

    
static
    
{
        
        
try
        
{
            Security.addProvider((Provider)Class.forName(DEFAULT_JCE).newInstance());
        }

        
catch (Exception e)
        
{
            log.info(e);
            
try
            
{
                Security.addProvider((Provider)Class.forName(IBM_JCE).newInstance());
            }

            
catch (Exception ex)
            
{
                log.info(ex);
            }

        }

    }


    
/**
     * get hex string
     * 
     * 
@param x
     * 
@return
     
*/

    
private static String hexDigit(byte x)
    
{
        StringBuffer sb 
= new StringBuffer();
        
char c;
        
// First nibble
        c = (char) ((x >> 4& 0xf);
        
if (c > 9)
        
{
            c 
= (char) ((c - 10+ 'a');
        }

        
else
        
{
            c 
= (char) (c + '0');
        }

        sb.append(c);
        
// Second nibble
        c = (char) (x & 0xf);
        
if (c > 9)
        
{
            c 
= (char) ((c - 10+ 'a');
        }

        
else
        
{
            c 
= (char) (c + '0');
        }

        sb.append(c);
        
return sb.toString();
    }


    
/**
     * 加密
     * 
     * 
@param content
     *            加密内容
     * 
@return 加密串
     
*/

    
public static String encrypt(String content)
    
{
        
try
        
{
            MessageDigest algorithm 
= null;
            algorithm 
= MessageDigest.getInstance("MD5");
            algorithm.reset();
            
if (content != null)
            
{
                algorithm.reset();
                algorithm.update(content.getBytes());
                
byte digest[] = algorithm.digest();
                StringBuffer hexString 
= new StringBuffer();
                
int digestLength = digest.length;
                
for (int i = 0; i < digestLength; i++)
                
{
                    hexString.append(hexDigit(digest[i]));
                    hexString.append(
" ");
                }

                
return hexString.toString();
            }

            
else
            
{
                
return "";
            }

        }

        
catch (NoSuchAlgorithmException ex)
        
{
            
//加密过程中出现异常,采用原始的的内容串
            return content;
        }

    }

    
    
public static void main(String[] args){
        System.out.println(
"//" +  Encrypter.encrypt("123"+ "//");
    }

}

 

登录Action

 

  // 获取用户名,密码
            String userName  =  request.getParameter( " username " );
            String passWord 
=  Encrypter.encrypt(request
                    .getParameter(
" password " ));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值