java md5加密

package com.h3c.imc.sgyy.sms;


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


import sun.misc.BASE64Encoder;


/**
 * MD5 encrypt algorithm. <br>
 * Encryption irreversible.
 *
 */
public final class MD5 {


/** Determine encrypt algorithm MD5 */
private static final String ALGORITHM_MD5 = "MD5";
/** UTF-8 Encoding */
private static final String UTF_8 = "UTF-8";


/**
* MD5 16bit Encrypt Methods.

* @param readyEncryptStr
*            ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
*/
public static final String MD5_16bit(String readyEncryptStr) throws NoSuchAlgorithmException {
if (readyEncryptStr != null) {
return MD5.MD5_32bit(readyEncryptStr).substring(8, 24);
} else {
return null;
}
}


/**
* MD5 32bit Encrypt Methods.

* @param readyEncryptStr
*            ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
*/
public static final String MD5_32bit(String readyEncryptStr) throws NoSuchAlgorithmException {
if (readyEncryptStr != null) {
// Get MD5 digest algorithm's MessageDigest's instance.
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
// Use specified byte update digest.
md.update(readyEncryptStr.getBytes());
// Get cipher text
byte[] b = md.digest();
// The cipher text converted to hexadecimal string
StringBuilder su = new StringBuilder();
// byte array switch hexadecimal number.
for (int offset = 0, bLen = b.length; offset < bLen; offset++) {
String haxHex = Integer.toHexString(b[offset] & 0xFF);
if (haxHex.length() < 2) {
su.append("0");
}
su.append(haxHex);
}
return su.toString();
} else {
return null;
}
}


/**
* MD5 32bit Encrypt Methods.

* @param readyEncryptStr
*            ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
*/
public static final String MD5_32bit1(String readyEncryptStr) throws NoSuchAlgorithmException {
if (readyEncryptStr != null) {
// The cipher text converted to hexadecimal string
StringBuilder su = new StringBuilder();
// Get MD5 digest algorithm's MessageDigest's instance.
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
byte[] b = md.digest(readyEncryptStr.getBytes());
int temp = 0;
// byte array switch hexadecimal number.
for (int offset = 0, bLen = b.length; offset < bLen; offset++) {
temp = b[offset];
if (temp < 0) {
temp += 256;
}
int d1 = temp / 16;
int d2 = temp % 16;
su.append(Integer.toHexString(d1) + Integer.toHexString(d2));
}
return su.toString();
} else {
return null;
}
}


/**
* MD5 32bit Encrypt Methods.

* @param readyEncryptStr
*            ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
*/
public static final String MD5_32bit2(String readyEncryptStr) throws NoSuchAlgorithmException {
if (readyEncryptStr != null) {
// The cipher text converted to hexadecimal string
StringBuilder su = new StringBuilder();
// Get MD5 digest algorithm's MessageDigest's instance.
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
// Use specified byte update digest.
md.update(readyEncryptStr.getBytes());
byte[] b = md.digest();
int temp = 0;
// byte array switch hexadecimal number.
for (int offset = 0, bLen = b.length; offset < bLen; offset++) {
temp = b[offset];
if (temp < 0) {
temp += 256;
}
if (temp < 16) {
su.append("0");
}
su.append(Integer.toHexString(temp));
}
return su.toString();
} else {
return null;
}
}


/**
* MD5 16bit Encrypt Methods.

* @param readyEncryptStr
*            ready encrypt string
* @return String encrypt result string
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static final String MD5_64bit(String readyEncryptStr)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(md.digest(readyEncryptStr.getBytes(UTF_8)));
}


public static void main(String[] args) {
try {
String md516 = MD5.MD5_16bit("kaka123");
System.out.println("16bit-md5:\n" + md516); //
String md532 = MD5.MD5_32bit("kaka123");
String md5321 = MD5.MD5_32bit1("kaka123");
String md5322 = MD5.MD5_32bit2("kaka123");
System.out.println("32bit-md5:"); // 5d052f1e32af4e4ac2544a5fc2a9b992
System.out.println("1:  " + md532);
System.out.println("2:  " + md5321);
System.out.println("3:  " + md5322);
String md564 = MD5.MD5_64bit("kaka123");
System.out.println("64bit-md5:\n" + md564); //
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java中,可以使用MD5加密算法对数据进行加密。引用是一个示例代码,展示了如何使用MD5加密算法对输入的用户名和密码进行加密。代码中使用了一个Md5类,它包含了一个md5方法,用于对输入数据进行MD5加密。引用展示了Md5类的具体实现。 要使用MD5加密对密码进行加密,可以调用Md5类的md5方法,并传入要加密的字符串作为参数。该方法将返回加密后的字符串。 在加密时,可以使用“加盐”的方式增加密码的安全性。这意味着将用户的用户名作为额外的参数传入md5方法,与密码一起进行加密。这样即使两个用户使用相同的密码,最终的加密结果也会不同。 需要注意的是,MD5加密是不可逆的,即无法从加密后的密码还原出原始密码。因此,一般在登录时,会将用户输入的密码进行加密后,与数据库中存储的加密后的密码进行比对来验证登录信息。引用展示了一个示例,其中将用户输入的密码进行MD5加密后,再与数据库中存储的密码进行比对。 综上所述,要在Java中进行MD5加密,可以使用Md5类的md5方法,将要加密的字符串作为参数传入。为了增加密码的安全性,可以使用“加盐”的方式,将用户的用户名作为额外的参数传入md5方法进行加密。在登录时,将用户输入的密码进行MD5加密后,与数据库中存储的加密后的密码进行比对来验证登录信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Java md5加密](https://blog.csdn.net/weixin_65637841/article/details/124889207)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值