导航:首页 > 源码编译 > md5hash算法java

md5hash算法java

发布时间:2022-04-20 19:18:31

java中如何把计算出来的哈希函数值(MD5)转换为对称加密(DES)的密钥

package com.kingsoft.main;/**
* @author King_wangyao
*/
public class MD5Main {
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; /**
* 转换字节数组为16进制字串
*
* @param b
* 字节数组
* @return 16进制字串
*/
public 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];
} /**
* MD5 摘要计算(byte[]).
*
* @param src
* byte[]
* @throws Exception
* @return byte[] 16 bit digest
*/
public static byte[] md5Digest(byte[] src) throws Exception {
java.security.MessageDigest alg = java.security.MessageDigest
.getInstance("MD5"); // MD5 is 16 bit message digest return alg.digest(src);
} /**
* MD5 摘要计算(String).
*
* @param src
* String
* @throws Exception
* @return String
*/
public static String md5Digest(String src) throws Exception {
return byteArrayToHexString(md5Digest(src.getBytes()));
} /** Test crypt */
public static void main(String[] args) {
try {
// 获得的明文数据
String desStr = "MERCHANTID=2300000003&ORDERSEQ=5465646&ORDERDATE=20100919&ORDERAMOUNT=1";
System.out.println("原文字符串:" + desStr);
// 生成MAC
String MAC = MainTest_T1.md5Digest(desStr);
System.out.println(" MAC:" + MAC);
// 使用key值生成 SIGN
String keyStr = "123456";// 使用固定key
// 获得的明文数据
desStr = "UPTRANSEQ=20080101000001&MERCHANTID=0250000001&ORDERID=2006050112564931556&PAYMENT=10000&RETNCODE=00&RETNINFO=00&PAYDATE =20060101";
// 将key值和明文数据组织成一个待签名的串
desStr = desStr + "&KEY:" + keyStr;
System.out.println("原文字符串:" + desStr);
// 生成 SIGN
String SIGN = md5Digest(desStr);
System.out.println(" SIGN:" + SIGN); } catch (Exception ex) {
ex.printStackTrace();
}
}
}

❷ MD5算法求助 用JAVA实现

package JavaBean.util;

import java.security.MessageDigest;

public class MD5Encrypt {
public MD5Encrypt() {
}

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

/**
* 转换字节数组为16进制字串
* @param b 字节数组
* @return 16进制字串
*/
public static String byteArrayToString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
//resultSb.append(byteToHexString(b[i]));//若使用本函数转换则可得到加密结果的16进制表示,即数字字母混合的形式
resultSb.append(byteToNumString(b[i]));//使用本函数则返回加密结果的10进制数字字串,即全数字形式
}
return resultSb.toString();
}

private static String byteToNumString(byte b) {

int _b = b;
if (_b < 0) {
_b = 256 + _b;
}

return String.valueOf(_b);
}

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];
}

public static String MD5Encode(String origin) {
String resultString = null;

try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
resultString =
byteArrayToString(md.digest(resultString.getBytes()));
}
catch (Exception ex) {

}
return resultString;
}

public static void main(String[] args) {
MD5Encrypt md5encrypt = new MD5Encrypt();
System.out.println(MD5Encode("10000000"));
}
}
完整的类.
调用为: md5.MD5Encode("hello");

❸ java MD5 分次分段获取整体摘要

理论来说。没有。换一个思路。

你指的是大文件的情况下,一次计算不了md5对吗。

你可以试试用流的方式读取。每次update md5对象。最后在 digest 。

FileInputStreamf=newFileInputStream(newFile("bigFile.txt"));
MessageDigestdigest=MessageDigest.getInstance("md5");
byte[]buffer=newbyte[8192];
intlen=0;
while(-1!=(len=f.read(buffer))){
digest.update(buffer,0,len);
}
byte[]md5hash=digest.digest();

or

//fast_md5lib
Stringhash=MD5.asHex(MD5.getHash(newFile(filename)));

❹ Java中怎么把密码加密!

通常不加密密码。而是用md5取出它的hash值。存到数据库里。验证时也用md5验证。这样管理员也看不到用户的密码。

❺ 如何使用JAVA md5算法

第一步:
import java.security.MessageDigest;

