{*****************************************************************************
The DEC team (see file NOTICE.txt) licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. A copy of this licence is found in the root directory
of this project in the file LICENCE.txt or alternatively at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*****************************************************************************}
unit DECCipherInterface;
interface
uses
{$IFDEF FPC}
SysUtils, Classes,
{$ELSE}
System.SysUtils, System.Classes,
{$ENDIF}
DECUtil, DECCipherBase, DECFormatBase;
type
///
/// Common interface for all ciphers. Some ciphers may have additional
/// methods/properties though!
///
IDECCipher = Interface
///
/// Encrypts the contents of a given byte array
///
///
/// Byte array with data to be encrypted
///
///
/// Byte array with encrypted data
///
function EncodeBytes(const Source: TBytes): TBytes;
///
/// Decrypts the contents of a given byte array
///
///
/// Byte array with data to be decrypted
///
///
/// Byte array with decrypted data
///
function DecodeBytes(const Source: TBytes): TBytes;
///
/// Encrypts the data contained in a given stream
///
///
/// Source stream containing the data to encrypt
///
///
/// Destination stream, where the encrypted data shall be put in
///
///
/// Number of bytes of Source to be encrypted
///
///
/// optional callback for reporting progress of the operation
///
procedure EncodeStream(const Source, Dest: TStream; DataSize: Int64;
const OnProgress: TDECProgressEvent = nil);
///
/// Decrypts the data contained in a given stream
///
///
/// Source stream containing the data to decrypt
///
///
/// Destination stream, where the decrypted data shall be put in
///
///
/// Number of bytes of Source to be decrypted
///
///
/// optional callback for reporting progress of the operation
///
procedure DecodeStream(const Source, Dest: TStream; DataSize: Int64;
const OnProgress: TDECProgressEvent = nil);
///
/// Reads the contents of one file, encrypts it and stores it in another file
///
///
/// Path and name of the file to encrypt
///
///
/// Path and name of the file the encrypted data shall be stored in
///
///
/// Optional event which can be passed to get information about the
/// progress of the encryption operation
///
procedure EncodeFile(const SourceFileName, DestFileName: string;
const OnProgress: TDECProgressEvent = nil);
///
/// Reads the contents of one file, decrypts it and stores it in another file
///
///
/// Path and name of the file to decrypt
///
///
/// Path and name of the file the decrypted data shall be stored in
///
///
/// Optional event which can be passed to get information about the
/// progress of the decryption operation
///
procedure DecodeFile(const SourceFileName, DestFileName: string;
const OnProgress: TDECProgressEvent = nil);
///
/// Encrypts the contents of the passed unicode string
///
///
/// String to encrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Encoded will be the encrypted data, not the
/// source data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Encrypted string as a byte array
///
function EncodeStringToBytes(const Source: string; Format: TDECFormatClass = nil): TBytes; overload;
///
/// Encrypts the contents of the passed RawByteString
///
///
/// String to encrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Encoded will be the encrypted data, not the
/// source data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Encrypted string as a byte array
///
function EncodeStringToBytes(const Source: RawByteString; Format: TDECFormatClass = nil): TBytes; overload;
///
/// Encrypts the contents of the passed unicode string
///
///
/// String to encrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Encoded will be the encrypted data, not the
/// source data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Encrypted string
///
///
/// The use of this method is only recommended if a formatting is passed
/// which will result in an 7-bit ASCII compatible string as we cannot
/// ensure that Unicode string processing will not alter/interpret some
/// byte combinations in a destructive way, making the encrypted string
/// un-decryptable.
///
function EncodeStringToString(const Source: string; Format: TDECFormatClass = nil): string; overload;
///
/// Encrypts the contents of the passed unicode string
///
///
/// String to encrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Encoded will be the encrypted data, not the
/// source data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Encrypted string
///
///
/// The use of this method is only recommended if a formatting is passed
/// which will result in an 7-bit ASCII compatible string as we cannot
/// ensure that string processing will not alter/interpret some
/// byte combinations in a destructive way, making the encrypted string
/// un-decryptable.
///
function EncodeStringToString(const Source: RawByteString; Format: TDECFormatClass = nil): RawByteString; overload;
///
/// Decrypts the contents of the passed encrypted unicode string
///
///
/// String to decrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Decoded will be the still encrypted data, not the
/// encrypted data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Decrypted string as a byte array
///
function DecodeStringToBytes(const Source: string; Format: TDECFormatClass = nil): TBytes; overload;
///
/// Decrypts the contents of the passed encrypted RawByteString
///
///
/// String to decrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Decoded will be the still encrypted data, not the
/// encrypted data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Decrypted string as a byte array
///
function DecodeStringToBytes(const Source: RawByteString; Format: TDECFormatClass = nil): TBytes; overload;
///
/// Decrypts the contents of the passed Unicode string
///
///
/// String to decrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Decoded will be the encrypted data, not the
/// decrypted data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Decrypted string
///
///
/// The use of this method is only recommended if a formatting is passed
/// which uses an 7-bit ASCII compatible string as input so that it
/// didn't get altered by Unicode string processing in some hafrmful way
///
function DecodeStringToString(const Source: string; Format: TDECFormatClass = nil): string; overload;
///
/// Decrypts the contents of the passed RawByteString string
///
///
/// String to decrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Decoded will be the encrypted data, not the
/// decrypted data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Decrypted string
///
///
/// The use of this method is only recommended if a formatting is passed
/// which uses an 7-bit ASCII compatible string as input so that it
/// didn't get altered by string processing in some hafrmful way
///
function DecodeStringToString(const Source: RawByteString; Format: TDECFormatClass = nil): RawByteString; overload;
{$IFDEF ANSISTRINGSUPPORTED}
///
/// Encrypts the contents of the passed Ansistring
///
///
/// String to encrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Encoded will be the encrypted data, not the
/// source data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Encrypted string as a byte array
///
function EncodeStringToBytes(const Source: AnsiString; Format: TDECFormatClass = nil): TBytes; overload;
///
/// Encrypts the contents of the passed Ansistring
///
///
/// String to encrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Encoded will be the encrypted data, not the
/// source data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Encrypted string as an AnsiString
///
///
/// The use of this method is only recommended if a formatting is passed
/// which will result in an 7-bit ASCII compatible string as we cannot
/// ensure that string processing will not alter/interpret some
/// byte combinations in a destructive way, making the encrypted string
/// un-decryptable.
///
function EncodeStringToString(const Source: AnsiString; Format: TDECFormatClass = nil): AnsiString; overload;
///
/// Decrypts the contents of the passed encrypted Ansistring
///
///
/// String to decrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Decoded will be the still encrypted data, not the
/// encrypted data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Decrypted string as a byte array
///
function DecodeStringToBytes(const Source: AnsiString; Format: TDECFormatClass = nil): TBytes; overload;
///
/// Decrypts the contents of the passed AnsiString string
///
///
/// String to decrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Decoded will be the encrypted data, not the
/// decrypted data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Decrypted string
///
///
/// The use of this method is only recommended if a formatting is passed
/// which uses an 7-bit ASCII compatible string as input so that it
/// didn't get altered by string processing in some hafrmful way
///
function DecodeStringToString(const Source: AnsiString; Format: TDECFormatClass = nil): AnsiString; overload;
{$ENDIF}
{$IFNDEF NEXTGEN}
///
/// Encrypts the contents of the passed Widestring
///
///
/// String to encrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Encoded will be the encrypted data, not the
/// source data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Encrypted string as a byte array
///
function EncodeStringToBytes(const Source: WideString; Format: TDECFormatClass = nil): TBytes; overload;
///
/// Encrypts the contents of the passed Widestring
///
///
/// String to encrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Encoded will be the encrypted data, not the
/// source data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Encrypted string as an WideString
///
///
/// The use of this method is only recommended if a formatting is passed
/// which will result in an 7-bit ASCII compatible string as we cannot
/// ensure that string processing will not alter/interpret some
/// byte combinations in a destructive way, making the encrypted string
/// un-decryptable.
///
function EncodeStringToString(const Source: WideString; Format: TDECFormatClass = nil): WideString; overload;
///
/// Decrypts the contents of the passed encrypted Widestring
///
///
/// String to decrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Decoded will be the still encrypted data, not the
/// encrypted data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Decrypted string as a byte array
///
function DecodeStringToBytes(const Source: WideString; Format: TDECFormatClass = nil): TBytes; overload;
///
/// Decrypts the contents of the passed WideString string
///
///
/// String to decrypt
///
///
/// Optional parameter. One can pass a class reference of one of the
/// concrete data formatting classes here which will be internally used
/// to convert the data. Decoded will be the encrypted data, not the
/// decrypted data. Formattings can be used to convert data into a format
/// suitable for the transport medium the data shall be transported with.
///
///
/// Decrypted string
///
///
/// The use of this method is only recommended if a formatting is passed
/// which uses an 7-bit ASCII compatible string as input so that it
/// didn't get altered by string processing in some hafrmful way
///
function DecodeStringToString(const Source: WideString; Format: TDECFormatClass = nil): WideString; overload;
{$ENDIF}
///
/// Initializes the cipher with the necessary encryption/decryption key
///
///
/// Encryption/decryption key. Recommended/required key length is dependant
/// on the concrete algorithm.
///
///
/// Size of the key in bytes
///
///
/// Initialization vector. This contains the values the first block of
/// data to be processed is linked with. This is being done the same way
/// as the 2nd block of the data to be processed will be linked with the
/// first block and so on and this is dependant on the cypher mode set via
/// Mode property
///
///
/// Size of the initialization vector in bytes
///
///
/// optional parameter defining the value with which the last block will
/// be filled up if the size of the data to be processed cannot be divided
/// by block size without reminder. Means: if the last block is not
/// completely filled with data.
///
procedure Init(const Key; Size: Integer; const IVector; IVectorSize: Integer; IFiller: Byte = $FF); overload;
///
/// Initializes the cipher with the necessary encryption/decryption key
///
///
/// Encryption/decryption key. Recommended/required key length is dependant
/// on the concrete algorithm.
///
///
/// Initialization vector. This contains the values the first block of
/// data to be processed is linked with. This is being done the same way
/// as the 2nd block of the data to be processed will be linked with the
/// first block and so on and this is dependant on the cypher mode set via
/// Mode property
///
///
/// optional parameter defining the value with which the last block will
/// be filled up if the size of the data to be processed cannot be divided
/// by block size without reminder. Means: if the last block is not
/// completely filled with data.
///
procedure Init(const Key: TBytes; const IVector: TBytes; IFiller: Byte = $FF); overload;
///
/// Initializes the cipher with the necessary encryption/decryption key
///
///
/// Encryption/decryption key. Recommended/required key length is dependant
/// on the concrete algorithm.
///
///
/// Initialization vector. This contains the values the first block of
/// data to be processed is linked with. This is being done the same way
/// as the 2nd block of the data to be processed will be linked with the
/// first block and so on and this is dependant on the cypher mode set via
/// Mode property
///
///
/// optional parameter defining the value with which the last block will
/// be filled up if the size of the data to be processed cannot be divided
/// by block size without reminder. Means: if the last block is not
/// completely filled with data.
///
procedure Init(const Key: RawByteString; const IVector: RawByteString = ''; IFiller: Byte = $FF); overload;
{$IFDEF ANSISTRINGSUPPORTED}
///
/// Initializes the cipher with the necessary encryption/decryption key.
/// Only for use with the classic desktop compilers.
///
///
/// Encryption/decryption key. Recommended/required key length is dependant
/// on the concrete algorithm.
///
///
/// Initialization vector. This contains the values the first block of
/// data to be processed is linked with. This is being done the same way
/// as the 2nd block of the data to be processed will be linked with the
/// first block and so on and this is dependant on the cypher mode set via
/// Mode property
///
///
/// optional parameter defining the value with which the last block will
/// be filled up if the size of the data to be processed cannot be divided
/// by block size without reminder. Means: if the last block is not
/// completely filled with data.
///
procedure Init(const Key: AnsiString; const IVector: AnsiString = ''; IFiller: Byte = $FF); overload;
{$ENDIF}
{$IFNDEF NEXTGEN}
///
/// Initializes the cipher with the necessary encryption/decryption key.
/// Only for use with the classic desktop compilers.
///
///
/// Encryption/decryption key. Recommended/required key length is dependant
/// on the concrete algorithm.
///
///
/// Initialization vector. This contains the values the first block of
/// data to be processed is linked with. This is being done the same way
/// as the 2nd block of the data to be processed will be linked with the
/// first block and so on and this is dependant on the cypher mode set via
/// Mode property
///
///
/// optional parameter defining the value with which the last block will
/// be filled up if the size of the data to be processed cannot be divided
/// by block size without reminder. Means: if the last block is not
/// completely filled with data.
///
procedure Init(const Key: WideString; const IVector: WideString = ''; IFiller: Byte = $FF); overload;
{$ENDIF}
///
/// Returns the currently set cipher block mode, means how blocks are
/// linked to each other in order to avoid certain attacks.
///
function GetMode: TCipherMode;
///
/// Sets the cipher mode, means how each block is being linked with his
/// predecessor to avoid certain attacks
///
procedure SetMode(Value: TCipherMode);
///
/// Mode used for padding data to be encrypted/decrypted. See TCipherMode.
///
property Mode: TCipherMode
read GetMode
write SetMode;
end;
implementation
end.