1.权限 密码放在普通表里足够了,SQL有自己的权限管理,只要你的程序没有权限漏洞,是比较安全的. 2.trim Delphi syntax:function Trim(const S: string): string; overload; function Trim(const S: WideString): WideString; overload;Trim removes leading and trailing spaces and control characters from the given string S.(别告诉我你看不懂英文!) :-)
数据库只是自动在右边加空格 用query组件的话 sql: select rtrim(field1),rtrim(field2) from table where ...
你用的是dbedit 吧,我用edit控件就没问题!
2、字段类型直接用varchar就好了啊。
伴水的一个加密函数: (*// 标题:字符串加密;pascal字符表示 说明:应用于文件加密 设计:Zswang 日期:2002-02-19 支持:[email protected] //*)///////Begin Source function StringToDisplay(mString: string): string; var I: Integer; S: string; begin Result := ''; S := ''; for I := 1 to Length(mString) do if mString[I] in [#32..#127] then S := S + mString[I] else begin if S <> '' then begin Result := Result + QuotedStr(S); S := ''; end; Result := Result + Format('#$%x', [Ord(mString[I])]); end; if S <> '' then Result := Result + QuotedStr(S); end; { StringToDisplay }function DisplayToString(mDisplay: string): string; var I: Integer; S: string; B: Boolean; begin Result := ''; B := False; mDisplay := mDisplay; for I := 1 to Length(mDisplay) do if B then case mDisplay[I] of '''': begin if S <> '' then Result := Result + StringReplace(S, '''''', '''', [rfReplaceAll]); if Copy(mDisplay, I + 1, 1) = '''' then Result := Result + ''''; S := ''; B := False; end; else S := S + mDisplay[I]; end else case mDisplay[I] of '#', '''': begin if S <> '' then Result := Result + Chr(StrToIntDef(S, 0)); S := ''; B := mDisplay[I] = ''''; end; '$', '0'..'9', 'a'..'f', 'A'..'F': S := S + mDisplay[I]; end; if (not B) and (S <> '') then Result := Result + Chr(StrToIntDef(S, 0)); end; { DisplayToString }function StringEncrypt(mStr: string; mKey: string): string; var I, J: Integer; begin J := 1; Result := ''; for I := 1 to Length(mStr) do begin Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J])); if J + 1 <= Length(mKey) then Inc(J) else J := 1; end; { 自己加步骤 } end; { StringEncrypt }function StringDecrypt(mStr: string; mKey: string): string; var I, J: Integer; begin J := 1; Result := ''; { 自己加步骤 } for I := 1 to Length(mStr) do begin Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J])); if J + 1 <= Length(mKey) then Inc(J) else J := 1; end; end; { StringDecrypt } ///////End Source///////Begin Demo const cKey = '给你这一把钥匙,只能打开这一把锁';procedure TForm1.Button1Click(Sender: TObject); begin Memo2.Text := StringToDisplay(StringEncrypt(Memo1.Text, cKey)); end;procedure TForm1.Button2Click(Sender: TObject); begin Memo1.Text := StringDecrypt(DisplayToString(Memo2.Text), cKey); end; ///////End Demo
以下建议,仅供参考: ======================================== 对于密码的保存,最好在不足长度尾部追加空格,但是不要直接保存密码,也不要保存加 密后的密码,因为加密的密码,只要知道你加密的密码和加密算法就可以破取你的密码表的所有用户密码。 最好使用不可逆的消息摘要发生函数,只保存hash的结果,那么这样,即使是程序设计者本身也 无法知道密码表里的真正的密码时什么,验证时,同样对用户登录时提供的密码执行hash, 将hash的结果直接和密码表的密码值(也是hash的结果)进行比较,如果一样就推断用户 的密码正确(当然,由于hash函数的缺陷,用户的密码不正确但通过验证的机会是有的, 但是对当前一些有名的digest函数,如md5,shl1等,这种机会很微,几乎可以认为不可能, 并且一般登录时还附带有用户信息,所以基本上不会发生碰撞。如果有人企图攻击一个长度 大于10个字母数字的超级用户的密码,他只有用穷举的办法,平均要1.8E+14 次才能确保成功, 如果加上其它字符,那么这个数值还要大,只要经常更换密码,应该是很安全的)。 不过,如果你使用MD5-128的话,你的密码字段的长度就要改了(至少16),并且要改成 Binary或者Image(blob),但不要char或varchar类型,因为用这些类型,SQLServer很可能会 截断你的Digest的。举例:const MinPasswordLength = 4; DefPasswordLength = 16;procedure PreprocessPassword(var aPassword: string); var vLen: Integer; begin vLen := Length(aPassword); if vLen < MinPasswordLength then raise Exception.Create('The password is too short.'); if vLen < MinPass then begin SetLength(aPassword, DefPasswordLength); //增加攻击者的字符串操作时间 FillChar(aPassword[vLen+1], DefPasswordLength-vLen, #32); end; end;procedure SaveAccountInfo(const aLoginName, aPassword: string); var vDigest: TDigest; //类型依赖于你使用的库 begin PreprocessPassword(aPassword); vDigest := MD5(aPassword); //具体函数 ActualSaveToTableProc(aLoginName, aDigest);//实际保存到密码表中 end;function CheckAccountInfo(const aLoginName, aPassword: string); var vDigest: TDigest; begin Result := LoginNameExistsInTable(aLoginName); if Result then begin vDigest := LoadDigestFromTable; PreprocessPassword(aPassword); Result := vDigest = MD5(aPassword); end; end;
if vLen < MinPass then => if vLen < DefPasswordLength then
2用trim就可以了
1。如何加密?
2.能不能说详细一点?
谢谢
密码放在普通表里足够了,SQL有自己的权限管理,只要你的程序没有权限漏洞,是比较安全的.
2.trim
Delphi syntax:function Trim(const S: string): string; overload;
function Trim(const S: WideString): WideString; overload;Trim removes leading and trailing spaces and control characters from the given string S.(别告诉我你看不懂英文!)
:-)
2.用Result:=Trim(s:string)。
字符串加密可参照下面帖子:
http://expert.csdn.net/Expert/topic/1193/1193606.xml?temp=.2378046
用query组件的话
sql:
select rtrim(field1),rtrim(field2) from table where ...
(*//
标题:字符串加密;pascal字符表示
说明:应用于文件加密
设计:Zswang
日期:2002-02-19
支持:[email protected]
//*)///////Begin Source
function StringToDisplay(mString: string): string;
var
I: Integer;
S: string;
begin
Result := '';
S := '';
for I := 1 to Length(mString) do
if mString[I] in [#32..#127] then
S := S + mString[I]
else begin
if S <> '' then begin
Result := Result + QuotedStr(S);
S := '';
end;
Result := Result + Format('#$%x', [Ord(mString[I])]);
end;
if S <> '' then Result := Result + QuotedStr(S);
end; { StringToDisplay }function DisplayToString(mDisplay: string): string;
var
I: Integer;
S: string;
B: Boolean;
begin
Result := '';
B := False;
mDisplay := mDisplay;
for I := 1 to Length(mDisplay) do
if B then case mDisplay[I] of
'''': begin
if S <> '' then Result := Result + StringReplace(S, '''''', '''', [rfReplaceAll]);
if Copy(mDisplay, I + 1, 1) = '''' then Result := Result + '''';
S := '';
B := False;
end;
else S := S + mDisplay[I];
end
else case mDisplay[I] of
'#', '''': begin
if S <> '' then Result := Result + Chr(StrToIntDef(S, 0));
S := '';
B := mDisplay[I] = '''';
end;
'$', '0'..'9', 'a'..'f', 'A'..'F': S := S + mDisplay[I];
end;
if (not B) and (S <> '') then Result := Result + Chr(StrToIntDef(S, 0));
end; { DisplayToString }function StringEncrypt(mStr: string; mKey: string): string;
var
I, J: Integer;
begin
J := 1;
Result := '';
for I := 1 to Length(mStr) do begin
Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J]));
if J + 1 <= Length(mKey) then
Inc(J)
else J := 1;
end;
{ 自己加步骤 }
end; { StringEncrypt }function StringDecrypt(mStr: string; mKey: string): string;
var
I, J: Integer;
begin
J := 1;
Result := '';
{ 自己加步骤 }
for I := 1 to Length(mStr) do begin
Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J]));
if J + 1 <= Length(mKey) then
Inc(J)
else J := 1;
end;
end; { StringDecrypt }
///////End Source///////Begin Demo
const
cKey = '给你这一把钥匙,只能打开这一把锁';procedure TForm1.Button1Click(Sender: TObject);
begin
Memo2.Text := StringToDisplay(StringEncrypt(Memo1.Text, cKey));
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := StringDecrypt(DisplayToString(Memo2.Text), cKey);
end;
///////End Demo
========================================
对于密码的保存,最好在不足长度尾部追加空格,但是不要直接保存密码,也不要保存加
密后的密码,因为加密的密码,只要知道你加密的密码和加密算法就可以破取你的密码表的所有用户密码。
最好使用不可逆的消息摘要发生函数,只保存hash的结果,那么这样,即使是程序设计者本身也
无法知道密码表里的真正的密码时什么,验证时,同样对用户登录时提供的密码执行hash,
将hash的结果直接和密码表的密码值(也是hash的结果)进行比较,如果一样就推断用户
的密码正确(当然,由于hash函数的缺陷,用户的密码不正确但通过验证的机会是有的,
但是对当前一些有名的digest函数,如md5,shl1等,这种机会很微,几乎可以认为不可能,
并且一般登录时还附带有用户信息,所以基本上不会发生碰撞。如果有人企图攻击一个长度
大于10个字母数字的超级用户的密码,他只有用穷举的办法,平均要1.8E+14 次才能确保成功,
如果加上其它字符,那么这个数值还要大,只要经常更换密码,应该是很安全的)。
不过,如果你使用MD5-128的话,你的密码字段的长度就要改了(至少16),并且要改成
Binary或者Image(blob),但不要char或varchar类型,因为用这些类型,SQLServer很可能会
截断你的Digest的。举例:const
MinPasswordLength = 4;
DefPasswordLength = 16;procedure PreprocessPassword(var aPassword: string);
var
vLen: Integer;
begin
vLen := Length(aPassword);
if vLen < MinPasswordLength then
raise Exception.Create('The password is too short.'); if vLen < MinPass then
begin
SetLength(aPassword, DefPasswordLength); //增加攻击者的字符串操作时间
FillChar(aPassword[vLen+1], DefPasswordLength-vLen, #32);
end;
end;procedure SaveAccountInfo(const aLoginName, aPassword: string);
var
vDigest: TDigest; //类型依赖于你使用的库
begin
PreprocessPassword(aPassword);
vDigest := MD5(aPassword); //具体函数
ActualSaveToTableProc(aLoginName, aDigest);//实际保存到密码表中
end;function CheckAccountInfo(const aLoginName, aPassword: string);
var
vDigest: TDigest;
begin
Result := LoginNameExistsInTable(aLoginName);
if Result then
begin
vDigest := LoadDigestFromTable;
PreprocessPassword(aPassword);
Result := vDigest = MD5(aPassword);
end;
end;
=> if vLen < DefPasswordLength then