home *** CD-ROM | disk | FTP | other *** search
- {
- Xceed Encryption Library - Encryption Manager sample
- Copyright (c) 2001 Xceed Software Inc.
-
- [unManager.pas]
-
- This unit contains the main form's code. It demonstrates how to encrypt
- a file using different encryption methods and how to decrypt an encrypted
- file. It specifically uses:
- - The ProcessFile, SetSecretKeyFromPassPhrase and SetRandomInitVector methods.
- - The EncryptionMethod, PaddingMethod and HashingMethod properties.
-
- This file is part of the Xceed Encryption Library sample applications.
- The source code in this file is only intended as a supplement to the Xceed
- Encryption Library's documentation and is provided "as is" without warranty
- of any kind, either expressed or implied.
- }
-
- unit unManager;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, XceedEncryptionLib_TLB, Menus;
-
- //The different encyption methods
- type TEncryptionMethod = ( eemRijndael, //0
- eemTwoFish ); //1
-
- //This different hashing methods
- type THashingMethod = ( ehmSHA, //0
- ehmHaval ); //1
-
- type
- TfrmMain = class(TForm)
- Label1 : TLabel;
- Label2 : TLabel;
- Label3 : TLabel;
- Label4 : TLabel;
- edtPassPhrase: TEdit;
- edtSourceFile : TEdit;
- edtDestinationFile : TEdit;
- btBrowseForSource : TButton;
- btBrowseForDestination : TButton;
- btEncypt : TButton;
- btDecrypt : TButton;
- xSaveDialog: TSaveDialog;
- xOpenDialog: TOpenDialog;
- btOptions: TButton;
- txtMessages: TMemo;
- procedure FormCreate(Sender: TObject);
- procedure btBrowseForDestinationClick(Sender: TObject);
- procedure btBrowseForSourceClick(Sender: TObject);
- procedure btDecryptClick(Sender: TObject);
- procedure btEncyptClick(Sender: TObject);
- procedure btOptionsClick(Sender: TObject);
- procedure edtSourceFileExit(Sender: TObject);
- private
-
- //The values chosen by the user in the Option form
- m_eEncryptionMethod : TEncryptionMethod;
- m_eEncryptionMode : EXEEncryptionMode;
- m_ePaddingMethod : EXEPaddingMethod;
- m_eHashingMethod : THashingMethod;
- m_nKeySize : SmallInt;
-
- function CreateEncryptionMethod( var xEncryptor : TXceedEncryption ) : boolean;
- function DecryptFile( sSourceFilename : string; sDecryptedFilename : string ) : boolean;
- function EncryptFile( sSourceFilename : string; sEncryptedFilename : string ) : boolean;
- function RemoveFileExtension( sFilename : string ) : string;
- procedure SetDestinationFilename();
- public
- { Public declarations }
- end;
-
- var
- frmMain: TfrmMain;
-
- implementation
-
- uses registry, unOptions;
-
- {$R *.DFM}
-
-
- {***************************************************************}
- { }
- { FORM EVENTS }
- { }
- {***************************************************************}
-
- {---------------------------------------------------------------}
- procedure TfrmMain.FormCreate(Sender: TObject);
- var
- xReg : TRegistryIniFile;
- begin
- //default value to avoid errors if the options are not set beforehand
- xReg := TRegistryIniFile.Create( 'Software\Xceed\Delphi\Manager' );
- m_eEncryptionMethod := TEncryptionMethod( xReg.ReadInteger( 'Encryption', 'EncryptionMethod', Ord( eemRijndael ) ) );
- m_eEncryptionMode := xReg.ReadInteger( 'Encryption', 'EncryptionMode', emoFreeBlocks );
- m_ePaddingMethod := xReg.ReadInteger( 'Encryption', 'PaddingMethod', epmFIPS81 );
- m_eHashingMethod := THashingMethod( xReg.ReadInteger( 'Encryption', 'HashingMethod', Ord( ehmSHA ) ) );
- m_nKeySize := xReg.ReadInteger( 'Encryption', 'KeySize', 128 );
- xReg.Free();
- end;
-
- {---------------------------------------------------------------}
- { Select the destination folder and the file name that will be }
- { processed when encrypting or decrypting }
- {---------------------------------------------------------------}
- procedure TfrmMain.btBrowseForDestinationClick(Sender: TObject);
- begin
- xSaveDialog.Files.Clear();
- xSaveDialog.Title := 'Destination File';
- xSaveDialog.Filter := 'Encrypted files(*.aes;*.2fs)|*.aes;*.2fs|All files (*.*)|*.*';
- xSaveDialog.FilterIndex := 0;
-
- if( xSaveDialog.Execute ) then
- edtDestinationFile.Text := trim( xSaveDialog.Files.Text )
- end;
-
- {---------------------------------------------------------------}
- { Select the source folder and file name that will be }
- { processed when encrypting or decrypting }
- {---------------------------------------------------------------}
- procedure TfrmMain.btBrowseForSourceClick(Sender: TObject);
- begin
- xOpenDialog.Files.Clear();
- xOpenDialog.Title := 'Source File';
- xOpenDialog.Filter := 'Encrypted files(*.aes;*.2fs)|*.aes;*.2fs|All files (*.*)|*.*';
- xOpenDialog.FilterIndex := 1;
-
- if( xOpenDialog.Execute ) then
- begin
- edtSourceFile.Text := trim( xOpenDialog.Files.Text );
- SetDestinationFilename();
- end;
- end;
-
- {---------------------------------------------------------------}
- { Decrypt the selected source file to the specified destination }
- {---------------------------------------------------------------}
- procedure TfrmMain.btDecryptClick(Sender: TObject);
- begin
- if( DecryptFile( edtSourceFile.Text, edtDestinationFile.Text ) ) then
- begin
- //If the decryption is successful, empty the source and destination
- //edit boxes to simplify subsequent encryption/decryption.
- edtSourceFile.Text := '';
- edtDestinationFile.Text := '';
- end;
- end;
-
- {---------------------------------------------------------------}
- { Encrypt the selected source file to the specified destination }
- {---------------------------------------------------------------}
- procedure TfrmMain.btEncyptClick(Sender: TObject);
- begin
- if( EncryptFile( edtSourceFile.Text, edtDestinationFile.Text ) ) then
- begin
- //If the encryption is successful, emtpy the source and destination
- //edit boxes to simplify subsequent encryption/decryption.
- edtSourceFile.Text := '';
- edtDestinationFile.Text := '';
- end;
- end;
-
- {---------------------------------------------------------------}
- { Display the options form }
- {---------------------------------------------------------------}
- procedure TfrmMain.btOptionsClick(Sender: TObject);
- var
- xOptionsForm : TfrmOptions;
- xReg : TRegistryIniFile;
- begin
- xOptionsForm := TfrmOptions.Create( self );
-
- if xOptionsForm.ShowForm( m_eEncryptionMethod, m_eEncryptionMode,
- m_ePaddingMethod, m_eHashingMethod, m_nKeySize ) then
- begin
- xReg := TRegistryIniFile.Create( 'Software\Xceed\Delphi\Manager' );
- xReg.WriteInteger( 'Encryption', 'EncryptionMethod', Ord( m_eEncryptionMethod ) );
- xReg.WriteInteger( 'Encryption', 'EncryptionMode', Ord( m_eEncryptionMode ) );
- xReg.WriteInteger( 'Encryption', 'PaddingMethod', Ord( m_ePaddingMethod ) );
- xReg.WriteInteger( 'Encryption', 'HashingMethod', Ord( m_eHashingMethod ) );
- xReg.WriteInteger( 'Encryption', 'KeySize', Ord( m_nKeySize ) );
- xReg.Free();
- end;
-
- xOptionsForm.Destroy();
- end;
-
- {---------------------------------------------------------------}
- { Initialize the destination file to a default value if the }
- { destination text box is empty }
- {---------------------------------------------------------------}
- procedure TfrmMain.edtSourceFileExit(Sender: TObject);
- begin
- SetDestinationFilename();
- end;
-
- {***************************************************************}
- { }
- { FORM FUNCTIONS AND PROCEDURES }
- { }
- {***************************************************************}
-
- {---------------------------------------------------------------}
- { Create an new instance of an encryption method according to }
- { the specified encryption method chosen in the option form. }
- { Set some properties to the encryption method object }
- { appropriate for the selected encryption method and common to }
- { both encryption and decryption since this function will be }
- { called before doing both. }
- {---------------------------------------------------------------}
- function TfrmMain.CreateEncryptionMethod( var xEncryptor : TXceedEncryption ) : boolean;
- var
- xRijndael : DXceedRijndaelEncryptionMethod;
- xTwoFish : DXceedTwofishEncryptionMethod;
- xHaval : DXceedHavalHashingMethod;
- xSHA : DXceedSHAHashingMethod;
- begin
- //We instantiate a new encryption method, assigning it directly
- //to the EncryptionMethod property of the XceedEncryption object.
- try
- //Instantiate the right encryption method and set the hashing method
- //that will be used to set the key from the pass phrase.
-
- case m_eEncryptionMethod of
- eemRijndael : begin
- xRijndael := CoXceedRijndaelEncryptionMethod.Create();
-
- if( m_eHashingMethod = ehmHaval ) then
- begin
- //Haval supports hash sizes equivalent to the supported
- //key size. So, we can assign the latter to the former
- //without any problems.
- xHaval := CoXceedHavalHashingMethod.Create();
- xHaval.HashSize := m_nKeySize;
- xRijndael.HashingMethod := xHaval;
- end;
-
- if( m_eHashingMethod = ehmSHA ) then
- begin
- //We arbitarirly set the HashSize to the maximum key
- //size allowed so we don't have to worry that the
- //hash result of the pass phrase could be shorter
- //than the expected key (although the Xceed Encryption
- //Library would have dealt with it).
- xSHA := CoXceedSHAHashingMethod.Create();
- xSHA.HashSize := 256;
- xRijndael.HashingMethod := xSHA;
- end;
- //Set the secret key of the desired size using the user
- //pass phrase
- xRijndael.SetSecretKeyFromPassPhrase( edtPassPhrase.Text, m_nKeySize );
- //Set the encryption mode
- xRijndael.EncryptionMode := m_eEncryptionMode;
- //Set the padding method (for the last encrypted and decrypted block)
- xRijndael.PaddingMethod := m_ePaddingMethod;
-
- if( m_eEncryptionMode = emoChainedBlocks ) then
- //user wants to encrypt in CBC mode. We set the
- //initialization vector to a random value.
- xRijndael.SetRandomInitVector();
-
- xEncryptor.EncryptionMethod := xRijndael;
- end;
- eemTwoFish : begin
- xTwoFish := CoXceedTwofishEncryptionMethod.Create();
- if( m_eHashingMethod = ehmHaval ) then
- begin
- //Haval supports hash sizes equivalent to the supported
- //key size. So, we can assign the latter to the former
- //without any problems.
- xHaval := CoXceedHavalHashingMethod.Create();
- xHaval.HashSize := m_nKeySize;
- xTwoFish.HashingMethod := xHaval;
- end;
-
- if( m_eHashingMethod = ehmSHA ) then
- begin
- //We arbitarirly set the HashSize to the maximum key
- //size allowed so we don't have to worry that the
- //hash result of the pass phrase could be shorter
- //than the expected key (although the Xceed Encryption
- //Library would have dealt with it).
- xSHA := CoXceedSHAHashingMethod.Create();
- xSHA.HashSize := 256;
- xTwoFish.HashingMethod := xSHA;
- end;
- //Set the secret key of the desired size using the user
- //pass phrase
- xTwoFish.SetSecretKeyFromPassPhrase( edtPassPhrase.Text, m_nKeySize );
- //Set the encryption mode
- xTwoFish.EncryptionMode := m_eEncryptionMode;
- //Set the padding method (for the last encrypted and decrypted block)
- xTwoFish.PaddingMethod := m_ePaddingMethod;
-
- if( m_eEncryptionMode = emoChainedBlocks ) then
- //user wants to encrypt in CBC mode. We set the
- //initialization vector to a random value.
- xTwoFish.SetRandomInitVector();
-
- xEncryptor.EncryptionMethod := xTwoFish;
- end;
- end;
- except
- on xErr : Exception do
- begin
- txtMessages.Text := txtMessages.Text + 'Error initializing the encryption method' + #13#10 +
- xErr.Message + #13#10;
- end;
- end;
- CreateEncryptionMethod := true;
- end;
-
- {---------------------------------------------------------------}
- { Function that performs the actual decryption of a source file }
- { to a destination file. }
- {---------------------------------------------------------------}
- function TfrmMain.DecryptFile( sSourceFilename : string; sDecryptedFilename : string ) : boolean;
- var
- xEncryptor : TXceedEncryption;
- vaBytesRead : OleVariant;
- begin
- Self.Cursor := crHourGlass;
-
- //Create and instance of the XceedEncryption object
- xEncryptor := TXceedEncryption.Create( self );
-
- //Clear the messages list box
- txtMessages.Clear();
-
- //Create and prepare the encryption method
- if( CreateEncryptionMethod( xEncryptor ) ) then
- begin
- try
- //Process the file specifying:
- // - the source file name
- // - we want to decrypt the entire file
- // - decrypt and it's the end of the data
- // - the destination file name and overwrite it
- // - the variable that will contain the number of bytes read
- xEncryptor.ProcessFile( sSourceFilename, 0, 0, efpDecrypt, true,
- sDecryptedFilename, false, vaBytesRead );
-
- //Display a message of success
- txtMessages.Text := txtMessages.Text +
- sSourceFilename + ' successfully decrypted in ' + sDecryptedFilename + #13#10;
- except
- on xErr : Exception do
- //Display that an error occured
- txtMessages.Text := txtMessages.Text +
- sSourceFilename + ' failed to decrypt!' + #13#10 +
- xErr.Message + #13#10;
- end;
- end;
- //Deallocate the Encryption object. The encryption object will free
- //the EncryptionMethod object
- xEncryptor.Free();
- Self.Cursor := crDefault;
- DecryptFile := true;
- end;
-
- {---------------------------------------------------------------}
- { Function that performs the actual encryption of a source file }
- { to a destination file. }
- {---------------------------------------------------------------}
- function TfrmMain.EncryptFile( sSourceFilename : string; sEncryptedFilename : string ) : boolean;
- var
- xEncryptor : TXceedEncryption; //our XceedEncryption object
- vaBytesRead : OleVariant;
- begin
- Self.Cursor := crHourGlass;
-
- //Create and instance of the XceedEncryption object
- xEncryptor := TXceedEncryption.Create( self );
-
- //Clear the messages list box
- txtMessages.Clear();
-
- //Create and prepare the encryption method
- if( CreateEncryptionMethod( xEncryptor ) ) then
- begin
- try
- //Process the file specifying:
- // - the source file name
- // - we want to encrypt the entire file
- // - encrypt and it's the end of the data
- // - the destination file name and overwrite it
- // - the variable that will contain the number of bytes read
-
- xEncryptor.ProcessFile( sSourceFilename, 0, 0, efpEncrypt, true,
- sEncryptedFilename, false, vaBytesRead );
-
- //Display a message of success
- txtMessages.Text := txtMessages.Text +
- sSourceFilename + ' successfully encrypted in ' + sEncryptedFilename + #13#10;
- except
- on xErr : Exception do
- //Display that an error occured
- txtMessages.Text := txtMessages.Text +
- sSourceFilename + ' failed to encrypt!' + #13#10 +
- xErr.Message + #13#10;
- end;
- end;
- //Deallocate the Encryption object. The encryption object will free
- //the EncryptionMethod object
- xEncryptor.Free();
- Self.Cursor := crDefault;
- EncryptFile := true;
- end;
-
- {---------------------------------------------------------------}
- { Returns the path and filename without it's extension }
- {---------------------------------------------------------------}
- function TfrmMain.RemoveFileExtension( sFilename : string ) : string;
- var
- i : integer;
- nFilenameLen : integer;
- nLenToRemove : integer;
- begin
- nFilenameLen := Length( sFilename );
- i := nFilenameLen;
- nLenToRemove := -1;
-
- while ( i > 0 ) and ( nLenToRemove = -1 ) do
- begin
- if( copy( sFilename, i, 1 ) = '.' ) then
- nLenToRemove := i - 1;
-
- if( copy( sFilename, i, 1 ) = '\' ) then
- nLenToRemove := nFilenameLen;
-
- i := i - 1;
- end;
-
- if( nLenToRemove = -1 ) then
- RemoveFileExtension := ''
- else
- RemoveFileExtension := copy( sFilename, 0, nLenToRemove );
- end;
-
- {---------------------------------------------------------------}
- { Assign a default value to the destination file name if the }
- { destination text box is empty. }
- {---------------------------------------------------------------}
- procedure TfrmMain.SetDestinationFilename();
- var
- sEncryptedFilename : string;
- begin
- sEncryptedFilename := edtDestinationFile.Text;
-
- if( length( sEncryptedFilename ) = 0 ) then
- begin
- sEncryptedFilename := RemoveFileExtension( edtSourceFile.Text );
- if( length( sEncryptedFilename ) <> 0 ) then
- begin
- if( m_eEncryptionMethod = eemRijndael ) then
- edtDestinationFile.Text := trim( sEncryptedFilename + '.aes' )
- else
- edtDestinationFile.Text := trim( sEncryptedFilename + '.2fs' );
- end;
- end;
-
- end;
-
- end.
-
-