Blog
All Blog Posts | Next Post | Previous Post
TEncryptedIniFile: easy to use class for handling app settings with encryption
Thursday, November 10, 2016
What's wrong with the KIS principle to store application settings in an INI file? The risk that someone is tampering with the INI file is an often heard reason for not using them. Not being able to store private or secret information another one.Both issues are solved with the introduced TEncryptedIniFile class. It descends from TMemIniFile and is as such a drop-in replacement and will deal only in app memory with decrypted data. In the file itself, the data is at all times encrypted. To build TEncryptedIniFile, we internally use AES 256bit encryption offered by the TAESEncryption class in TMS Cryptography Pack.
The code to use TEncryptedIniFile becomes something like:
const aeskey = 'anijd54dee1c3e87e1de1d6e4d4e1de3'; var mi: TEncryptedIniFile; begin try mi := TEncryptedIniFile.Create('.settings.cfg', aeskey); try FTPUserNameEdit.Text := mi.ReadString('FTP','USER',''); FTPPasswordNameEdit.Text := mi.ReadString('FTP','PWD',''); FTPPortSpin.Value := mi.ReadInteger('FTP','PORT',21); mi.WriteDateTime('SETTINGS','LASTUSE',Now); mi.UpdateFile; finally mi.Free; end; except ShowMessage('Error in encrypted file. Someone tampered with the file?'); end; end;
To start using this TEncryptedIniFile you can get the latest TMS Cryptography Pack that has this class now included.
Bruno Fierens

This blog post has received 6 comments.


Bruno Fierens

But i receive same error on same line:
type
TEncryptedIniFile = class(TMemInifile)
private
FFileName: string;
FEncoding: TEncoding;
FKey: string;
FOnDecryptError: TNotifyEvent;
procedure LoadValues;
public
constructor Create(const FileName: string; const Key: string); overload;
constructor Create(const FileName: string; const Encoding: TEncoding; CaseSensitive: Boolean); overload; override;<-- [dcc32 Error] inifiles_gen.pas(37): E2170 Cannot override a non-virtual method -->
procedure UpdateFile; override;
published
property OnDecryptError: TNotifyEvent read FOnDecryptError write FOnDecryptError;
end;
Gabriel Cristea

I dont have the last pack installed
After fix that all work correct
Thank you
Cristea Gabriel

Can''t find unit Inifiles
Willie Holtzhauzen


Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;System;Xml;Data;Datasnap;Web;Soap;Vcl;Vcl.Imaging;Vcl.Touch;Vcl.Samples;Vcl.Shell
Bruno Fierens
All Blog Posts | Next Post | Previous Post
I have succesfully use your example in my project
But the problem appear starting with XE10.3 Rio, it get an error:
"[dcc32 Error] TMSEncryptedIniFile.pas(35): E2170 Cannot override a non-virtual method"
type
TEncryptedIniFile = class(TMemInifile)
private
FFileName: string;
FKey: string;
procedure LoadValues;
public
constructor Create(const FileName: string; const Key: string); overload;
constructor Create(const FileName: string; const Encoding: TEncoding; CaseSensitive: Boolean); overload; override; // <--This is the line with problem
procedure UpdateFile; override;
end;
I cannot manage to fix that.
Maybe you can help me, please ?
Thank you,
Gabriel Cristea
Gabriel Cristea