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.
Cipher.Burn – Once we have finished encrypting we use the Burn method to erase all keying information.
Cipher.Free – Finally, as all good programmers should know, we must destroy our objects otherwise they consume memory.
So thats it, we’ve encrypted a string. How do we get the original unencrypted string back? Good question, lets take a look at another sample method :
function TForm1.DecryptThis(anEncryptedString : string) : string; var Cipher: TDCP_cast256; KeyStr: string; begin KeyStr:= 'AcceleratedKeyString'; Cipher:= TDCP_cast256.Create(Self); Cipher.InitStr(KeyStr,TDCP_sha1); result := Cipher.DecryptString(aString); Cipher.Burn; Cipher.Free; end;
You will notice that it looks almost exactly the same as the encrypt method. Well, you’re right, that’s the beauty of the encryption library it makes life so simple. The only difference is that we must call the DecryptString method instead. The returned result of the DecryptString function will be our original unencrypted string again. Notice though that our method isn’t very good, we have stored the key phrase as another variable. It would be quite easy to make a mistype and end up in all sorts of trouble. The best location to store your key phrase is either in the Windows registry or as an interface variable inside your application. Thus all classes will access the same variable for encrypting and decrypting every time.
So now you know how to encrypt and decrypt strings. Lets take this encryption further and explore the methods for file encryption. Imagine the possibilities for you Delphi applications if you can master encrypting configuration files, text files or sensitive reports. Lets take a look at another file encryption sample method.
procedure TForm1.EncryptFiles(aFileIn : string; aFileOut : string)
var
Cipher: TDCP_rc4;
KeyStr: string;
SourceStream, DestStream: TFileStream;
begin
KeyStr := 'AcceleratedKeyString';
try
SourceStream:= TFileStream.Create(aFileIn,fmOpenRead);
DestStream:= TFileStream.Create(aFileOut,fmCreate);
Cipher:= TDCP_rc4.Create(Self);
Cipher.InitStr(KeyStr,TDCP_sha1);
Cipher.EncryptStream(SourceStream, DestStream, SourceStream.Size);
Cipher.Burn;
Cipher.Free;
DestStream.Free;
SourceStream.Free;
MessageDlg('File encryption complete',mtInformation,[mbOK],0);
except
MessageDlg('Sorry, there was a File IO error',mtError,[mbOK],0);
end;
end;
As you can see from the example, to encrypt files you need to work with the stream of both files. However the encryption process is still so simple with the help of the library classes.
Why not try the library for yourself..
This library is covered by the Open Source license. See the readme.txt file for details
Download DCPcrypt Cryptographic Component library HERE