C++ builder md5加密问题
AnsiString __fastcall Strip(AnsiString S)
{
return S.Delete(1, 2);
}
AnsiString md5ToString(AnsiString S)
{
AnsiString AHex;
for (int i=1; i <=S.Length(); i++)
{
AHex += IntToHex((unsigned char)S[i], 2);// + " ";
}
return AHex;
}
String Md5Str(String src)
{
TIdCoderMD5 *md5=new TIdCoderMD5(NULL);
md5-> Reset();
md5-> AutoCompleteInput = true;
String tmp=md5ToString(Strip(md5-> CodeString(src)));
//String tmp=md5-> CodeString(src);
delete md5;
md5=NULL;
return tmp;
}
//---------------------------------------
//---------------------------------------
__fastcall TUserLoginForm::TUserLoginForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TUserLoginForm::BitBtn1Click(TObject *Sender)
{ String source,digest;
int sys;
source=Edit2-> Text.Trim();
if (Edit1-> Text.Trim().Length()==0 ||Edit2-> Text.Length()==0 )
{ ShowMessage( "请输入用户名称或密码!!! ");
Edit1-> SetFocus();
return;
}
//验证用户名称与密码
char pBuf[1024];
memset(pBuf,0,1024);
strcpy(pBuf,source.c_str());
int iTotal=strlen(pBuf);
//MD5_CTX *Mymd5;
//Mymd5= new MD5_CTX();
//unsigned char digest[16];
//Mymd5-> MD5Update(pBuf,iTotal);
// Mymd5-> MD5Final(digest);
//BCD_to_AnsiString(digest,16);
//Mymd5.MessageDigest(pBuf,iTotal,digest);
digest=Md5Str(source);
//String sHedx;
//sHedx=IntToHex((__int64)digest,8);
//sHedx=IntToHex(digest,8);
qry1-> Close();
qry1-> SQL-> Add( "select * from [user] ");
qry1-> SQL-> Add( "where loginname=:loginname and agree=1 and lock=0 ");
qry1-> Parameters-> ParamByName( "loginname ")-> Value=Edit1-> Text.Trim();
qry1-> Open();
if(qry1-> RecordCount!=1||qry1-> FieldByName( "password ")-> AsString!=sHedx )
{if (qry1-> RecordCount!=1 )
{ ShowMessage( "用户登陆名称不存在? ");
Edit1-> SetFocus();
return;
}
if (qry1-> FieldByName( "password ")-> AsString!=sHedx)
{ ShowMessage( "用户密码不对! ");
Edit2-> SetFocus();
return;
}
}
...
为什么这个算出来与.net算出来的不一致呢?
谢谢!
[解决办法]
就如同一把锁也有不同的钥匙
[解决办法]
同是MD5算法,如果程序代码正确的话,计算结果应该是一致的
[解决办法]
这个写法看不出有什么错误,不知道是不是.net的写法有问题。
[解决办法]
AnsiString md5ToString(AnsiString S)
{
AnsiString AHex;
for (int i=1; i <=S.Length(); i++)
{
AHex += IntToHex((unsigned char)S[i], 2);// + " ";
}
return AHex;
}
//i好像应该从0开始
顺便给你两个在线查md5的网址,你对照一下。
http://www.mmkey.com/md5/
http://www.cmd5.com/