使用MD5对数据库密码进行加密的实例

package com.lxitedu.tools.generate;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.dcivision.framework.Crypt;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;

/**
* suit the php
*
* @author Administrator
*
*/
public class MD5 {

// 解密类: Crypt
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
"e", "f" };

public static void main(String[] args) throws Exception {
getMD5EncryptedString("");
String encrptedPwd = Crypt.encrypt("diaoer",
SystemParameterFactory.getSystemParameter(SystemParameterConstant.CRYPTO_SALT));
System.out.println(getMD5EncryptedString("diaoer"));
System.out.println(encrptedPwd);
}

public static String getMD5EncryptedString(String sourceString) throws NoSuchAlgorithmException {
// 加密后的字符串
// 创建具有指定算法名称的信息摘要
MessageDigest md = MessageDigest.getInstance("MD5");
// 使用指定的字节数组对摘要进行最后更新,然后完成摘要计算
byte[] results = md.digest(sourceString.getBytes());
return byteArrayToHexString(results);
}

private static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}

/**
* 将一个字节转化成十六进制形式的字符串
*/
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}

}


解密:
/*
* @(#)Crypt.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information"). You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.framework;

/**
* Crypt.java
*
* This class is to provide encrypt and decrypt function by a password provided.
*
* @author Rollo Chan
* @company DCIVision Limited
* @creation date 25/06/2003
* @version $Revision: 1.6 $
*/

public class Crypt {

public static final String REVISION = "$Revision: 1.6 $";

private Crypt() {
}

/**
* encrypt
*
* Encrypt a string with a password
*
* @param buffer
* The buffer to be encrypted
* @param password
* The password
*/
public static String encrypt(String buffer, String password) {
StringBuffer sbSpace = new StringBuffer(" ");
buffer = sbSpace.toString() + buffer + sbSpace.toString();
char[] temp = new char[buffer.length()];

int a = 0;
for (int i = 0; i < buffer.length(); i++) {
int b = password.charAt(a);
a++;
if (a >= password.length()) {
a = 0;
}
int c = (int) buffer.charAt(i);
temp[i] = (char) (c ^ b);
}
return toHex(temp);
}

/**
* decrypt
*
* Decrypt a string with a password
*
* @param buffer
* The buffer to be decrypted
* @param password
* The password
*/
public static String decrypt(String buffer, String password) {
char[] temp = toChar(buffer);
String result = "";
int a = 0;
for (int i = 0; i < temp.length; i++) {
int b = password.charAt(a);
a++;
if (a >= password.length()) {
a = 0;
}
int c = temp[i];
result += (char) (c ^ b);// ^ 按位异或XOR 就是二个操作数只有一个是1的话结果就是 1 否则就是 0 。
}
return result.trim();
}

private static int hexToInt(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
return (c - 'a') + 10;
}

private static char hexToChar(String buffer) {
int l = hexToInt(buffer.charAt(1));
int h = hexToInt(buffer.charAt(0));
return (char) (h * 16 + l);
}

private static String toHex(char[] buffer) {
String temp = "";
String result = "";
for (int i = 0; i < buffer.length; i++) {
int c = buffer[i];
temp = Integer.toHexString((int) c);
if (temp.length() == 1) {
temp = "0" + temp;
}
result += temp;
}
return result;
}

private static char[] toChar(String buffer) {
char[] result = new char[buffer.length() / 2];
for (int i = 0; i < buffer.length(); i += 2) {
if (i + 2 <= buffer.length()) {
String temp = buffer.substring(i, i + 2);
result[i / 2] = (char) hexToChar(temp);
}
}
return result;
}

/**
* main
*
* @param arg
*/
public static void main(String arg[]) {
// String buffer = arg[0];
// String password = arg[1];
String buffer = "diaoer";
String password = "test";
String temp = Crypt.encrypt(buffer, password);
System.out.println("buffer = \"" + buffer + "\"");
System.out.println("password = \"" + password + "\"");
System.out.println("encrypted = \"" + temp + "\"");
System.out.println("decrypted = \"" + Crypt.decrypt(temp, password) + "\"");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Java代码示例,展示如何使用MD5Util对字符串进行加密: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Util { // 将字符串进行MD5加密 public static String md5(String str) { try { // 获取MessageDigest实例,指定加密算法MD5 MessageDigest md = MessageDigest.getInstance("MD5"); // 更新摘要内容 md.update(str.getBytes()); // 获取加密结果,转换为16进制字符串 byte[] digest = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digest) { // 将每个字节转换为两位16进制数,不足补0 String hex = Integer.toHexString(b & 0xff); if (hex.length() == 1) { sb.append("0"); } sb.append(hex); } return sb.toString(); } catch (NoSuchAlgorithmException e) { // 异常处理 e.printStackTrace(); return null; } } public static void main(String[] args) { // 测试MD5加密 String password = "123456"; String md5Password = md5(password); System.out.println(md5Password); } } ``` 在上面的代码中,我们首先通过`MessageDigest.getInstance("MD5")`获取MessageDigest实例,指定加密算法MD5。然后,我们通过`md.update(str.getBytes())`将字符串的字节数组更新到摘要中,接着通过`md.digest()`获取加密结果的字节数组,最后将字节数组转换为16进制字符串返回。 在实际应用中,我们通常会将用户输入的密码进行MD5加密后再存储到数据库中,以保障用户的密码安全。在用户登录时,我们需要将用户输入的密码再次进行MD5加密,然后与数据库中存储的MD5摘要进行比对,以验证用户身份。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值