Solved

Saving as a binary file

Posted on 2001-07-15
6
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month7 days, 5 hours left to enroll

623 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