Mega Code Archive

 
Categories / Delphi / Strings
 

How to encode and decode a String with XOR

Title: How to encode and decode a String with XOR? Question: How to encode and decode a String using the XOR function? Easy! Read the article... ;-] Answer: Here is an example on how to complete this task: function XorStr(Stri, Strk: String): String; var Longkey: string; I: Integer; Next: char; begin for I := 0 to (Length(Stri) div Length(Strk)) do Longkey := Longkey + Strk; for I := 1 to length(Stri) do begin Next := chr((ord(Stri[i]) xor ord(Longkey[i]))); Result := Result + Next; end; end; procedure TForm1.Button1Click(Sender: TObject); begin { Encode The String } Edit1.Text := XorStr('The String', '1234567890'); end; procedure TForm1.Button2Click(Sender: TObject); begin { Decode The String } Edit2.Text := XorStr(Edit1.Text, '1234567890'); end; That's it! Now it will be dificult to a person to decode a string like that without knowing the "Strk" key value that you used. Try it, becauseit really can become handy some day.