|
|
|
Delphi - Encryption, ciphers and hashes (2)
Now we have installed the library lets get to work and start encrypting some strings. Firstly, before we can use any of the encryption methods we must set a reference in our Delphi class to the encryption classes we want to use:
Now we have a reference to the classes lets take a look at a function I made earlier:
function TForm1.EncryptThis(aString : string) : string;
var
Cipher: TDCP_cast256;
KeyStr: string;
begin
KeyStr:= 'AcceleratedKeyString';
Cipher:= TDCP_cast256.Create(Self);
Cipher.InitStr(KeyStr,TDCP_sha1);
result := Cipher.EncryptString(aString);
Cipher.Burn;
Cipher.Free;
end;
|
That probably seems a bit daunting but its incredibly simple. The idea of the function is to provide a compact single call to encrypt any string we want. The string we want to encrypt is passed as the only parameter to our EncryptThis function. The result returned will be the encrypted string, simple!The real simplicity to this function actually comes from the hard work done by the encryption classes TDCP_cast256 and TDCP_sha1 which work seamlessly together to encrypt my string for me. To get a better understanding of how the encryption works lets take a look at some of the method calls used from the encryption classes:
TDCP_cast256.Create(Self) - The constructor method of the TDCP_cast256 class returns an object of type TDCP_cast256. Using this object we can call the EncryptString method.
Cipher.InitStr(KeyStr,TDCP_sha1) - Before we can actually use the EncryptString method we must first initialize the cipher with our key phrase. This provides the basis for encrypting and decrypting strings. Its important to remember that we need to use the same key phrase that a string was encrypted with to decypt it again. Thus it is essential that this phrase is kept secret, more on this later. Notice how we also used TDCP_sha1 as a parameter. This initializes the cipher with a hash of the key string using the specified hash type.
|
|
Featured |
Other popular Delphi resources. Why not try them for yourself?
|
|
Delphi Knowledge Base. Best practices, walkthroughs, tutorials, component advice and more. Solutions and professional designs for Delphi 4, Delphi 5, Delphi 6 and Delphi, 7. Word COM, Excel COM, Outlook COM including code examples for Word OLE, Excel OLE and Outlook OLE. Automating Outlook to create MailItem and send attachments in Delphi. ActiveX, Com. ADO Data Architecture, TMS component pack walk through and code examples. Using Developer Express and example walkthroughs. Date & time conversion routines, video playback and frame manipulation. COM articles, papers, tips, techniques and hints. How to create Word reports and excel spreadsheets using Delphi code and OLE automation. Understanding Delphi encryption and using ciphers, with source code examples of Blowfish, Twofish, and Rijndael. Securing application data and confidential information using open source encryption components. Free encryption dll component. Accessing and managing MS Excel sheets with Delphi. How to retrieve, display and edit Microsoft Excel spreadsheets with Delphi automation. Accessing and managing MS Word documents with Delphi. How to retrieve, display and edit Microsoft Word documents using Delphi automation. Read and write Excel spreadsheets using Delphi code. How to read and write registry entries using Delphi code. Source code examples using Delphi 5, Delphi 6 and Delphi 7. Working with VCL Developer Express components. How to apply grid styles and themes to Delphi applications. Using image columns and shading to improve the grid and treeview appearances. Including examples from ExpressQuantumPack, for Delphi, ExpressPack for Delphi. Developer guides for Developer Express components. All our articles are written by our senior Delphi professionals to help you get the best from your legacy Delphi applications |
|