Solved

Saving as a binary file

Posted on 2001-07-15
6
218 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
Comment Utility
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
Comment Utility
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
Comment Utility
gemarti > note that WritePrivateProfileStruct() is provided only for backword compatybility with 16-bit Windows
ziolko.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 21

Expert Comment

by:gemarti
Comment Utility
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
Comment Utility
listening...
0
 
LVL 3

Accepted Solution

by:
nnbbb09 earned 150 total points
Comment Utility

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

10 Experts available now in Live!

Get 1:1 Help Now