Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Saving as a binary file

Posted on 2001-07-15
6
Medium Priority
?
230 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 450 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses

783 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