Solved

Saving as a binary file

Posted on 2001-07-15
6
219 Views
Last Modified: 2010-04-06
Hello All,
I need to save a INI file that cannot be read by
anyone when installed on another computer (Saved Password). Can someone send me an example of this.
Do I need to save it as a binary file instead of
a text file..if so how is this done.
Regards,
Frank
0
Comment
Question by:frw
6 Comments
 
LVL 6

Expert Comment

by:MannSoft
ID: 6284395
You would have to encrypt the password string before storing it.  I think on www.torry.ru there is a TCryptIni compoenent but if I remember right it costs money.  I just used a simple routine that used XOR (ripped it from SWAG) to encrypt when i needed to store a password.  Definately not secure, but its not plain text and (for my uses) was good enough.
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6284400
See if you can make any progress with this function:

WritePrivateProfileStruct(
  lpAppname:PChar;  {Pointer to section name of INI file}
  lpKeyName:PChar;  {Pointer to the Key name in the INI    
                     File Section}
  lpStruct:Pointer; {size of lpStruct Buffer}
  nSize: UINT;      {pointer to initialization file name}
  lpFileName:PChar; {pointer to initialization file name}
): BOOL;

This function will add a block of data to lpKeyName in a section of a private user-defined initialization file.



0
 
LVL 21

Expert Comment

by:ziolko
ID: 6284699
gemarti > note that WritePrivateProfileStruct() is provided only for backword compatybility with 16-bit Windows
ziolko.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 21

Expert Comment

by:gemarti
ID: 6285434
Yes, you are right ziolko and the question was how to store the information in an ini file, thus the answer is WritePrivateProfileStruc().
If the question wasn't so narrow there are obviously other options, such as using the registry.

0
 
LVL 3

Expert Comment

by:vladh
ID: 6287029
listening...
0
 
LVL 3

Accepted Solution

by:
nnbbb09 earned 150 total points
ID: 6287644

This is some code that I found on the web. There are functions to encrypt/decrypt a string and also to encrypt/decrypt a file. All you need to do is a key (of type word) when encrypting/decrypting. It's also a good idea to change the values of C1 and C2 as they are part of the encryption key. As MannSoft has said, it's not particularly secure (that's the nature of ini files) but at least they won't be able to read the file contents.

unit EncryptIt;

interface
uses
  Classes;

const
  C1 = 52845;
  C2 = 22719;

function Encrypt(const S: String; Key: Word): String;
function Decrypt(const S: String; Key: Word): String;
procedure EncryptFile(INFName, OutFName : String; Key : Word);
procedure DecryptFile(INFName, OutFName : String; Key : Word);

implementation

function Encrypt(const S: String; Key: Word): String;
var
  I: Integer;
begin
  Result := S;
  for I := 1 to Length(S) do
  begin
    Result[I] := char(byte(S[I]) xor (Key shr 8));
    Key := (byte(Result[I]) + Key) * C1 + C2;
  end;
end;

function Decrypt(const S: String; Key: Word): String;
var
  I: Integer;
begin
  Result := S;
  for I := 1 to Length(S) do
  begin
    Result[I] := char(byte(S[I]) xor (Key shr 8));
    Key := (byte(S[I]) + Key) * C1 + C2;
  end;
end;

procedure EncryptFile(INFName, OutFName : String; Key : Word);
var
  MS, SS : TMemoryStream;
  X : Integer;
  C : Byte;
begin
  MS := TMemoryStream.Create;
  SS := TMemoryStream.Create;
  try
    MS.LoadFromFile(INFName);
    MS.Position := 0;
    for X := 0 to MS.Size - 1 DO
    begin
      MS.Read(C, 1);
      C := (C xor (Key shr 8));
      Key := (C + Key) * C1 + C2;
      SS.Write(C,1);
    end;
    SS.SaveToFile(OutFName);

  finally
    SS.Free;
    MS.Free;
  end;
end;

procedure DecryptFile(INFName, OutFName : String; Key : Word);
var
  MS, SS : TMemoryStream;
  X : Integer;
  C, O : Byte;
begin
  MS := TMemoryStream.Create;
  SS := TMemoryStream.Create;
  try
    MS.LoadFromFile(INFName);
    MS.Position := 0;
    for x := 0 TO MS.Size - 1 DO
      begin
      MS.Read(C, 1);
      O := C;
      C := (C xor (Key shr 8));
      Key := (O + Key) * C1 + C2;
      SS.Write(C,1);
    end;
    SS.SaveToFile(OutFName);
  FINALLY
    SS.Free;
    MS.Free;
  end;
end;

end.

Regards

Jo
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi 2010 Export to pdf 2 287
Memory leak using records in a TVirtualStringTree 11 111
add combobox item based on numbers 9 125
Magic Software info 18 125
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now