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

packagecom.lxitedu.tools.generate;

importjava.security.MessageDigest;

importjava.security.NoSuchAlgorithmException;

importcom.dcivision.framework.Crypt;

importcom.dcivision.framework.SystemParameterConstant;

importcom.dcivision.framework.SystemParameterFactory;

/**

*suitthephp

*

*@authorAdministrator

*

*/

publicclassMD5{

//解密类:Crypt

privatefinalstaticString[]hexDigits={"0","1","2","3","4","5","6","7","8","9","a","b","c","d",

"e","f"};

publicstaticvoidmain(String[]args)throwsException{

getMD5EncryptedString("");

StringencrptedPwd=Crypt.encrypt("diaoer",

SystemParameterFactory.getSystemParameter(SystemParameterConstant.CRYPTO_SALT));

System.out.println(getMD5EncryptedString("diaoer"));

System.out.println(encrptedPwd);

}

publicstaticStringgetMD5EncryptedString(StringsourceString)throwsNoSuchAlgorithmException{

//加密后的字符串

//创建具有指定算法名称的信息摘要

MessageDigestmd=MessageDigest.getInstance("MD5");

//使用指定的字节数组对摘要进行最后更新,然后完成摘要计算

byte[]results=md.digest(sourceString.getBytes());

returnbyteArrayToHexString(results);

}

privatestaticStringbyteArrayToHexString(byte[]b){

StringBufferresultSb=newStringBuffer();

for(inti=0;i<b.length;i++){

resultSb.append(byteToHexString(b[i]));

}

returnresultSb.toString();

}

/**

*将一个字节转化成十六进制形式的字符串

*/

privatestaticStringbyteToHexString(byteb){

intn=b;

if(n<0)

n=256+n;

intd1=n/16;

intd2=n%16;

returnhexDigits[d1]+hexDigits[d2];

}

}

解密:

/*

*@(#)Crypt.java

*

*Copyright(c)2003DCIVisionLtd

*Allrightsreserved.

*

*ThissoftwareistheconfidentialandproprietaryinformationofDCIVision

*Ltd("ConfidentialInformation").YoushallnotdisclosesuchConfidential

*Informationandshalluseitonlyinaccordancewiththetermsofthelicense

*agreementyouenteredintowithDCIVisionLtd.

*/

packagecom.dcivision.framework;

/**

*Crypt.java

*

*Thisclassistoprovideencryptanddecryptfunctionbyapasswordprovided.

*

*@authorRolloChan

*@companyDCIVisionLimited

*@creationdate25/06/2003

*@version$Revision:1.6$

*/

publicclassCrypt{

publicstaticfinalStringREVISION="$Revision:1.6$";

privateCrypt(){

}

/**

*encrypt

*

*Encryptastringwithapassword

*

*@parambuffer

*Thebuffertobeencrypted

*@parampassword

*Thepassword

*/

publicstaticStringencrypt(Stringbuffer,Stringpassword){

StringBuffersbSpace=newStringBuffer("");

buffer=sbSpace.toString()+buffer+sbSpace.toString();

char[]temp=newchar[buffer.length()];

inta=0;

for(inti=0;i<buffer.length();i++){

intb=password.charAt(a);

a++;

if(a>=password.length()){

a=0;

}

intc=(int)buffer.charAt(i);

temp[i]=(char)(c^b);

}

returntoHex(temp);

}

/**

*decrypt

*

*Decryptastringwithapassword

*

*@parambuffer

*Thebuffertobedecrypted

*@parampassword

*Thepassword

*/

publicstaticStringdecrypt(Stringbuffer,Stringpassword){

char[]temp=toChar(buffer);

Stringresult="";

inta=0;

for(inti=0;i<temp.length;i++){

intb=password.charAt(a);

a++;

if(a>=password.length()){

a=0;

}

intc=temp[i];

result+=(char)(c^b);//^按位异或XOR就是二个操作数只有一个是1的话结果就是1否则就是0。

}

returnresult.trim();

}

privatestaticinthexToInt(charc){

if(c>='0'&&c<='9'){

returnc-'0';

}

return(c-'a')+10;

}

privatestaticcharhexToChar(Stringbuffer){

intl=hexToInt(buffer.charAt(1));

inth=hexToInt(buffer.charAt(0));

return(char)(h*16+l);

}

privatestaticStringtoHex(char[]buffer){

Stringtemp="";

Stringresult="";

for(inti=0;i<buffer.length;i++){

intc=buffer[i];

temp=Integer.toHexString((int)c);

if(temp.length()==1){

temp="0"+temp;

}

result+=temp;

}

returnresult;

}

privatestaticchar[]toChar(Stringbuffer){

char[]result=newchar[buffer.length()/2];

for(inti=0;i<buffer.length();i+=2){

if(i+2<=buffer.length()){

Stringtemp=buffer.substring(i,i+2);

result[i/2]=(char)hexToChar(temp);

}

}

returnresult;

}

/**

*main

*

*@paramarg

*/

publicstaticvoidmain(Stringarg[]){

//Stringbuffer=arg[0];

//Stringpassword=arg[1];

Stringbuffer="diaoer";

Stringpassword="test";

Stringtemp=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)+"\"");

}

}

相关推荐