packageendecrypt;

importjava.io.UnsupportedEncodingException;
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;

/**
*采用MD5加密解密
*@authortfq
*@datetime2011-10-13
*/
publicclassMD5Util{

/***
*MD5加码生成32位md5码
*/
publicstaticStringstring2MD5(StringinStr){
MessageDigestmd5=null;
try{
md5=MessageDigest.getInstance("MD5");
}catch(Exceptione){
System.out.println(e.toString());
e.printStackTrace();
return"";
}
char[]charArray=inStr.toCharArray();
byte[]byteArray=newbyte[charArray.length];

for(inti=0;ibyteArray[i]=(byte)charArray[i];
byte[]md5Bytes=md5.digest(byteArray);
StringBufferhexValue=newStringBuffer();
for(inti=0;iintval=((int)md5Bytes[i])&0xff;
if(val<16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
returnhexValue.toString();

}

/**
*加密解密算法执行一次加密,两次解密
*/
publicstaticStringconvertMD5(StringinStr){

char[]a=inStr.toCharArray();
for(inti=0;ia[i]=(char)(a[i]^'t');
}
Strings=newString(a);
returns;

}

//测试主函数
publicstaticvoidmain(Stringargs[]){
Strings=newString("tangfuqiang");
System.out.println("原始:"+s);
System.out.println("MD5后:"+string2MD5(s));
System.out.println("加密的:"+convertMD5(s));
System.out.println("解密的:"+convertMD5(convertMD5(s)));

}
}