package md5;
import java.io.IOException;
import java.security.MessageDigest;
class MD5State {
int state[];
int count[];
byte buffer[];
public MD5State() {
buffer = new byte[64];
count = new int[2];
state = new int[4];
state[0] = 0x67452301;
state[1] = 0xefcdab89;
state[2] = 0x98badcfe;
state[3] = 0x10325476;
count[0] = count[1] = 0;
}
/** Create this State as a copy of another state */
public MD5State(MD5State from) {
this();
int i;
for (i = 0; i < buffer.length; i++)
this.buffer[i] = from.buffer[i];
for (i = 0; i < state.length; i++)
this.state[i] = from.state[i];
for (i = 0; i < count.length; i++)
this.count[i] = from.count[i];
}
};
public class MD5 {
/**
* MD5 state
*/
MD5State state;
/**
* If Final() has been called, finals is set to the current finals state.
* Any Update() causes this to be set to null.
*/
MD5State finals;
/**
* Padding for Final()
*/
static byte padding[] = { (byte) 0x80, 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 };
/**
* Initialize MD5 internal state (object can be reused just by calling
* Init() after every Final()
*/
public synchronized void Init() {
state = new MD5State();
finals = null;
}
public MD5() {
this.Init();
}
public MD5(Object ob) {
this();
Update(ob.toString());
}
public String debugDump() {
return asHex();
}
private int rotate_left(int x, int n) {
return (x << n) | (x >>> (32 - n));
}
/*
* I wonder how many loops and hoops you'll have to go through to get
* unsigned add for longs in java
*/
private int uadd(int a, int b) {
long aa, bb;
aa = ((long) a) & 0xffffffffL;
bb = ((long) b) & 0xffffffffL;
aa += bb;
return (int) (aa & 0xffffffffL);
}
private int uadd(int a, int b, int c) {
return uadd(uadd(a, b), c);
}
private int uadd(int a, int b, int c, int d) {
return uadd(uadd(a, b, c), d);
}
private int FF(int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, ((b & c) | (~b & d)), x, ac);
return uadd(rotate_left(a, s), b);
}
private int GG(int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, ((b & d) | (c & ~d)), x, ac);
return uadd(rotate_left(a, s), b);
}
private int HH(int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, (b ^ c ^ d), x, ac);
return uadd(rotate_left(a, s), b);
}
private int II(int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, (c ^ (b | ~d)), x, ac);
return uadd(rotate_left(a, s), b);
}
private int[] Decode(byte buffer[], int len, int shift) {
int out[];
int i, j;
out = new int[16];
for (i = j = 0; j < len; i++, j += 4) {
out[i] = ((int) (buffer[j + shift] & 0xff))
| (((int) (buffer[j + 1 + shift] & 0xff)) <<
| (((int) (buffer[j + 2 + shift] & 0xff)) << 16)
| (((int) (buffer[j + 3 + shift] & 0xff)) << 24);
}
return out;
}
private void Transform(MD5State state, byte buffer[], int shift) {
int a = state.state[0], b = state.state[1], c = state.state[2], d = state.state[3], x[];
x = Decode(buffer, 64, shift);
/* Round 1 */
a = FF(a, b, c, d, x[0], 7, 0xd76aa478); /* 1 */
d = FF(d, a, b, c, x[1], 12, 0xe8c7b756); /* 2 */
c = FF(c, d, a, b, x[2], 17, 0x242070db); /* 3 */
b = FF(b, c, d, a, x[3], 22, 0xc1bdceee); /* 4 */
a = FF(a, b, c, d, x[4], 7, 0xf57c0faf); /* 5 */
d = FF(d, a, b, c, x[5], 12, 0x4787c62a); /* 6 */
c = FF(c, d, a, b, x[6], 17, 0xa8304613); /* 7 */
b = FF(b, c, d, a, x[7], 22, 0xfd469501); /* 8 */
a = FF(a, b, c, d, x[8], 7, 0x698098d8); /* 9 */
d = FF(d, a, b, c, x[9], 12, 0x8b44f7af); /* 10 */
c = FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
b = FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
a = FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
d = FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
c = FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
b = FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
/* Round 2 */
a = GG(a, b, c, d, x[1], 5, 0xf61e2562); /* 17 */
d = GG(d, a, b, c, x[6], 9, 0xc040b340); /* 18 */
c = GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
b = GG(b, c, d, a, x[0], 20, 0xe9b6c7aa); /* 20 */
a = GG(a, b, c, d, x[5], 5, 0xd62f105d); /* 21 */
d = GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */
c = GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
b = GG(b, c, d, a, x[4], 20, 0xe7d3fbc8); /* 24 */
a = GG(a, b, c, d, x[9], 5, 0x21e1cde6); /* 25 */
d = GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
c = GG(c, d, a, b, x[3], 14, 0xf4d50d87); /* 27 */
b = GG(b, c, d, a, x[8], 20, 0x455a14ed); /* 28 */
a = GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
d = GG(d, a, b, c, x[2], 9, 0xfcefa3f8); /* 30 */
c = GG(c, d, a, b, x[7], 14, 0x676f02d9); /* 31 */
b = GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
/* Round 3 */
a = HH(a, b, c, d, x[5], 4, 0xfffa3942); /* 33 */
d = HH(d, a, b, c, x[8], 11, 0x8771f681); /* 34 */
c = HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
b = HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
a = HH(a, b, c, d, x[1], 4, 0xa4beea44); /* 37 */
d = HH(d, a, b, c, x[4], 11, 0x4bdecfa9); /* 38 */
c = HH(c, d, a, b, x[7], 16, 0xf6bb4b60); /* 39 */
b = HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
a = HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
d = HH(d, a, b, c, x[0], 11, 0xeaa127fa); /* 42 */
c = HH(c, d, a, b, x[3], 16, 0xd4ef3085); /* 43 */
b = HH(b, c, d, a, x[6], 23, 0x4881d05); /* 44 */
a = HH(a, b, c, d, x[9], 4, 0xd9d4d039); /* 45 */
d = HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
c = HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
b = HH(b, c, d, a, x[2], 23, 0xc4ac5665); /* 48 */
/* Round 4 */
a = II(a, b, c, d, x[0], 6, 0xf4292244); /* 49 */
d = II(d, a, b, c, x[7], 10, 0x432aff97); /* 50 */
c = II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
b = II(b, c, d, a, x[5], 21, 0xfc93a039); /* 52 */
a = II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
d = II(d, a, b, c, x[3], 10, 0x8f0ccc92); /* 54 */
c = II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
b = II(b, c, d, a, x[1], 21, 0x85845dd1); /* 56 */
a = II(a, b, c, d, x[8], 6, 0x6fa87e4f); /* 57 */
d = II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
c = II(c, d, a, b, x[6], 15, 0xa3014314); /* 59 */
b = II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
a = II(a, b, c, d, x[4], 6, 0xf7537e82); /* 61 */
d = II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
c = II(c, d, a, b, x[2], 15, 0x2ad7d2bb); /* 63 */
b = II(b, c, d, a, x[9], 21, 0xeb86d391); /* 64 */
state.state[0] += a;
state.state[1] += b;
state.state[2] += c;
state.state[3] += d;
}
public void Update(MD5State stat, byte buffer[], int offset, int length) {
int index, partlen, i, start;
finals = null;
/* Length can be told to be shorter, but not inter */
if ((length - offset) > buffer.length)
length = buffer.length - offset;
/* compute number of bytes mod 64 */
index = (int) (stat.count[0] >>> 3) & 0x3f;
if ((stat.count[0] += (length << 3)) < (length << 3))
stat.count[1]++;
stat.count[1] += length >>> 29;
partlen = 64 - index;
if (length >= partlen) {
for (i = 0; i < partlen; i++)
stat.buffer[i + index] = buffer[i + offset];
Transform(stat, stat.buffer, 0);
for (i = partlen; (i + 63) < length; i += 64)
Transform(stat, buffer, i);
index = 0;
} else
i = 0;
/* buffer remaining input */
if (i < length) {
start = i;
for (; i < length; i++)
stat.buffer[index + i - start] = buffer[i + offset];
}
}
public void Update(byte buffer[], int offset, int length) {
Update(this.state, buffer, offset, length);
}
public void Update(byte buffer[], int length) {
Update(this.state, buffer, 0, length);
}
public void Update(byte buffer[]) {
Update(buffer, 0, buffer.length);
}
public void Update(byte b) {
byte buffer[] = new byte[1];
buffer[0] = b;
Update(buffer, 1);
}
public void Update(String s) {
byte chars[];
chars = s.getBytes();
Update(chars, chars.length);
}
private byte[] Encode(int input[], int len) {
int i, j;
byte out[];
out = new byte[len];
for (i = j = 0; j < len; i++, j += 4) {
out[j] = (byte) (input[i] & 0xff);
out[j + 1] = (byte) ((input[i] >>> & 0xff);
out[j + 2] = (byte) ((input[i] >>> 16) & 0xff);
out[j + 3] = (byte) ((input[i] >>> 24) & 0xff);
}
return out;
}
public synchronized byte[] Final() {
byte bits[];
int index, padlen;
MD5State fin;
if (null == finals) {
fin = new MD5State(state);
bits = Encode(fin.count,;
index = (int) ((fin.count[0] >>> 3) & 0x3f);
padlen = (index < 56) ? (56 - index) : (120 - index);
Update(fin, padding, 0, padlen);
/**/
Update(fin, bits, 0,;
/* Update() sets finalds to null */
finals = fin;
}
return Encode(finals.state, 16);
}
public static String asHex(byte hash[]) {
StringBuilder buf = new StringBuilder(hash.length * 2);
int i;
for (i = 0; i < hash.length; i++) {
if (((int) hash[i] & 0xff) < 0x10)
buf.append("0");
buf.append(Long.toString((int) hash[i] & 0xff, 16));
}
return buf.toString();
}
public String asHex() {
return asHex(this.Final());
}
public static String md5crypt(String input) {
MD5 md5 = new MD5();
md5.Init();
md5.Update(input);
return md5.asHex();
}
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) {
System.out.println(e.getMessage());
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();
}
public static String encodeString(String str) {
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
return encoder.encodeBuffer(str.getBytes()).trim();
}
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());
}
}
/**
* 算法可以使用:SHA-512,MD5,SHA-256等
* @param args
*/
public static void main(String[] args) {
String passwd = "admin";
String encodeString = encodePassword(passwd, "MD5");
System.out.println(encodeString);
System.out.println(md5crypt(passwd));
}
}
import java.io.IOException;
import java.security.MessageDigest;
class MD5State {
int state[];
int count[];
byte buffer[];
public MD5State() {
buffer = new byte[64];
count = new int[2];
state = new int[4];
state[0] = 0x67452301;
state[1] = 0xefcdab89;
state[2] = 0x98badcfe;
state[3] = 0x10325476;
count[0] = count[1] = 0;
}
/** Create this State as a copy of another state */
public MD5State(MD5State from) {
this();
int i;
for (i = 0; i < buffer.length; i++)
this.buffer[i] = from.buffer[i];
for (i = 0; i < state.length; i++)
this.state[i] = from.state[i];
for (i = 0; i < count.length; i++)
this.count[i] = from.count[i];
}
};
public class MD5 {
/**
* MD5 state
*/
MD5State state;
/**
* If Final() has been called, finals is set to the current finals state.
* Any Update() causes this to be set to null.
*/
MD5State finals;
/**
* Padding for Final()
*/
static byte padding[] = { (byte) 0x80, 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 };
/**
* Initialize MD5 internal state (object can be reused just by calling
* Init() after every Final()
*/
public synchronized void Init() {
state = new MD5State();
finals = null;
}
public MD5() {
this.Init();
}
public MD5(Object ob) {
this();
Update(ob.toString());
}
public String debugDump() {
return asHex();
}
private int rotate_left(int x, int n) {
return (x << n) | (x >>> (32 - n));
}
/*
* I wonder how many loops and hoops you'll have to go through to get
* unsigned add for longs in java
*/
private int uadd(int a, int b) {
long aa, bb;
aa = ((long) a) & 0xffffffffL;
bb = ((long) b) & 0xffffffffL;
aa += bb;
return (int) (aa & 0xffffffffL);
}
private int uadd(int a, int b, int c) {
return uadd(uadd(a, b), c);
}
private int uadd(int a, int b, int c, int d) {
return uadd(uadd(a, b, c), d);
}
private int FF(int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, ((b & c) | (~b & d)), x, ac);
return uadd(rotate_left(a, s), b);
}
private int GG(int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, ((b & d) | (c & ~d)), x, ac);
return uadd(rotate_left(a, s), b);
}
private int HH(int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, (b ^ c ^ d), x, ac);
return uadd(rotate_left(a, s), b);
}
private int II(int a, int b, int c, int d, int x, int s, int ac) {
a = uadd(a, (c ^ (b | ~d)), x, ac);
return uadd(rotate_left(a, s), b);
}
private int[] Decode(byte buffer[], int len, int shift) {
int out[];
int i, j;
out = new int[16];
for (i = j = 0; j < len; i++, j += 4) {
out[i] = ((int) (buffer[j + shift] & 0xff))
| (((int) (buffer[j + 1 + shift] & 0xff)) <<
| (((int) (buffer[j + 2 + shift] & 0xff)) << 16)
| (((int) (buffer[j + 3 + shift] & 0xff)) << 24);
}
return out;
}
private void Transform(MD5State state, byte buffer[], int shift) {
int a = state.state[0], b = state.state[1], c = state.state[2], d = state.state[3], x[];
x = Decode(buffer, 64, shift);
/* Round 1 */
a = FF(a, b, c, d, x[0], 7, 0xd76aa478); /* 1 */
d = FF(d, a, b, c, x[1], 12, 0xe8c7b756); /* 2 */
c = FF(c, d, a, b, x[2], 17, 0x242070db); /* 3 */
b = FF(b, c, d, a, x[3], 22, 0xc1bdceee); /* 4 */
a = FF(a, b, c, d, x[4], 7, 0xf57c0faf); /* 5 */
d = FF(d, a, b, c, x[5], 12, 0x4787c62a); /* 6 */
c = FF(c, d, a, b, x[6], 17, 0xa8304613); /* 7 */
b = FF(b, c, d, a, x[7], 22, 0xfd469501); /* 8 */
a = FF(a, b, c, d, x[8], 7, 0x698098d8); /* 9 */
d = FF(d, a, b, c, x[9], 12, 0x8b44f7af); /* 10 */
c = FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
b = FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
a = FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
d = FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
c = FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
b = FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
/* Round 2 */
a = GG(a, b, c, d, x[1], 5, 0xf61e2562); /* 17 */
d = GG(d, a, b, c, x[6], 9, 0xc040b340); /* 18 */
c = GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
b = GG(b, c, d, a, x[0], 20, 0xe9b6c7aa); /* 20 */
a = GG(a, b, c, d, x[5], 5, 0xd62f105d); /* 21 */
d = GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */
c = GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
b = GG(b, c, d, a, x[4], 20, 0xe7d3fbc8); /* 24 */
a = GG(a, b, c, d, x[9], 5, 0x21e1cde6); /* 25 */
d = GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
c = GG(c, d, a, b, x[3], 14, 0xf4d50d87); /* 27 */
b = GG(b, c, d, a, x[8], 20, 0x455a14ed); /* 28 */
a = GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
d = GG(d, a, b, c, x[2], 9, 0xfcefa3f8); /* 30 */
c = GG(c, d, a, b, x[7], 14, 0x676f02d9); /* 31 */
b = GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
/* Round 3 */
a = HH(a, b, c, d, x[5], 4, 0xfffa3942); /* 33 */
d = HH(d, a, b, c, x[8], 11, 0x8771f681); /* 34 */
c = HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
b = HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
a = HH(a, b, c, d, x[1], 4, 0xa4beea44); /* 37 */
d = HH(d, a, b, c, x[4], 11, 0x4bdecfa9); /* 38 */
c = HH(c, d, a, b, x[7], 16, 0xf6bb4b60); /* 39 */
b = HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
a = HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
d = HH(d, a, b, c, x[0], 11, 0xeaa127fa); /* 42 */
c = HH(c, d, a, b, x[3], 16, 0xd4ef3085); /* 43 */
b = HH(b, c, d, a, x[6], 23, 0x4881d05); /* 44 */
a = HH(a, b, c, d, x[9], 4, 0xd9d4d039); /* 45 */
d = HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
c = HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
b = HH(b, c, d, a, x[2], 23, 0xc4ac5665); /* 48 */
/* Round 4 */
a = II(a, b, c, d, x[0], 6, 0xf4292244); /* 49 */
d = II(d, a, b, c, x[7], 10, 0x432aff97); /* 50 */
c = II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
b = II(b, c, d, a, x[5], 21, 0xfc93a039); /* 52 */
a = II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
d = II(d, a, b, c, x[3], 10, 0x8f0ccc92); /* 54 */
c = II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
b = II(b, c, d, a, x[1], 21, 0x85845dd1); /* 56 */
a = II(a, b, c, d, x[8], 6, 0x6fa87e4f); /* 57 */
d = II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
c = II(c, d, a, b, x[6], 15, 0xa3014314); /* 59 */
b = II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
a = II(a, b, c, d, x[4], 6, 0xf7537e82); /* 61 */
d = II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
c = II(c, d, a, b, x[2], 15, 0x2ad7d2bb); /* 63 */
b = II(b, c, d, a, x[9], 21, 0xeb86d391); /* 64 */
state.state[0] += a;
state.state[1] += b;
state.state[2] += c;
state.state[3] += d;
}
public void Update(MD5State stat, byte buffer[], int offset, int length) {
int index, partlen, i, start;
finals = null;
/* Length can be told to be shorter, but not inter */
if ((length - offset) > buffer.length)
length = buffer.length - offset;
/* compute number of bytes mod 64 */
index = (int) (stat.count[0] >>> 3) & 0x3f;
if ((stat.count[0] += (length << 3)) < (length << 3))
stat.count[1]++;
stat.count[1] += length >>> 29;
partlen = 64 - index;
if (length >= partlen) {
for (i = 0; i < partlen; i++)
stat.buffer[i + index] = buffer[i + offset];
Transform(stat, stat.buffer, 0);
for (i = partlen; (i + 63) < length; i += 64)
Transform(stat, buffer, i);
index = 0;
} else
i = 0;
/* buffer remaining input */
if (i < length) {
start = i;
for (; i < length; i++)
stat.buffer[index + i - start] = buffer[i + offset];
}
}
public void Update(byte buffer[], int offset, int length) {
Update(this.state, buffer, offset, length);
}
public void Update(byte buffer[], int length) {
Update(this.state, buffer, 0, length);
}
public void Update(byte buffer[]) {
Update(buffer, 0, buffer.length);
}
public void Update(byte b) {
byte buffer[] = new byte[1];
buffer[0] = b;
Update(buffer, 1);
}
public void Update(String s) {
byte chars[];
chars = s.getBytes();
Update(chars, chars.length);
}
private byte[] Encode(int input[], int len) {
int i, j;
byte out[];
out = new byte[len];
for (i = j = 0; j < len; i++, j += 4) {
out[j] = (byte) (input[i] & 0xff);
out[j + 1] = (byte) ((input[i] >>> & 0xff);
out[j + 2] = (byte) ((input[i] >>> 16) & 0xff);
out[j + 3] = (byte) ((input[i] >>> 24) & 0xff);
}
return out;
}
public synchronized byte[] Final() {
byte bits[];
int index, padlen;
MD5State fin;
if (null == finals) {
fin = new MD5State(state);
bits = Encode(fin.count,;
index = (int) ((fin.count[0] >>> 3) & 0x3f);
padlen = (index < 56) ? (56 - index) : (120 - index);
Update(fin, padding, 0, padlen);
/**/
Update(fin, bits, 0,;
/* Update() sets finalds to null */
finals = fin;
}
return Encode(finals.state, 16);
}
public static String asHex(byte hash[]) {
StringBuilder buf = new StringBuilder(hash.length * 2);
int i;
for (i = 0; i < hash.length; i++) {
if (((int) hash[i] & 0xff) < 0x10)
buf.append("0");
buf.append(Long.toString((int) hash[i] & 0xff, 16));
}
return buf.toString();
}
public String asHex() {
return asHex(this.Final());
}
public static String md5crypt(String input) {
MD5 md5 = new MD5();
md5.Init();
md5.Update(input);
return md5.asHex();
}
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) {
System.out.println(e.getMessage());
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();
}
public static String encodeString(String str) {
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
return encoder.encodeBuffer(str.getBytes()).trim();
}
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());
}
}
/**
* 算法可以使用:SHA-512,MD5,SHA-256等
* @param args
*/
public static void main(String[] args) {
String passwd = "admin";
String encodeString = encodePassword(passwd, "MD5");
System.out.println(encodeString);
System.out.println(md5crypt(passwd));
}
}
- MD5.rar (3.4 KB)
- 下载次数: 2
发表评论
-
linux发布web项目
2021-08-30 17:27 268虚拟机 192.168.64.170 root hadoop ... -
linux服务器tomcat发布项目
2021-07-12 21:28 936虚拟机 192.168.64.215 安装jdk cd /h ... -
linux服务器tomcat发布项目
2021-07-12 21:25 0虚拟机 192.168.64.215 安装jdk cd /h ... -
nginx反向代理tomcat集群
2018-04-23 21:18 695dsCentOS-empty虚拟机 用户名:root 密码:r ... -
nginx反向代理tomcat
2018-04-23 21:07 779dsCentOS-empty虚拟机为桥接网络模式 用户名 ro ... -
nginx反向代理tomcat
2018-04-23 20:29 0dsCentOS-empty虚拟机为桥接网络模式 用户名 ro ...
相关推荐
下面是一个简单的Java MD5加密的代码示例,对应于你提供的`TestMD5.java`文件: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class TestMD5 { public ...
在Java EE(J2EE)环境中,MD5加密通常结合各种框架如Struts、Spring和Hibernate来实现。Struts是MVC架构的Web开发框架,Spring提供了全面的DI(依赖注入)和AOP(面向切面编程)功能,而Hibernate则是一个流行的ORM...
Java MD5加密是一种广泛应用于密码保护、数据...这个例子可以帮助开发者更好地理解如何在Java环境中实现MD5加密,提高密码存储的安全性。在学习和使用这些示例时,应结合理论知识,以确保正确理解和应用MD5加密技术。
java MD5加密 代码实例 没有bug 典型例子
在Java中实现MD5加密,主要涉及`java.security.MessageDigest`类。以下是一个简单的MD5加密示例: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD...
在Java中,MD5加密是将任意长度的字符串转化为固定长度的128位(16字节)摘要的过程,通常以32位十六进制数字的形式展示。MD5的主要应用在于验证数据的完整性和一致性,例如存储密码时,我们会对原始密码进行MD5加密...
在描述中提到的"MD5加密解密demo",实际上MD5并不具备可逆的加密特性。MD5是一个单向函数,即给定任意输入,可以很容易地计算出固定的输出(摘要),但无法根据输出反推出原始输入。因此,我们通常不会说"MD5解密",...
下面我们将详细探讨如何用Java实现MD5加密,并通过实例来加深理解。 首先,MD5加密的基本原理是通过一个特定的算法,将输入的字符串转换成32位的十六进制数字。这个过程是单向的,即从原始信息到MD5摘要容易,但从...
本篇文章将详细介绍Java中如何实现MD5加密,并提供相关的代码实现。 MD5加密的基本流程: 1. 对原始数据进行预处理,包括填充和添加长度信息。 2. 将预处理后的数据转换为一个初始的128位(16字节)的中间状态。 3....
在Java中,我们可以使用`java.security.MessageDigest`类来实现MD5加密。 MD5的主要应用场景包括文件完整性校验、用户密码存储(虽然现在MD5因为安全性问题不建议用于密码存储)和数据验证。其优点在于计算速度快,...
在Java中,我们可以使用`java.security.MessageDigest`类来实现MD5加密。这个类提供了对各种消息摘要算法的支持,包括MD5。以下是一个简单的示例,展示了如何使用Java调用MD5加密来计算一个字符串的摘要值: ```...
在Java中,实现MD5加密并不复杂,因为Java的标准库`java.security.MessageDigest`类提供了相关的支持。 以下是一个简单的Java MD5加密的步骤详解: 1. **导入相关包**:在Java中,实现MD5加密不需要额外导入第三方...
在标题提到的"可用的jsp的MD5加密的javaBean",我们可以理解为这是一个专门用于在JSP(JavaServer Pages)环境中处理MD5加密的Java类。开发者可能在寻找如何在JSP中对用户输入的数据进行MD5加密,例如密码,以提高...
在JAVA中,MD5主要用于数据的校验和密码的加密,因为它产生的哈希值具有不可逆性,即无法通过哈希值还原原始数据,这为数据安全提供了基础。 MD5算法的基本步骤包括: 1. **初始化:** MD5算法有四个32位的中间...
Java实现MD5加密算法的简单实例 Java中实现MD5加密算法的简单实例主要介绍了如何使用Java语言来实现MD5加密算法,提供了一个简单的实例帮助大家应用这样的加密算法。MD5加密算法是一种不可逆的加密算法,破解的难度...
在这个例子中,`encryptWithSalt`方法接收一个密码和一个盐值,将它们组合后进行MD5加密,并返回16进制的哈希值。 总之,Java中的MD5加密是通过`MessageDigest`类实现的,适用于简单的加密需求。然而,为了增强安全...
在Java中实现MD5加密,可以使用`java.security.MessageDigest`类。以下是一个简单的示例: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5...
JAVA实现MD5加密的例子,调用getMD5String方法,双次MD5加密,单次MD5加密
在这个例子中,`CalculateMD5`函数接收一个字符串`Data`,使用`TIdHashMD5`对象对其进行MD5加密,并返回16进制格式的MD5摘要。 另外,如果你不想使用第三方库,也可以自行编写MD5算法。MD5算法包含四个主要的步骤:...