我设法使用以下代码对输入的密码进行简单加密,然后在标签标题中显示加密密码, procedure TfrmLogin.edtAddPasswordClick(Sender: TObject);var NormalPassword, EncryptedPassword: string; PasswordChar: Char; Encrypte
procedure TfrmLogin.edtAddPasswordClick(Sender: TObject); var NormalPassword, EncryptedPassword: string; PasswordChar: Char; EncryptedCharValue: string; CharPtr: Integer; Ptr, n: Integer; begin NormalPassword := Edit1.text; EncryptedPassword := ''; for CharPtr := 1 to Length(NormalPassword) do begin PasswordChar := NormalPassword[CharPtr]; EncryptedCharValue := IntToStr (Ord(PasswordChar) * 5 + 14); EncryptedPassword := EncryptedPassword + EncryptedCharValue; Label1.Caption := EncryptedPassword; end; end;
问题是我想将label1.caption中显示的加密密码转换回其原始形式,点击另一个按钮,我无法弄清楚如何做到这一点.有什么建议?
不要创建自己的算法来散列(或加密)密码,而是尝试使用经过良好测试的可靠算法,如SHA1,MD5等.回到你的问题,将加密值转换为原始值,你只需要反转你的算法,试试这个样本.
var NormalPassword, EncryptedPassword: String; PasswordChar : char; EncryptedCharValue : String; CharPtr : Integer; begin NormalPassword :=''; EncryptedPassword := Label1.Caption; //here is stored the encrypted password CharPtr := 1; while CharPtr< length(EncryptedPassword) do Begin EncryptedCharValue:=Copy(EncryptedPassword, CharPtr, 3); Inc(CharPtr, 3); PasswordChar := Chr((StrToint(EncryptedCharValue)-14) div 5); NormalPassword :=NormalPassword+ PasswordChar; end; Label2.Caption := NormalPassword; end;