2007-04-28
一个登录密码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"));发表评论
- 浏览: 72219 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Spring AOP四种创建通知( ...
我一直想知道如何拦截某个类中特定的方法,而不是所有的方法?
-- by yourgame -
HashTable和HashMap的区 ...
这么多总结,还算你的这个不错,谢谢,学习
-- by lysmart_8 -
鼠标移动距离点击次数及键 ...
-- by fengtotoer -
处理Date对象时进行格式化 ...
好
-- by wuha_yu -
Webwork的FileUploadInte ...
我想知道如何取消息掉struts2的文件上传拦截器. fileuploadint ...
-- by jems






评论排行榜