第二步:
private final static String[] digits = {"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "a", "b", "c", "d", "e", "f"};

第三步:
public static String byteArrayToHexString(byte[] b) {
StringBuffer sb = new StringBuffer();
for (byte aB : b) {
sb.append(byteToHexString(aB));
}
return sb.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 digits[d1] + digits[d2];
}

第五步:
public static String MD5Encode(String origin) {
String result = "";
try {
result = origin;
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(result.getBytes("UTF-8"));
result = byteArrayToHexString(md.digest());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

应用实例:
package zy.until;

import java.security.MessageDigest;

/**
* User: lxy
* Date: 2015/6/23
* Time: 15:43
*/
public class MD5 {
private final static String[] digits = {"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "a", "b", "c", "d", "e", "f"};

public static String byteArrayToHexString(byte[] b) {
StringBuffer sb = new StringBuffer();
for (byte aB : b) {
sb.append(byteToHexString(aB));
}
return sb.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 digits[d1] + digits[d2];
}

public static String MD5Encode(String origin) {
String result = "";
try {
result = origin;
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(result.getBytes("UTF-8"));
result = byteArrayToHexString(md.digest());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

}

❻ JAVA做一个简单的MD5加密...

常用的MD5加密类
package org.tool;

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

public class MD5_Encoding
{
private MessageDigest md = null;
private static MD5_Encoding md5 = null;
private static final char[] hexChars = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

/**
* Constructor is private so you must use the getInstance method
*/
private MD5_Encoding() throws NoSuchAlgorithmException
{
md = MessageDigest.getInstance("MD5");
}

/**
* This returns the singleton instance
*/
public static MD5_Encoding getInstance() throws NoSuchAlgorithmException
{
if (md5 == null)
{
md5 = new MD5_Encoding();
}
return (md5);
}
/* 将字符串使用md5加密的方法*/
public static String hashCode(String dataToHash)
throws NoSuchAlgorithmException
{
return getInstance().hashData(dataToHash.getBytes());
}

public static String hashCode(byte[] dataToHash)
throws NoSuchAlgorithmException
{
return getInstance().hashData(dataToHash);
}

public String hashData(byte[] dataToHash)
{
return hexStringFromBytes((calculateHash(dataToHash))).toLowerCase();
}

private byte[] calculateHash(byte[] dataToHash)
{
md.update(dataToHash, 0, dataToHash.length);
return (md.digest());
}

public String hexStringFromBytes(byte[] b)
{
String hex = "";
int msb;
int lsb = 0;
int i;
// MSB maps to idx 0
for (i = 0; i < b.length; i++)
{
msb = ((int) b[i] & 0x000000FF) / 16;
lsb = ((int) b[i] & 0x000000FF) % 16;
hex = hex + hexChars[msb] + hexChars[lsb];
}
return (hex);
}

public static void main(String args[]) throws Exception{
//举例对1进行加密
System.out.println(MD5_Encoding.hashCode("1"));
}
}

❼ java算法 md5加密方法

根据MD5算法的特点,我们可以把MD5加密过程看作是一个函数调用过程,建议必须做如下方式修改,这样可以保证一定程度上你的网站用户和数据安全:
1、修改MD5算法中的4个常数,这是最捷径的作法,其特点是加密后的数据和加密前非常类似,但是不会被破解
2、多次加密,对MD5加密过的数据进行二次或三次加密,或者在每次加密后从重抽取部分值进行在加密,比如“我爱你”,加密后“”,我们可以取任意一部分进行再加密,比如取前18位“1E6986ACEC7BAE541”进行再加密得到“”,这种做法修改很简单,比如asp中调用是md5("password")那么你可以改成md5(left(md5("password"),16)),这样以来就很安全了,就是你的数据被下载,破解的话也是不可能的
3、仿MD5加密,顾名思义,我们不采用MD5加密,而采用其他算法,然后取其中的部分散列,比如用SHA1或SHA64得到加密结果,然后取其中的32位或16位,很像MD5算法加密的结果,可以保证不被破解
方法有很多,我这里只是抛砖引玉,希望你在做网站的时候自己修改,可以确保万无一失,不管你用的是什么软件,希望大家谨慎一下,我们把这种改法称为MD5的私有算法或私有MD5算法。

❽ java MD5算法加密如何实现简单就是美!!!

var hexcase = 0;
var b64pad = "";
var chrsz = 8;

function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }

function md5_vm_test()
{
return hex_md5("abc") == "";
}

function core_md5(x, len)
{
x[len >> 5] |= 0x80 << ((len) % 32);
x[(((len + 64) >>> 9) << 4) + 14] = len;

var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;

for(var i = 0; i < x.length; i += 16)
{
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;

a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);

a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
}
return Array(a, b, c, d);

}

function md5_cmn(q, a, b, x, s, t)
{
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}
function core_hmac_md5(key, data)
{
var bkey = str2binl(key);
if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);

var ipad = Array(16), opad = Array(16);
for(var i = 0; i < 16; i++)
{
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
return core_md5(opad.concat(hash), 512 + 128);
}
function safe_add(x, y)
{
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
function bit_rol(num, cnt)
{
return (num << cnt) | (num >>> (32 - cnt));
}
function str2binl(str)
{
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str.length * chrsz; i += chrsz)
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
return bin;
}

function binl2str(bin)
{
var str = "";
var mask = (1 << chrsz) - 1;
for(var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
return str;
}
function binl2hex(binarray)
{
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for(var i = 0; i < binarray.length * 4; i++)
{
str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
}
return str;
}
function binl2b64(binarray)
{
var tab = "+/";
var str = "";
for(var i = 0; i < binarray.length * 4; i += 3)
{
var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16)
| (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
| ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
for(var j = 0; j < 4; j++)
{
if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
}
}
return str;
}

❾ 虔诚的求一个java的MD5加密函数,别整没用的往这复制,最好自己用过的,谢谢了。

//md5加密算法
public static String makeMD5(String password)
{
MessageDigest md;
try
{
// 生成一个MD5加密计算摘要
md = MessageDigest.getInstance("MD5");
// 计算md5函数
md.update(password.getBytes());
// digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
// BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
String pwd = new BigInteger(1, md.digest()).toString(16);
// System.err.println(pwd);
return pwd;
} catch (Exception e)
{
e.printStackTrace();
}
return password;
}

一直在用祝你好运!

阅读全文

与md5hash算法java相关的资料

热点内容
方言app有什么用 浏览:768
程序员点赞视频大全 浏览:284
命令的异同 浏览:471
加密狗是干什么工作的 浏览:389
centosinit命令 浏览:402
三年怎么算利息怎么算法 浏览:888
手机拍照根目录是哪个文件夹 浏览:968
小猫爪解压 浏览:612
源码置入微信小程序 浏览:922
如何开一家少儿编程公司 浏览:953
光伏计算日照时用什么app 浏览:234
计算阶乘的python程序 浏览:47
传奇如何选择服务器 浏览:574
英雄联盟光辉和程序员哪个厉害 浏览:253
什么是pojo编程 浏览:924
外挂编程视频 浏览:133
学javaapp 浏览:12
客户端无盘如何与服务器连接 浏览:792
狙击手命令 浏览:505
财务防雷指标公式源码 浏览:877