记得我离开我我可以吗c的贴吧

能帮我一个忙嘛?吧这个C的MD5算法改成JAVA的 - ITeye问答
在做移动的缴费应用,移动需要做连接加密认证,给了一个C的MD5算法,可是我们的程序是用JAVA做的,C的我也看不懂
// MD5.cpp: implementation of the MD5 class.
#include &string.h&
#include &stdio.h&
#include &stdlib.h&
#include "MD5.h"
#include "MD5Defines.h"
char CMD5::m_szResult[260];
char *CMD5::GetMD5(unsigned char* pBuf, unsigned int nLength)
//calculate and return the checksum
MD5.Update( pBuf, nLength );
MD5.Final();
return m_szR
char *CMD5::GetMD5Bits(unsigned char* pBuf, unsigned int nLength)
//calculate and return the checksum
MD5.Update( pBuf, nLength );
MD5.FinalBits();
return m_szR
unsigned int CMD5::RotateLeft(unsigned int x, int n)
//rotate and return x
return (x && n) | (x && (32-n));
void CMD5::FF( unsigned int& A, unsigned int B, unsigned int C, unsigned int D, unsigned int X, unsigned int S, unsigned int T)
unsigned int F = (B & C) | (~B & D);
A += F + X + T;
A = RotateLeft(A, S);
void CMD5::GG( unsigned int& A, unsigned int B, unsigned int C, unsigned int D, unsigned int X, unsigned int S, unsigned int T)
unsigned int G = (B & D) | (C & ~D);
A += G + X + T;
A = RotateLeft(A, S);
void CMD5::HH( unsigned int& A, unsigned int B, unsigned int C, unsigned int D, unsigned int X, unsigned int S, unsigned int T)
unsigned int H = (B ^ C ^ D);
A += H + X + T;
A = RotateLeft(A, S);
void CMD5::II( unsigned int& A, unsigned int B, unsigned int C, unsigned int D, unsigned int X, unsigned int S, unsigned int T)
unsigned int I = (C ^ (B | ~D));
A += I + X + T;
A = RotateLeft(A, S);
void CMD5::ByteToDWord(unsigned int* Output, unsigned char* Input, unsigned int nLength)
//initialisations
unsigned int i=0; //index to Output array
unsigned int j=0; //index to Input array
//transfer the data by shifting and copying
for ( ; j & nL i++, j += 4)
Output[i] = (unsigned int)Input[j]
(unsigned int)Input[j+1] && 8 |
(unsigned int)Input[j+2] && 16 |
(unsigned int)Input[j+3] && 24;
void CMD5::ByteToBits(unsigned char c,char *dst)
//initialisations
//rotate loop test bits 0-7 1='1' 0='0'
while(i&8)
c&0x80?dst[i++]='1':dst[i++]='0';
//terminated by '\0'
void CMD5::Transform(unsigned char Block[64])
//initialise local data with current checksum
unsigned int a = m_lMD5[0];
unsigned int b = m_lMD5[1];
unsigned int c = m_lMD5[2];
unsigned int d = m_lMD5[3];
//copy BYTES from input 'Block' to an array of ULONGS 'X'
unsigned int X[16];
ByteToDWord( X, Block, 64 );
//Perform Round 1 of the transformation
FF (a, b, c, d, X[ 0], MD5_S11, MD5_T01);
FF (d, a, b, c, X[ 1], MD5_S12, MD5_T02);
FF (c, d, a, b, X[ 2], MD5_S13, MD5_T03);
FF (b, c, d, a, X[ 3], MD5_S14, MD5_T04);
FF (a, b, c, d, X[ 4], MD5_S11, MD5_T05);
FF (d, a, b, c, X[ 5], MD5_S12, MD5_T06);
FF (c, d, a, b, X[ 6], MD5_S13, MD5_T07);
FF (b, c, d, a, X[ 7], MD5_S14, MD5_T08);
FF (a, b, c, d, X[ 8], MD5_S11, MD5_T09);
FF (d, a, b, c, X[ 9], MD5_S12, MD5_T10);
FF (c, d, a, b, X[10], MD5_S13, MD5_T11);
FF (b, c, d, a, X[11], MD5_S14, MD5_T12);
FF (a, b, c, d, X[12], MD5_S11, MD5_T13);
FF (d, a, b, c, X[13], MD5_S12, MD5_T14);
FF (c, d, a, b, X[14], MD5_S13, MD5_T15);
FF (b, c, d, a, X[15], MD5_S14, MD5_T16);
//Perform Round 2 of the transformation
GG (a, b, c, d, X[ 1], MD5_S21, MD5_T17);
GG (d, a, b, c, X[ 6], MD5_S22, MD5_T18);
GG (c, d, a, b, X[11], MD5_S23, MD5_T19);
GG (b, c, d, a, X[ 0], MD5_S24, MD5_T20);
GG (a, b, c, d, X[ 5], MD5_S21, MD5_T21);
GG (d, a, b, c, X[10], MD5_S22, MD5_T22);
GG (c, d, a, b, X[15], MD5_S23, MD5_T23);
GG (b, c, d, a, X[ 4], MD5_S24, MD5_T24);
GG (a, b, c, d, X[ 9], MD5_S21, MD5_T25);
GG (d, a, b, c, X[14], MD5_S22, MD5_T26);
GG (c, d, a, b, X[ 3], MD5_S23, MD5_T27);
GG (b, c, d, a, X[ 8], MD5_S24, MD5_T28);
GG (a, b, c, d, X[13], MD5_S21, MD5_T29);
GG (d, a, b, c, X[ 2], MD5_S22, MD5_T30);
GG (c, d, a, b, X[ 7], MD5_S23, MD5_T31);
GG (b, c, d, a, X[12], MD5_S24, MD5_T32);
//Perform Round 3 of the transformation
HH (a, b, c, d, X[ 5], MD5_S31, MD5_T33);
HH (d, a, b, c, X[ 8], MD5_S32, MD5_T34);
HH (c, d, a, b, X[11], MD5_S33, MD5_T35);
HH (b, c, d, a, X[14], MD5_S34, MD5_T36);
HH (a, b, c, d, X[ 1], MD5_S31, MD5_T37);
HH (d, a, b, c, X[ 4], MD5_S32, MD5_T38);
HH (c, d, a, b, X[ 7], MD5_S33, MD5_T39);
HH (b, c, d, a, X[10], MD5_S34, MD5_T40);
HH (a, b, c, d, X[13], MD5_S31, MD5_T41);
HH (d, a, b, c, X[ 0], MD5_S32, MD5_T42);
HH (c, d, a, b, X[ 3], MD5_S33, MD5_T43);
HH (b, c, d, a, X[ 6], MD5_S34, MD5_T44);
HH (a, b, c, d, X[ 9], MD5_S31, MD5_T45);
HH (d, a, b, c, X[12], MD5_S32, MD5_T46);
HH (c, d, a, b, X[15], MD5_S33, MD5_T47);
HH (b, c, d, a, X[ 2], MD5_S34, MD5_T48);
//Perform Round 4 of the transformation
II (a, b, c, d, X[ 0], MD5_S41, MD5_T49);
II (d, a, b, c, X[ 7], MD5_S42, MD5_T50);
II (c, d, a, b, X[14], MD5_S43, MD5_T51);
II (b, c, d, a, X[ 5], MD5_S44, MD5_T52);
II (a, b, c, d, X[12], MD5_S41, MD5_T53);
II (d, a, b, c, X[ 3], MD5_S42, MD5_T54);
II (c, d, a, b, X[10], MD5_S43, MD5_T55);
II (b, c, d, a, X[ 1], MD5_S44, MD5_T56);
II (a, b, c, d, X[ 8], MD5_S41, MD5_T57);
II (d, a, b, c, X[15], MD5_S42, MD5_T58);
II (c, d, a, b, X[ 6], MD5_S43, MD5_T59);
II (b, c, d, a, X[13], MD5_S44, MD5_T60);
II (a, b, c, d, X[ 4], MD5_S41, MD5_T61);
II (d, a, b, c, X[11], MD5_S42, MD5_T62);
II (c, d, a, b, X[ 2], MD5_S43, MD5_T63);
II (b, c, d, a, X[ 9], MD5_S44, MD5_T64);
//add the transformed values to the current checksum
m_lMD5[0] +=
m_lMD5[1] +=
m_lMD5[2] +=
m_lMD5[3] +=
CMD5::CMD5()
// zero members
memset( m_lpszBuffer, 0, 64 );
m_nCount[0] = m_nCount[1] = 0;
// Load magic state initialization constants
m_lMD5[0] = MD5_INIT_STATE_0;
m_lMD5[1] = MD5_INIT_STATE_1;
m_lMD5[2] = MD5_INIT_STATE_2;
m_lMD5[3] = MD5_INIT_STATE_3;
void CMD5::DWordToByte(unsigned char* Output, unsigned int* Input, unsigned int nLength )
//transfer the data by shifting and copying
unsigned int i = 0;
unsigned int j = 0;
for ( ; j & nL i++, j += 4)
Output[j] =
(unsigned char)(Input[i] & 0xff);
Output[j+1] = (unsigned char)((Input[i] && 8) & 0xff);
Output[j+2] = (unsigned char)((Input[i] && 16) & 0xff);
Output[j+3] = (unsigned char)((Input[i] && 24) & 0xff);
void CMD5::Final()
//Save number of bits
unsigned char Bits[8];
DWordToByte( Bits, m_nCount, 8 );
//Pad out to 56 mod 64.
unsigned int nIndex = (unsigned int)((m_nCount[0] && 3) & 0x3f);
unsigned int nPadLen = (nIndex & 56) ? (56 - nIndex) : (120 - nIndex);
Update( PADDING, nPadLen );
//Append length (before padding)
Update( Bits, 8 );
//Store final state in 'lpszMD5'
const int nMD5Size = 16;
unsigned char lpszMD5[ nMD5Size ];
DWordToByte( lpszMD5, m_lMD5, nMD5Size );
memset(m_szResult,0,sizeof(m_szResult));
//Convert the hexadecimal checksum to a CString
for ( int i=0; i & nMD5S i++)
char Str[10]={0};
if (lpszMD5[i] == 0) {
Str[0]='0';Str[1]='0';Str[2]=0;
else if (lpszMD5[i] &= 15)
sprintf(Str,"0%x",lpszMD5[i]);
sprintf(Str,"%x",lpszMD5[i]);
strcat(m_szResult,Str);
void CMD5::FinalBits()
//Save number of bits
unsigned char Bits[8];
DWordToByte( Bits, m_nCount, 8 );
//Pad out to 56 mod 64.
unsigned int nIndex = (unsigned int)((m_nCount[0] && 3) & 0x3f);
unsigned int nPadLen = (nIndex & 56) ? (56 - nIndex) : (120 - nIndex);
Update( PADDING, nPadLen );
//Append length (before padding)
Update( Bits, 8 );
//Store final state in 'lpszMD5'
const int nMD5Size = 16;
unsigned char lpszMD5[ nMD5Size ];
DWordToByte( lpszMD5, m_lMD5, nMD5Size );
memset(m_szResult,0,sizeof(m_szResult));
//Convert the hexadecimal checksum to a CString
for ( int i=0; i & nMD5S i++)
char Str[10]={0};
ByteToBits(lpszMD5[i],Str);
strcat(m_szResult,Str);
void CMD5::Update( unsigned char* Input, unsigned int nInputLen )
//Compute number of bytes mod 64
unsigned int nIndex = (unsigned int)((m_nCount[0] && 3) & 0x3F);
//Update number of bits
if ( ( m_nCount[0] += nInputLen && 3 )
( nInputLen && 3) )
m_nCount[1]++;
m_nCount[1] += (nInputLen && 29);
//Transform as many times as possible.
unsigned int i=0;
unsigned int nPartLen = 64 - nI
if (nInputLen &= nPartLen)
memcpy( &m_lpszBuffer[nIndex], Input, nPartLen );
Transform( m_lpszBuffer );
for (i = nPartL i + 63 & nInputL i += 64)
Transform( &Input[i] );
nIndex = 0;
// Buffer remaining input
memcpy( &m_lpszBuffer[nIndex], &Input[i], nInputLen-i);
直接找他们再要java版的啊... 一般加密算法改好之后都会有各个版本的
你试试直接要
MD5应该只是加密的一部分吧 一般他们还需要解密啊 MD5搞个毛
还是直接使用java原生的MD5算法吧...
或者使用Apache 下的Commons库也可以.
Java有标准的MD5实现。直接用API提供的就是了
既然是企业进行开发,lz应该讲求效率,赶紧用java MD5算法实现即可,既不需要参考移动提供的C版本(除非移动用了自己的MD5变种,不是标准MD5),也不用自己实现MD5算法(除非学习用)。
Java有标准的MD5实现。直接用API提供的就是了。
MD5算法封装带测试类
package com.demo.
import java.io.FileInputS
import java.io.InputS
import java.security.MessageD
public class DigSecurity {
/** 二进制摘要 */
public String getDigForFile(String fileAbsolutePath, String type)
throws Exception {
InputStream fis = new FileInputStream(fileAbsolutePath);
byte[] buffer = new byte[1024];
MessageDigest md5 = MessageDigest.getInstance(type);
int numRead = 0;
while ((numRead = fis.read(buffer)) & 0) {
md5.update(buffer, 0, numRead);
fis.close();
return toHexString(md5.digest());
/** 文本摘要 */
public String getDigForText(String text, String type) throws Exception {
MessageDigest md5 = MessageDigest.getInstance(type);
byte[] digest = md5.digest(text.getBytes());
return toHexString(digest);
private static String toHexString(byte[] b) {
StringBuffer sb = new StringBuffer(b.length * 2);
for (int i = 0; i & b. i++) {
sb.append(hexChar[(b[i] & 0xf0) &&& 4]);
sb.append(hexChar[b[i] & 0x0f]);
return sb.toString();
public static void main(String[] args) {
DigSecurity md5 = DigSecurity.getInstance();
String md5ForText1 = md5.getDigForText("testText", "MD5");
System.out.println(md5ForText1);
System.out.println();
} catch (Exception e) {
e.printStackTrace();
// public static char[] hexChar = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static char[] hexChar = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
public static DigSecurity getInstance() {
return InstanceHolder.
private static final class InstanceHolder {
private static final DigSecurity instance = new DigSecurity();
public class Encryptor {
* 下面这些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
private byte[] buffer = new byte[64]; // input buffer
* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的   16进制ASCII表示.
public String digestHexS
* 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 digestHexS
// 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数
public Encryptor() {
md5Init();
/* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */
private void md5Init() {
count[0] = 0L;
count[1] = 0L;
// /* Load magic initialization constants.
state[0] = 0xL;
state[1] = 0xefcdab89L;
state[2] = 0x98badcfeL;
state[3] = 0xL;
* F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是
* 简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们   实现成了private方法,名字保持了原来C中的。
private long F(long x, long y, long z) {
return (x & y) | ((~x) & z);
private long G(long x, long y, long z) {
return (x & z) | (y & (~z));
private long H(long x, long y, long z) {
return x ^ y ^
private long I(long x, long y, long z) {
return y ^ (x | (~z));
* FF,GG,HH和II将调用F,G,H,I进行近一步变换 FF, GG, HH, and II transformations for
* rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent
* recomputation.
private long FF(long a, long b, long c, long d, long x, long s, long ac) {
a += F(b, c, d) + x +
a = ((int) a && s) | ((int) a &&& (32 - s));
private long GG(long a, long b, long c, long d, long x, long s, long ac) {
a += G(b, c, d) + x +
a = ((int) a && s) | ((int) a &&& (32 - s));
private long HH(long a, long b, long c, long d, long x, long s, long ac) {
a += H(b, c, d) + x +
a = ((int) a && s) | ((int) a &&& (32 - s));
private long II(long a, long b, long c, long d, long x, long s, long ac) {
a += I(b, c, d) + x +
a = ((int) a && s) | ((int) a &&& (32 - s));
* md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个
* 函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的
private void md5Update(byte[] inbuf, int inputLen) {
int i, index, partL
byte[] block = new byte[64];
index = (int) (count[0] &&& 3) & 0x3F;
// /* Update number of bits */
if ((count[0] += (inputLen && 3)) & (inputLen && 3)) {
count[1]++;
count[1] += (inputLen &&& 29);
partLen = 64 -
// Transform as many times as possible.
if (inputLen &= partLen) {
md5Memcpy(buffer, inbuf, index, 0, partLen);
md5Transform(buffer);
for (i = partL i + 63 & inputL i += 64) {
md5Memcpy(block, inbuf, 0, i, 64);
md5Transform(block);
index = 0;
// /* Buffer remaining input */
md5Memcpy(buffer, inbuf, index, i, inputLen - i);
* md5Final整理和填写输出结果
private void md5Final() {
byte[] bits = new byte[8];
int index, padL
// /* Save number of bits */
Encode(bits, count, 8);
// /* Pad out to 56 mod 64.
index = (int) (count[0] &&& 3) & 0x3f;
padLen = (index & 56) ? (56 - index) : (120 - index);
md5Update(PADDING, padLen);
// /* Append length (before padding) */
md5Update(bits, 8);
// /* Store state in digest */
Encode(digest, state, 16);
* md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的      
* 字节拷贝到output的outpos位置开始
private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,
int len) {
for (i = 0; i & i++) {
output[outpos + i] = input[inpos + i];
* md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节
private void md5Transform(byte block[]) {
long a = state[0], b = state[1], c = state[2], d = state[3];
long[] x = new long[16];
Decode(x, block, 64);
/* Round 1 */
a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */
d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */
c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */
b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */
a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */
d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */
c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */
b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */
a = FF(a, b, c, d, x[8], S11, 0xL); /* 9 */
d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */
c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */
b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */
a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */
d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */
c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */
b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */
/* Round 2 */
a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */
d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */
c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */
b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */
a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */
d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */
c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */
b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */
a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */
d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */
c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */
b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */
a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */
d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */
c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */
b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */
/* Round 3 */
a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */
d = HH(d, a, b, c, x[8], S32, 0xL); /* 34 */
c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */
b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */
a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */
d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */
c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */
b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */
a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */
d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */
c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */
b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */
a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */
d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */
c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */
b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */
/* Round 4 */
a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */
d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */
c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */
b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */
a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */
d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */
c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */
b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */
a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */
d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */
c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */
b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */
a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */
d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */
c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */
b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */
state[0] +=
state[1] +=
state[2] +=
state[3] +=
* Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的, 只拆低32bit,以适应原始C实现的用途
private void Encode(byte[] output, long[] input, int len) {
for (i = 0, j = 0; j & i++, j += 4) {
output[j] = (byte) (input[i] & 0xffL);
output[j + 1] = (byte) ((input[i] &&& 8) & 0xffL);
output[j + 2] = (byte) ((input[i] &&& 16) & 0xffL);
output[j + 3] = (byte) ((input[i] &&& 24) & 0xffL);
* Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的,
* 只合成低32bit,高32bit清零,以适应原始C实现的用途
private void Decode(long[] output, byte[] input, int len) {
for (i = 0, j = 0; j & i++, j += 4) {
output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) && 8)
| (b2iu(input[j + 2]) && 16) | (b2iu(input[j + 3]) && 24);
* b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算
public static long b2iu(byte b) {
return b & 0 ? b & 0x7F + 128 :
* byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示,
*  因为java中的byte的toString无法实现这一点,我们又没有C语言中的 sprintf(outbuf,"%02X",ib)
public static String byteHEX(byte ib) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
'b', 'c', 'd', 'e', 'f' };
char[] ob = new char[2];
ob[0] = Digit[(ib &&& 4) & 0X0F];
ob[1] = Digit[ib & 0X0F];
String s = new String(ob);
public static String md5(String inbuf) {
Encryptor m = new Encryptor();
return m.getMD5ofStr(inbuf);
导入该类后,直接调用:
Encryptor.md5("");
就可以了。
我有现成的java MD5密码加密算法,留个邮箱我发给你吧。
jdk里有
MessageDigest md = MessageDigest.getInstance("MD5");
参考jdk文档,看看能不能符合你的要求
已解决问题
未解决问题

我要回帖

更多关于 塔罗测他舍得离开我吗 的文章

 

随机推荐