Solved

Saving as a binary file

Posted on 2001-07-15
6
222 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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