首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

用java如何对密码加密

2012-01-06 
用java怎么对密码加密?输入密码后到数据库中变成别的了怎么加密?请高手帮忙![解决办法]我用MD5。[解决办法]

用java怎么对密码加密?
输入密码后到数据库中变成别的了
怎么加密?
请高手帮忙!

[解决办法]
我用MD5。
[解决办法]
md5
[解决办法]
MD5

[解决办法]
顶。就是MD5.public class MD5 {
/* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的,
这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个
Instance间共享*/
static final int S11 = 7;
static final int S12 = 12;
static final int S13 = 17;
static final int S14 = 22;

static final int S21 = 5;
static final int S22 = 9;
static final int S23 = 14;
static final int S24 = 20;

static final int S31 = 4;
static final int S32 = 11;
static final int S33 = 16;
static final int S34 = 23;

static final int S41 = 6;
static final int S42 = 10;
static final int S43 = 15;
static final int S44 = 21;

static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
/* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中
被定义到MD5_CTX结构中

*/
private long[] state = new long[4]; // state (ABCD)
private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb first)
private byte[] buffer = new byte[64]; // input buffer

/* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的
  16进制ASCII表示.
*/
public String digestHexStr;

/* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.
*/
private byte[] digest = new byte[16];

/*
getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串
返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.
*/
public String getMD5ofStr(String inbuf) {
md5Init();
md5Update(inbuf.getBytes(), inbuf.length());
md5Final();
digestHexStr = " ";
for (int i = 0; i < 16; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;

}
[解决办法]
md5
[解决办法]
永远支持MD5 我永远的痛!
[解决办法]
MD5加密
[解决办法]
md5真的有那么好吗?我都在md5上机输了一次了
[解决办法]
mark
[解决办法]
前面都说了用MD5
你要是自己写个加密的也可以呀
[解决办法]
private static String encrypte(String plainText, String algorithm) {
  MessageDigest md = null;
  try {
  md = MessageDigest.getInstance(algorithm);
  } catch (NoSuchAlgorithmException e) {
  e.printStackTrace();
  }
  md.update(plainText.getBytes());
  byte[] b = md.digest();
  StringBuffer output = new StringBuffer(32);


  for (int i = 0; i < b.length; i++) {
  String temp = Integer.toHexString(b[i] & 0xff);
  if (temp.length() < 2) {
    output.append( "0 ");
  }
  output.append(temp);
  }
  return output.toString();
}

public static String password(String password) {
  return encrypte(encrypte(password, "sha-1 "), "md5 ");
}

呵呵,采用 JCA 的 MessageDigest 摘要算法,进行 SHA-1 和 MD5 双重加密。
[解决办法]
md5 多次加密

缺点 无法找回密码
[解决办法]
md5就可以,网上有很多现成的组件
[解决办法]
java.security.MessageDigest;里面有很多加密算法
sun已经帮你实现了
[解决办法]
appfuse的加密类

import java.io.IOException;
import java.security.MessageDigest;

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


/**
 * String Utility Class This is used to encode passwords programmatically
 *
 * <p>
 * <a h
 * ref="StringUtil.java.html"><i>View Source</i></a>
 * </p>
 * 
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
public class StringUtil {
//~ Static fields/initializers =============================================

private final static Log log = LogFactory.getLog(StringUtil.class);

//~ Methods ================================================================

/**
* Encode a string using algorithm specified in web.xml and return the
* resulting encrypted password. If exception, the plain credentials
* string is returned
*
* @param password Password or other credentials to use in authenticating
* this username
* @param algorithm Algorithm used to do the digest
*
* @return encypted password based on the algorithm.
*/
public static String encodePassword(String password, String algorithm) {
byte[] unencodedPassword = password.getBytes();

MessageDigest md = null;

try {
// first create an instance, given the provider
md = MessageDigest.getInstance(algorithm);
} catch (Exception e) {
log.error("Exception: " + e);

return password;
}

md.reset();

// call the update method one or more times
// (useful when you don't know the size of your data, eg. stream)
md.update(unencodedPassword);

// now calculate the hash
byte[] encodedPassword = md.digest();

StringBuffer buf = new StringBuffer();

for (int i = 0; i < encodedPassword.length; i++) {
if ((encodedPassword[i] & 0xff) < 0x10) {
buf.append("0");
}

buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
}

return buf.toString();
}

/**
* Encode a string using Base64 encoding. Used when storing passwords
* as cookies.
*
* This is weak encoding in that anyone can use the decodeString
* routine to reverse the encoding.
*
* @param str
* @return String
*/
public static String encodeString(String str) {
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
return encoder.encodeBuffer(str.getBytes()).trim();


}

/**
* Decode a string using Base64 encoding.
*
* @param str
* @return String
*/
public static String decodeString(String str) {
sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
try {
return new String(dec.decodeBuffer(str));
} catch (IOException io) {
throw new RuntimeException(io.getMessage(), io.getCause());
}
}
}


如下调用

String encryptPwd = StringUtil.encodePassword(pwd, "MD5"));
[解决办法]
md5   感觉不错!
[解决办法]
md5就可以,网上有很多现成的组件

热点排行