1. Technology

Simple File Encryption/Decryption in Delphi

Use The TFile Delphi Record To Encrypt or Decrypt A File

By

File encryption is a process that makes (your) files protected in a way that they cannot be viewed by others. The most common use of file encryption is to protect your private files, documents and information. Encrypted files are protected from "unauthorized" viewing until you decrypt them.

A member of the Delphi Programming Forum in a post titled "RTF File encryption" is asking for some help in encrypting RTF documents.

TFile.Encrypt, TFile.Decrypt

Starting with Delphi 2010, the RTL includes the "ioutils.pas" unit hosting dozens of file and folder related functions grouped into TFile, TDirectory, TPath and alike classes (records to be more precise).

Having file and directory manipulation functions and procedures is not new in Delphi, the IOUtils.pas has grouped (wrapped) file/directory related operations into a bunch of functions exposed by the TFile and TDirectory records.

Two function exposed by TFile are worth a look regarding file encryption and file decryption:

TFile.Encrypt(fileName), TFile.Decrypt(fileName)

The Encrypt and Decrypt function of the TFile record are implemented around the "Advapi32.dll" where Windows API functions "EncryptFile" and "DecryptFile" are defined.

The TFile.Encrypt wraps the Windows API "EncryptFile" which encrypts a file (or directory). EncryptFile ensures all data streams in a file are encrypted.

The TFile.Decrypt wraps the "DecryptFile" function which decrypts an encrypted file (or directory).

Yes, that's all to it :)

?!? Recovery policy configured for this system contains invalid recovery certificate ?!?

So you try to do "TFile.Encrypt('c:\this is MY file.rtf');" and you receive a weird "Recovery policy configured for this system contains invalid recovery certificate" error.

In short: the error tells you that the Encrypting File System (EFS) recovery policy that is implemented on (your) computer contains one or more EFS recovery agent certificates that have expired. These certificates cannot be used.

In long: How to fix the Recovery policy configured for this system contains invalid recovery certificate message?

Delphi tips navigator:
» Implementing StartsText With SubText as Pattern
« Occurrences Of A SubString in String

©2014 About.com. All rights reserved.