2004. július 26., hétfő

How to save a password to the registry


Problem/Question/Abstract:

How to save a password to the registry

Answer:

When saving the password for your application in the registry, make sure you implement some level of encryption on the password though. There are a number of algorithms out there. Below is a really easy way that I used:

This function is resposible for rotating the ASCII value of each character in the string. It takes in an integer that represents the direction to rotate the character's value.


function ROT(s: string; direction: integer): string;
var
  i: integer;
begin
  if length(s) < 1 then {if the string is empty then exit}
    exit;
  for i := 1 to length(s) do {iterate the number of characters in string}
    Inc(s[i], direction); {change the value of char by the value of direction}
  result := s; {result becomes altered string}
end;


This routine will read values from keys of the registry:

procedure ReadRegistryValues(Tci: TConnectInfo);
var
  Reg: TRegIniFile;
begin
  Reg := nil;
  {init the variable Reg}
  try
    {attempt to do the following}
    Reg := TRegIniFile.Create;
    {create blank instance}
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    {set Root Key}
    if Reg.OpenKey('SOFTWARE\Futura International, Inc.\FTPUpdater', false) then
      {If the key opens successfully}
    begin
      {fill class with values from the registry}
      Tci.TC_url := ROT(Reg.ReadString(SECTION, 'Name of Key', 'string to be stored'),
        -1);
      Tci.TC_username := ROT(Reg.ReadString(SECTION, 'UserName',
        'string to be stored'), -1);
      Tci.TC_password := ROT(Reg.ReadString(SECTION, 'Password',
        'string to be stored'), -1);
      Tci.TC_username := ROT(Reg.ReadString(SECTION, 'UserName',
        'string to be stored'), -1);
      Tci.TC_passive := Reg.ReadBool(SECTION, 'string to be stored', false);
      Tci.TC_socks_add := Reg.ReadString(SECTION, 'string to be stored', '');
      Tci.TC_socks_password := ROT(Reg.ReadString(SECTION, 'string to be stored', ''),
        -1);
      Tci.TC_socks_port := Reg.ReadString(SECTION, 'string to be stored', '');
      Tci.TC_socks_usercode := Reg.ReadString(SECTION, 'string to be stored', '');
      Tci.TC_socks_version := Reg.ReadInteger(SECTION, 'string to be stored', 0);
    end;
  finally
    {when done with the above}
    Reg.Free;
    {free memory of instance of class}
  end;
end;


This routine will save values to the registry:

procedure SaveToRegistry(Tci: TConnectInfo);
var
  Reg: TRegIniFile;
begin
  Reg := nil;
  {initializing the variable}
  try
    {attempt to do the following}
    Reg := TRegIniFile.Create;
    {create an instance of TRegIniFile}
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    {setting the root key}
    {The call below and in the routine above has a third param. This parameter is used
    to create the key if necessary. I set the value to true here just in case the key did not
    exist, this should only be in the first use of the program.}
    if Reg.OpenKey('SOFTWARE\Futura International, Inc.\FTPUpdater', true) then
      {this rtn will create the key}
    begin
      Reg.WriteString(SECTION, 'URL', ROT(Tci.TC_url, 1));
      {same as above routine except we are writing values instead reading them}
      Reg.WriteString(SECTION, 'UserName', ROT(Tci.TC_username, 1));
      Reg.WriteString(SECTION, 'Password', ROT(Tci.TC_password, 1));
      Reg.WriteBool(SECTION, 'Passive Connect', Tci.TC_passive);
      Reg.WriteString(SECTION, 'Socks Address', Tci.TC_socks_add);
      Reg.WriteString(SECTION, 'Socks Password', ROT(Tci.TC_socks_password, 1));
      Reg.WriteString(SECTION, 'Socks Port', Tci.TC_socks_port);
      Reg.WriteString(SECTION, 'Socks UserCode', Tci.TC_socks_usercode);
      Reg.WriteInteger(SECTION, 'Socks Version', Tci.TC_socks_version);
    end;
  finally
    Reg.Free;
    {free memory alloced to Reg}
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése