Author: William Gerbert
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.
1 function ROT(s: string; direction: integer): string;
2 var
3 i: integer;
4 begin
5 if length(s) < 1 then {if the string is empty then exit}
6 exit;
7 for i := 1 to length(s) do {iterate the number of characters in string}
8 Inc(s[i], direction); {change the value of char by the value of direction}
9 result := s; {result becomes altered string}
10 end;
11
12
13 //This routine will read values from keys of the registry:
14
15 procedure ReadRegistryValues(Tci: TConnectInfo);
16 var
17 Reg: TRegIniFile;
18 begin
19 Reg := nil;
20 {init the variable Reg}
21 try
22 {attempt to do the following}
23 Reg := TRegIniFile.Create;
24 {create blank instance}
25 Reg.RootKey := HKEY_LOCAL_MACHINE;
26 {set Root Key}
27 if Reg.OpenKey('SOFTWARE\Futura International, Inc.\FTPUpdater', false) then
28 {If the key opens successfully}
29 begin
30 {fill class with values from the registry}
31 Tci.TC_url := ROT(Reg.ReadString(SECTION, 'Name of Key', 'string to be
32 stored'
33 -1);
34 Tci.TC_username := ROT(Reg.ReadString(SECTION, 'UserName',
35 'string to be stored'), -1);
36 Tci.TC_password := ROT(Reg.ReadString(SECTION, 'Password',
37 'string to be stored'), -1);
38 Tci.TC_username := ROT(Reg.ReadString(SECTION, 'UserName',
39 'string to be stored'), -1);
40 Tci.TC_passive := Reg.ReadBool(SECTION, 'string to be stored', false);
41 Tci.TC_socks_add := Reg.ReadString(SECTION, 'string to be stored', '');
42 Tci.TC_socks_password := ROT(Reg.ReadString(SECTION, 'string to be stored',
43 ''),
44 -1);
45 Tci.TC_socks_port := Reg.ReadString(SECTION, 'string to be stored', '');
46 Tci.TC_socks_usercode := Reg.ReadString(SECTION, 'string to be stored', '');
47 Tci.TC_socks_version := Reg.ReadInteger(SECTION, 'string to be stored', 0);
48 end;
49 finally
50 {when done with the above}
51 Reg.Free;
52 {free memory of instance of class}
53 end;
54 end;
55
56
57 //This routine will save values to the registry:
58
59 procedure SaveToRegistry(Tci: TConnectInfo);
60 var
61 Reg: TRegIniFile;
62 begin
63 Reg := nil;
64 {initializing the variable}
65 try
66 {attempt to do the following}
67 Reg := TRegIniFile.Create;
68 {create an instance of TRegIniFile}
69 Reg.RootKey := HKEY_LOCAL_MACHINE;
70 {setting the root key}
71 {The call below and in the routine above has a third param. This parameter is
72 used
73 to create the key if necessary. I set the value to true here just in case the
74 key did not
75 exist, this should only be in the first use of the program.}
76 if Reg.OpenKey('SOFTWARE\Futura International, Inc.\FTPUpdater', true) then
77 {this rtn will create the key}
78 begin
79 Reg.WriteString(SECTION, 'URL', ROT(Tci.TC_url, 1));
80 {same as above routine except we are writing values instead reading them}
81 Reg.WriteString(SECTION, 'UserName', ROT(Tci.TC_username, 1));
82 Reg.WriteString(SECTION, 'Password', ROT(Tci.TC_password, 1));
83 Reg.WriteBool(SECTION, 'Passive Connect', Tci.TC_passive);
84 Reg.WriteString(SECTION, 'Socks Address', Tci.TC_socks_add);
85 Reg.WriteString(SECTION, 'Socks Password', ROT(Tci.TC_socks_password, 1));
86 Reg.WriteString(SECTION, 'Socks Port', Tci.TC_socks_port);
87 Reg.WriteString(SECTION, 'Socks UserCode', Tci.TC_socks_usercode);
88 Reg.WriteInteger(SECTION, 'Socks Version', Tci.TC_socks_version);
89 end;
90 finally
91 Reg.Free;
92 {free memory alloced to Reg}
93 end;
94 end;
|