Root > Reference > All Functions > TEAEncrypt

Procedure TEAEncrypt

Previous pageReturn to chapter overviewNext page   

Encrypts "clear text" data by using TEA cipher.

 

Unit

EEncrypt

 

Syntax

 

Code (Delphi)

procedure TEAEncrypt(

const AKey: TTEAKey;

const AData, AEncryptedData: Pointer;

const ADataSize: Cardinal

); overload;

 

function TEAEncrypt(

const AKey: TTEAKey;

const AData: RawByteString

): RawByteString; overload;

 

procedure TEAEncrypt(

const AKey: TTEAKey;

AInPlaceData: TEncryptBuffer

); overload;

 

Parameters

AKey [in]

A key for encryption from TEADeriveKey function.

 

AData [in]

A data to encrypt.

 

AEncryptedData [out]

An encrypted AData's data. Must have the same size as AData. Data is returned in little-endian format.

 

ADataSize [in]

Size of AData/AEncryptedData.

 

AData [in]

String "clear text" data to encrypt.

 

AInPlaceData [in, out]

On input: "clear text" buffer to encrypt.

On output: encrypted buffer (same size, lettle-endian).

 

Return value

Encrypted AData's data in a string form. Data is returned in little-endian format.

 

Remarks

This function encrypts data passed in AData parameter by symmetric TEA cipher and outputs encrypted data back to AEncryptedData parameter. AEncryptedData expected to have the same size as AData. In other words, symmetric encryption with TEA does not change the size of the data.

 

Note

AEncryptedData value can also point to the same place as AData - to make "in place" encryption, but it is better to use overloaded variant of TEAEncrypt with AInPlace parameter.

 

The overload variant with RawByteString encrypts AData parameter and outputs encrypted data as function's result.

 

The overload variant with AInPlaceData encrypts data "in place".

 

Tip

Using salt in TEADeriveKey function means that final size of encrypted/decrypted data will change - because salt must be stored with encrypted data. If you do not use salt - the data size will be the same. However, data size for TEAEncrypt function is never changed (even if salt is used).

 

Tiny Encryption Algorithm (TEA) is a block cipher notable for its simplicity of description and implementation, typically a few lines of code. TEA operates on two 32-bit unsigned integers (could be derived from a 64-bit data block) and uses a 128-bit key.

 

Important!

TEA has a few weaknesses. Most notably, it suffers from equivalent keys—each key is equivalent to three others, which means that the effective key size is only 126 bits. As a result, TEA is especially bad as a cryptographic hash function. TEA is also susceptible to a related-key attack which requires 2^23 chosen plaintexts under a related-key pair, with 2^32 time complexity.

 

Examples

 

Code (Delphi)

var

Key: TTEAKey;

MS: TMemoryStream;

EncryptedData: Pointer;

EncryptedDataSize: Integer;

FS: TFileStream;

begin

MS := TMemoryStream.Create;

try

// Load C:\Text.txt file into MS

MS.LoadFromFile('C:\Text.txt');

 

// Allocate buffer for encryption - must have the same size

EncryptedDataSize := MS.Size;

EncryptedData := AllocMem(EncryptedDataSize);

try

 

// Create new key from password

Key := TEADeriveKey('password'); // - always the same for the same password

 

// Encrypt MS into EncryptedData

TEAEncrypt(Key, MS.Memory, EncryptedData, MS.Size { same as EncryptedDataSize } );

 

// Wipe key from memory - do this as soon as possible

TEADestroyKey(Key); // - same as SecureFree(Key);

 

// No longer needed, so we can safely delete it

SecureFree(MS);

 

// Save encrypted buffer into C:\Text.bin

FS := TFileStream.Create('C:\Text.bin', fmCreate or fmShareExclusive);

try

 FS.WriteBuffer(EncryptedData^, EncryptedDataSize);

finally

 FreeAndNil(FS);

end;

 

finally

SecureFree(EncryptedData, EncryptedDataSize);

end;

 

finally

FreeAndNil(MS);

end;

end;

 

 

Code (Delphi)

var

Key: TTEAKey;

EncryptedText: RawByteString;

S: String;

begin

// Create new key from password

Key := TEADeriveKey('password'); // - always the same for the same password

 

// Encrypt some text with the above key

// (meaning "encrypt some text with the above password")

EncryptedText := TEAEncrypt(Key, 'clear text');

 

// Wipe key from memory - do this as soon as possible

TEADestroyKey(Key); // - same as SecureFree(Key);

 

// EncryptedText contains encrypted RAW bytes

// Do not output EncryptedText to any place which expects human-readable text

 

// Debug output

// Encrypted text is RAW bytes, so we need to convert it to something readable

// Encrypted value will be the same for the same original text and password

Edit1.Text := HexEncodeString(EncryptedText);

 

// _____________________________________________

 

// Create new key from password

Key := TEADeriveKey('password'); // - always the same for the same password

 

// Decrypt text back

// Use encrypted RAW binary, not the hex-converted text

S := TEADecrypt(Key, EncryptedText);

 

// Wipe key from memory - do this as soon as possible

TEADestroyKey(Key); // - same as SecureFree(Key);

 

// Wipe also all data

SecureFree(EncryptedText);

 

// Now S = 'clear text'

 

// Call SecureFree for all data once you have finished working with it

SecureFree(S);

end;

 

 

Code (Delphi)

var

Key: TTEAKey;

Data: TEncryptBuffer;

FS: TFileStream;

begin

// Mark buffer as emtpy

Data.pbData := nil;

Data.cbData := 0;

try

 

// Load C:\Text.txt file into Data

FS := TFileStream.Create('C:\Text.txt', fmOpenRead or fmShareDenyWrite);

try

Data.cbData := FS.Size;

Data.pbData := AllocMem(Data.cbData);

FS.ReadBuffer(Data.pbData^, Data.cbData);

finally

FreeAndNil(FS);

end;

 

// Create new key from password

Key := TEADeriveKey('password'); // - always the same for the same password

 

// Encrypt Data in place

TEAEncrypt(Key, Data);

 

// Wipe key from memory - do this as soon as possible

TEADestroyKey(Key); // - same as SecureFree(Key);

 

// Data now holds encrypted binary data

 

// Debug output

Memo1.Lines.Text := HexEncodeToString(Data.pbData, Data.cbData);

 

// Save encrypted data into new file

FS := TFileStream.Create('C:\Text.bin', fmCreate or fmShareExclusive);

try

FS.WriteBuffer(Data.pbData^, Data.cbData);

finally

FreeAndNil(FS);

end;

 

finally

SecureFree(Data);

end;

end;

 

See also




Send feedback... Build date: 2024-09-30
Last edited: 2024-09-30
PRIVACY STATEMENT
The documentation team uses the feedback submitted to improve the EurekaLog documentation. We do not use your e-mail address for any other purpose. We will remove your e-mail address from our system after the issue you are reporting has been resolved. While we are working to resolve this issue, we may send you an e-mail message to request more information about your feedback. After the issues have been addressed, we may send you an email message to let you know that your feedback has been addressed.


Permanent link to this article: https://www.eurekalog.com/help/eurekalog/topic_function_eencrypt_teaencrypt.php